1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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)
51 m_dir.cd( QDir::currentPath() );
53 //go to the saved directory. if not found, create save directory
55 if ( m_dir.cd(m_saveDir) == 0 ) {
56 m_dir.mkdir(m_saveDir);
59 m_filterList << "*.txt";
64 Directory::filesNumber
65 Return the number of Files
67 int Directory::filesCount() const
69 return m_fileList.size();
73 Function called to append data onto list property
75 void appendFiles(QDeclarativeListProperty<File> * property, File * file)
79 //Do nothing. can't add to a directory using this method
83 Function called to retrieve file in the list using an index
85 File* fileAt(QDeclarativeListProperty<File> * property, int index)
87 return static_cast< QList<File *> *>(property->data)->at(index);
91 Returns the number of files in the list
93 int filesSize(QDeclarativeListProperty<File> * property)
95 return static_cast< QList<File *> *>(property->data)->size();
99 Function called to empty the list property contents
101 void clearFilesPtr(QDeclarativeListProperty<File> *property)
103 return static_cast< QList<File *> *>(property->data)->clear();
107 Returns the list of files as a QDeclarativeListProperty.
109 QDeclarativeListProperty<File> Directory::files()
112 return QDeclarativeListProperty<File>( this, &m_fileList, &appendFiles, &filesSize, &fileAt, &clearFilesPtr );
116 Return the name of the currently selected file
118 QString Directory::filename() const
120 return currentFile.name();
124 Return the file's content as a string.
126 QString Directory::fileContent() const
128 return m_fileContent;
132 Set the file name of the current file
134 void Directory::setFilename(const QString &str)
136 if( str != currentFile.name() ) {
137 currentFile.setName(str);
138 emit filenameChanged();
143 Set the content of the file as a string
145 void Directory::setFileContent(const QString &str)
147 if(str != m_fileContent){
149 emit fileContentChanged();
154 Called from QML to save the file using the filename and file content.
155 Saving makes sure that the file has a .txt extension.
157 void Directory::saveFile()
159 if(currentFile.name().size() == 0){
160 qWarning()<< "Empty filename. no save";
163 QString extendedName = currentFile.name();
164 if(!currentFile.name().endsWith(".txt")){
165 extendedName.append(".txt");
167 QFile file( m_dir.filePath(extendedName) );
168 if ( file.open(QFile::WriteOnly | QFile::Truncate) ) {
169 QTextStream outStream(&file);
170 outStream << m_fileContent;
174 emit directoryChanged();
178 Load the contents of a file.
179 Only loads files with a .txt extension
181 void Directory::loadFile()
183 m_fileContent.clear();
184 QString extendedName = currentFile.name();
185 if( !currentFile.name().endsWith(".txt") ) {
186 extendedName.append(".txt");
189 QFile file( m_dir.filePath(extendedName) );
190 if ( file.open(QFile::ReadOnly ) ) {
191 QTextStream inStream(&file);
195 line = inStream.read(75);
196 m_fileContent.append(line);
197 } while ( !line.isNull() ) ;
203 Reloads the content of the files list. This is to ensure that the newly
204 created files are added onto the list.
206 void Directory::refresh()
208 m_dirFiles = m_dir.entryList(m_filterList,QDir::Files,QDir::Name);
212 for(int i = 0; i < m_dirFiles.size() ; i ++) {
215 if( m_dirFiles.at(i).endsWith(".txt") ) {
216 QString name = m_dirFiles.at(i);
217 file->setName( name.remove(".txt",Qt::CaseSensitive) );
220 file->setName(m_dirFiles.at(i));
222 m_fileList.append(file);