1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
15 ** * Redistributions of source code must retain the above copyright
16 ** notice, this list of conditions and the following disclaimer.
17 ** * Redistributions in binary form must reproduce the above copyright
18 ** notice, this list of conditions and the following disclaimer in
19 ** the documentation and/or other materials provided with the
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 ** the names of its contributors may be used to endorse or promote
23 ** products derived from this software without specific prior written
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
41 #include "directory.h"
47 Initialize the saves directory and creates the file list
49 Directory::Directory(QObject *parent) : QObject(parent)
53 m_dir.cd( QDir::currentPath());
55 //go to the saved directory. if not found, create save directory
57 if (m_dir.cd(m_saveDir) == 0){
58 m_dir.mkdir(m_saveDir);
61 m_filterList << "*.txt";
67 Directory::filesNumber
68 Return the number of Files
70 int Directory:: filesCount() const{
71 return m_fileList.size();
75 Function called to append data onto list property
77 void appendFiles(QDeclarativeListProperty<File> * property, File * file){
78 //Do nothing. can't add to a directory using this method
82 Function called to retrieve file in the list using an index
84 File* fileAt(QDeclarativeListProperty<File> * property, int index){
85 return static_cast< QList<File *> *>(property->data)->at(index);
89 Returns the number of files in the list
91 int filesSize(QDeclarativeListProperty<File> * property){
92 return static_cast< QList<File *> *>(property->data)->size();
96 Function called to empty the list property contents
98 void clearFilesPtr(QDeclarativeListProperty<File> *property){
99 return static_cast< QList<File *> *>(property->data)->clear();
103 Returns the list of files as a QDeclarativeListProperty.
105 QDeclarativeListProperty<File> Directory::files(){
108 // return QDeclarativeListProperty<QString>(this,m_filePtrList); //not recommended in the docs
109 return QDeclarativeListProperty<File>( this, &m_fileList, &appendFiles, &filesSize, &fileAt, &clearFilesPtr );
113 Return te name of the currently selected file
115 QString Directory::filename() const{
116 return currentFile.name();
120 Return the file's content as a string.
122 QString Directory::fileContent() const{
123 return m_fileContent;
127 Set the file name of the current file
129 void Directory::setFilename(const QString &str){
130 if(str != currentFile.name()){
131 currentFile.setName(str);
132 emit filenameChanged();
137 Set the content of the file as a string
139 void Directory::setFileContent(const QString &str){
140 if(str != m_fileContent){
142 emit fileContentChanged();
147 Called from QML to save the file using the filename and file content.
148 Saving makes sure that the file has a .txt extension.
150 void Directory::saveFile(){
152 if(currentFile.name().size() == 0){
153 qWarning()<< "Empty filename. no save";
157 QString extendedName = currentFile.name();
158 if(!currentFile.name().endsWith(".txt")){
159 extendedName.append(".txt");
162 QFile file( m_dir.filePath(extendedName) );
163 if (file.open(QFile::WriteOnly | QFile::Truncate)){
164 QTextStream outStream(&file);
165 outStream << m_fileContent;
169 emit directoryChanged();
173 Load the contents of a file.
174 Only loads files with a .txt extension
176 void Directory::loadFile(){
178 m_fileContent.clear();
179 QString extendedName = currentFile.name();
180 if(!currentFile.name().endsWith(".txt")){
181 extendedName.append(".txt");
184 QFile file( m_dir.filePath(extendedName) );
185 if (file.open(QFile::ReadOnly )){
186 QTextStream inStream(&file);
190 line = inStream.read(75);
191 m_fileContent.append(line);
192 }while (!line .isNull());
198 Reloads the content of the files list. This is to ensure that the newly
199 created files are added onto the list.
201 void Directory::refresh(){
202 m_dirFiles = m_dir.entryList(m_filterList,QDir::Files,QDir::Name);
206 for(int i = 0; i < m_dirFiles.size() ; i ++){
210 if(m_dirFiles.at(i).endsWith(".txt")){
211 QString name = m_dirFiles.at(i);
212 file->setName( name.remove(".txt",Qt::CaseSensitive));
215 file->setName(m_dirFiles.at(i));
217 m_fileList.append(file);