ff05e28a2ec249841452ac5e78e0b185bdfc0435
[profile/ivi/qtdeclarative.git] / examples / tutorials / gettingStartedQml / filedialog / directory.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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     m_dir.cd( QDir::currentPath() );
52
53     //go to the saved directory. if not found, create save directory
54     m_saveDir = "saves";
55     if ( m_dir.cd(m_saveDir) == 0 ) {
56         m_dir.mkdir(m_saveDir);
57         m_dir.cd(m_saveDir);
58     }
59     m_filterList << "*.txt";
60     refresh();
61 }
62
63 /*
64 Directory::filesNumber
65 Return the number of Files
66 */
67 int Directory::filesCount() const
68 {
69     return m_fileList.size();
70 }
71
72 /*
73 Function called to append data onto list property
74 */
75 void appendFiles(QDeclarativeListProperty<File> * property, File * file)
76 {
77     Q_UNUSED(property);
78     Q_UNUSED(file);
79     //Do nothing. can't add to a directory using this method
80 }
81
82 /*
83 Function called to retrieve file in the list using an index
84 */
85 File* fileAt(QDeclarativeListProperty<File> * property, int index)
86 {
87     return static_cast< QList<File *> *>(property->data)->at(index);
88 }
89
90 /*
91 Returns the number of files in the list
92 */
93 int filesSize(QDeclarativeListProperty<File> * property) 
94 {
95     return static_cast< QList<File *> *>(property->data)->size();
96 }
97
98 /*
99 Function called to empty the list property contents
100 */
101 void clearFilesPtr(QDeclarativeListProperty<File> *property)
102 {
103     return static_cast< QList<File *> *>(property->data)->clear();
104 }
105
106 /*
107 Returns the list of files as a QDeclarativeListProperty.
108 */
109 QDeclarativeListProperty<File> Directory::files()
110 {
111     refresh();
112     return QDeclarativeListProperty<File>( this, &m_fileList, &appendFiles, &filesSize, &fileAt,  &clearFilesPtr );
113 }
114
115 /*
116 Return the name of the currently selected file
117 */
118 QString Directory::filename() const
119 {
120     return currentFile.name();
121 }
122
123 /*
124 Return the file's content as a string.
125 */
126 QString Directory::fileContent() const
127 {
128     return m_fileContent;
129 }
130
131 /*
132 Set the file name of the current file
133 */
134 void Directory::setFilename(const QString &str)
135 {
136     if( str != currentFile.name() ) {
137         currentFile.setName(str);
138         emit filenameChanged();
139     }
140 }
141
142 /*
143 Set the content of the file as a string
144 */
145 void Directory::setFileContent(const QString &str)
146 {
147     if(str != m_fileContent){
148         m_fileContent = str;
149         emit fileContentChanged();
150     }
151 }
152
153 /*
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.
156 */
157 void Directory::saveFile()
158 {
159     if(currentFile.name().size() == 0){
160         qWarning()<< "Empty filename. no save";
161         return;
162     }
163     QString extendedName = currentFile.name();
164     if(!currentFile.name().endsWith(".txt")){
165         extendedName.append(".txt");
166     }
167     QFile file( m_dir.filePath(extendedName) );
168     if ( file.open(QFile::WriteOnly | QFile::Truncate) ) {
169         QTextStream outStream(&file);
170         outStream << m_fileContent;
171     }
172     file.close();
173     refresh();
174     emit directoryChanged();
175 }
176
177 /*
178 Load the contents of a file.
179 Only loads files with a .txt extension
180 */
181 void Directory::loadFile()
182 {
183     m_fileContent.clear();
184     QString extendedName = currentFile.name();
185     if( !currentFile.name().endsWith(".txt") ) {
186         extendedName.append(".txt");
187     }
188
189     QFile file( m_dir.filePath(extendedName) );
190     if ( file.open(QFile::ReadOnly ) ) {
191         QTextStream inStream(&file);
192
193         QString line;
194         do {
195             line = inStream.read(75);
196             m_fileContent.append(line);
197         } while ( !line.isNull() ) ;
198     }
199     file.close();
200 }
201
202 /*
203 Reloads the content of the files list. This is to ensure that the newly
204 created files are added onto the list.
205 */
206 void Directory::refresh()
207 {
208     m_dirFiles = m_dir.entryList(m_filterList,QDir::Files,QDir::Name);
209     m_fileList.clear();
210
211     File * file;
212     for(int i = 0; i < m_dirFiles.size() ; i ++) {
213         file = new File();
214
215         if( m_dirFiles.at(i).endsWith(".txt") ) {
216             QString name = m_dirFiles.at(i);
217             file->setName( name.remove(".txt",Qt::CaseSensitive) );
218         }
219         else {
220             file->setName(m_dirFiles.at(i));
221         }
222         m_fileList.append(file);
223     }
224 }