Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / examples / tutorials / gettingStarted / gsQml / parts / part5 / filedialog / directory.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
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
20 **     distribution.
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
24 **     permission.
25 **
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."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "directory.h"
42 #include <QDebug>
43
44 /*
45 Directory constructor
46
47 Initialize the saves directory and creates the file list
48 */
49 Directory::Directory(QObject *parent) : QObject(parent)
50 {
51     
52
53     m_dir.cd( QDir::currentPath());
54     
55     //go to the saved directory. if not found, create save directory
56     m_saveDir = "saves";
57     if (m_dir.cd(m_saveDir) == 0){
58         m_dir.mkdir(m_saveDir);
59         m_dir.cd(m_saveDir);
60     }
61      m_filterList << "*.txt";
62
63     refresh();
64 }
65
66 /*
67 Directory::filesNumber
68 Return the number of Files
69 */
70 int Directory::    filesCount() const{
71     return m_fileList.size();
72 }
73
74 /*
75 Function called to append data onto list property
76 */
77 void appendFiles(QDeclarativeListProperty<File> * property, File * file){
78     //Do nothing. can't add to a directory using this method
79 }
80
81 /*
82 Function called to retrieve file in the list using an index
83 */
84 File* fileAt(QDeclarativeListProperty<File> * property, int index){
85     return static_cast< QList<File *> *>(property->data)->at(index);
86 }
87
88 /*
89 Returns the number of files in the list
90 */
91 int filesSize(QDeclarativeListProperty<File> * property){
92     return static_cast< QList<File *> *>(property->data)->size();
93 }
94
95 /*
96 Function called to empty the list property contents
97 */
98 void clearFilesPtr(QDeclarativeListProperty<File> *property){
99     return static_cast< QList<File *> *>(property->data)->clear();
100 }
101
102 /*
103 Returns the list of files as a QDeclarativeListProperty.
104 */
105 QDeclarativeListProperty<File> Directory::files(){
106
107     refresh();
108 //     return QDeclarativeListProperty<QString>(this,m_filePtrList);        //not recommended in the docs
109     return QDeclarativeListProperty<File>( this, &m_fileList, &appendFiles, &filesSize, &fileAt,  &clearFilesPtr );
110 }
111
112 /*
113 Return te name of the currently selected file
114 */
115 QString Directory::filename() const{
116     return currentFile.name();
117 }
118
119 /*
120 Return the file's content as a string.
121 */
122 QString Directory::fileContent() const{
123     return m_fileContent;
124 }
125
126 /*
127 Set the file name of the current file
128 */
129 void Directory::setFilename(const QString &str){
130     if(str != currentFile.name()){
131         currentFile.setName(str);
132         emit filenameChanged();
133     }
134 }
135
136 /*
137 Set the content of the file as a string
138 */
139 void Directory::setFileContent(const QString &str){
140     if(str != m_fileContent){
141         m_fileContent = str;
142         emit fileContentChanged();
143     }    
144 }
145
146 /*
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.
149 */
150 void Directory::saveFile(){
151     
152     if(currentFile.name().size() == 0){
153         qWarning()<< "Empty filename. no save";
154         return;
155     }
156         
157     QString extendedName = currentFile.name();
158     if(!currentFile.name().endsWith(".txt")){
159         extendedName.append(".txt");
160     }
161     
162     QFile file( m_dir.filePath(extendedName) );
163     if (file.open(QFile::WriteOnly | QFile::Truncate)){
164         QTextStream outStream(&file);
165         outStream << m_fileContent;
166     }
167     file.close();
168     refresh();
169     emit directoryChanged();
170 }
171
172 /*
173 Load the contents of a file.
174 Only loads files with a .txt extension
175 */
176 void Directory::loadFile(){
177
178     m_fileContent.clear();
179     QString extendedName = currentFile.name();
180     if(!currentFile.name().endsWith(".txt")){
181         extendedName.append(".txt");
182     }
183     
184     QFile file( m_dir.filePath(extendedName) );
185     if (file.open(QFile::ReadOnly )){
186         QTextStream inStream(&file);
187         
188         QString line;
189         do{
190             line = inStream.read(75);
191             m_fileContent.append(line);
192         }while (!line .isNull());
193     }
194     file.close();
195 }
196
197 /*
198 Reloads the content of the files list. This is to ensure that the newly
199 created files are added onto the list.
200 */
201 void Directory::refresh(){
202     m_dirFiles = m_dir.entryList(m_filterList,QDir::Files,QDir::Name);
203     m_fileList.clear();
204     
205     File * file;
206     for(int i = 0; i < m_dirFiles.size() ; i ++){
207         
208         file = new File();
209         
210         if(m_dirFiles.at(i).endsWith(".txt")){
211             QString name = m_dirFiles.at(i);
212             file->setName( name.remove(".txt",Qt::CaseSensitive));
213         }
214         else{
215             file->setName(m_dirFiles.at(i));
216         }
217         m_fileList.append(file);
218     }
219 }