summaryrefslogtreecommitdiff
path: root/library/locationcombo.cpp
Unidiff
Diffstat (limited to 'library/locationcombo.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--library/locationcombo.cpp295
1 files changed, 295 insertions, 0 deletions
diff --git a/library/locationcombo.cpp b/library/locationcombo.cpp
new file mode 100644
index 0000000..31429f5
--- a/dev/null
+++ b/library/locationcombo.cpp
@@ -0,0 +1,295 @@
1/**********************************************************************
2** Copyright (C) 2000-2006 Trolltech AS. All rights reserved.
3**
4** This file is part of the Qtopia Environment.
5**
6** This program is free software; you can redistribute it and/or modify it
7** under the terms of the GNU General Public License as published by the
8** Free Software Foundation; either version 2 of the License, or (at your
9** option) any later version.
10**
11** A copy of the GNU GPL license version 2 is included in this package as
12** LICENSE.GPL.
13**
14** This program is distributed in the hope that it will be useful, but
15** WITHOUT ANY WARRANTY; without even the implied warranty of
16** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17** See the GNU General Public License for more details.
18**
19** In addition, as a special exception Trolltech gives permission to link
20** the code of this program with Qtopia applications copyrighted, developed
21** and distributed by Trolltech under the terms of the Qtopia Personal Use
22** License Agreement. You must comply with the GNU General Public License
23** in all respects for all of the code used other than the applications
24** licensed under the Qtopia Personal Use License Agreement. If you modify
25** this file, you may extend this exception to your version of the file,
26** but you are not obligated to do so. If you do not wish to do so, delete
27** this exception statement from your version.
28**
29** See http://www.trolltech.com/gpl/ for GPL licensing information.
30**
31** Contact info@trolltech.com if any conditions of this licensing are
32** not clear to you.
33**
34**********************************************************************/
35
36#include "locationcombo.h"
37
38#include <qpe/ir.h>
39#include <qpe/applnk.h>
40#include <qpe/global.h>
41#include <qpe/categorywidget.h>
42#include <qpe/categoryselect.h>
43#ifdef QWS
44#include <qpe/qcopenvelope_qws.h>
45#endif
46#include <qpe/filemanager.h>
47#include <qpe/config.h>
48#include <qpe/storage.h>
49#include <qpe/global.h>
50#include <qtopia/qpemessagebox.h>
51
52#include <qlineedit.h>
53#include <qtoolbutton.h>
54#include <qpushbutton.h>
55#include <qgroupbox.h>
56#include <qcheckbox.h>
57#include <qlabel.h>
58#include <qlayout.h>
59#include <qfile.h>
60#include <qdir.h>
61#include <qfileinfo.h>
62#include <qmessagebox.h>
63#include <qsize.h>
64#include <qcombobox.h>
65#include <qregexp.h>
66
67#include <qradiobutton.h>
68#include <qlayout.h>
69
70#include <stdlib.h>
71
72/*!
73 \class LocationCombo locationcombo.h
74 \brief The LocationCombo class displays a list of available storage
75 locations.
76
77 First availability: Qtopia 1.6
78
79 \ingroup qtopiaemb
80 \sa DocPropertiesDialog
81 */
82
83
84class LocationComboPrivate
85{
86public:
87 LocationComboPrivate() : homeLocation(-1), fileSize(0), listEmpty(TRUE) {}
88 QString originalPath;
89 int homeLocation;
90 int fileSize;
91 bool listEmpty;
92};
93
94/*!
95 Constructs a LocationCombo with parent \a parent and name \a name.
96 */
97LocationCombo::LocationCombo( QWidget *parent, const char *name )
98 : QComboBox( FALSE, parent, name )
99{
100 storage = new StorageInfo;
101 d = new LocationComboPrivate;
102 setLocation( 0 );
103 connect( this, SIGNAL(activated(int)), this, SIGNAL(newPath()) );
104 connect( storage, SIGNAL(disksChanged()), this, SLOT(updatePaths()) );
105}
106
107/*!
108 Constructs a LocationCombo with parent \a parent and name \a name.
109 \a lnk is pointer to an existing AppLnk.
110 */
111LocationCombo::LocationCombo( const AppLnk * lnk, QWidget *parent, const char *name )
112 : QComboBox( FALSE, parent, name )
113{
114 storage = new StorageInfo;
115 d = new LocationComboPrivate;
116 setLocation(lnk);
117 connect( this, SIGNAL(activated(int)), this, SIGNAL(newPath()) );
118 connect( storage, SIGNAL(disksChanged()), this, SLOT(updatePaths()) );
119}
120
121/*!
122 Destroys the widget.
123 */
124LocationCombo::~LocationCombo()
125{
126 delete storage;
127 delete d;
128}
129
130/*!
131 Sets the display of the LocationCombo to the location associated with the
132 AppLnk \a lnk.
133 */
134void LocationCombo::setLocation( const AppLnk * lnk )
135{
136 // NB: setLocation(const QString) assumes only lnk->file() is used.
137
138 if ( lnk ) {
139 QFileInfo fi( lnk->file() );
140 d->fileSize = fi.size();
141 const FileSystem *fs = storage->fileSystemOf( lnk->file() );
142 d->originalPath = fs ? fs->path() : QString::null;
143 } else {
144 d->fileSize = 0;
145 d->originalPath = QString::null;
146 }
147
148 setupCombo();
149
150 int currentLocation = -1;
151 if ( lnk ) {
152 int n = locations.count();
153 for ( int i = 0; i < n; i++ ) {
154 if ( lnk->file().contains( locations[i] ) )
155 currentLocation = i;
156 }
157 }
158 if ( currentLocation == -1 )
159 currentLocation = 0; //default to the first one
160
161 setCurrentItem( currentLocation );
162}
163
164/*!
165 \internal
166*/
167void LocationCombo::setupCombo()
168{
169 clear();
170 locations.clear();
171
172 const QList<FileSystem> &fs = storage->fileSystems();
173 QListIterator<FileSystem> it ( fs );
174 QString s;
175 QString homeDir = QDir::homeDirPath();
176 QString homeFs;
177 QString homeFsPath;
178 int index = 0;
179 for ( ; it.current(); ++it ) {
180 // we add 10k to the file size so we are sure we can also save the desktop file
181 if ( !d->fileSize || (*it)->path() == d->originalPath ||
182 (ulong)(*it)->availBlocks() * (ulong)(*it)->blockSize()
183 > (ulong)d->fileSize + 10000 )
184 {
185 if ( (*it)->isRemovable() ) {
186 insertItem( (*it)->name(), index );
187 locations.append( (*it)->path() );
188 index++;
189 } else if ( homeDir.contains( (*it)->path() ) &&
190 (*it)->path().length() > homeFsPath.length() ) {
191 homeFs = (*it)->name();
192 homeFsPath = (*it)->path();
193 }
194 }
195 }
196
197 // $HOME is *somewhere*, but not shown in Storage::fileSystems(),
198 // eg. because it's mounted in some unexpected way.
199 if ( homeFsPath.isEmpty() ) {
200 homeFs = StorageInfo::tr("Internal Storage");
201 homeFsPath = homeDir;
202 }
203
204 if ( !homeFsPath.isEmpty() ) {
205 d->homeLocation = 0;
206 insertItem( homeFs, d->homeLocation );
207 locations.prepend( homeDir );
208 } else {
209 d->homeLocation = -1;
210 }
211
212 d->listEmpty = locations.count() == 0;
213 if ( d->listEmpty ) {
214 insertItem( tr("No FileSystems Available!"), 0 );
215 locations.append( "" );
216 }
217}
218
219/*!
220 \internal
221 */
222void LocationCombo::updatePaths()
223{
224 QString oldPath = locations[currentItem()];
225
226 setupCombo();
227
228 int currentLocation = 0;
229 int n = locations.count();
230 for ( int i = 0; i < n; i++ ) {
231 if ( oldPath == locations[i] ) {
232 currentLocation = i;
233 }
234 }
235 setCurrentItem( currentLocation );
236 if ( locations[currentItem()] != oldPath )
237 emit newPath();
238}
239
240/*!
241 Returns TRUE to indicate that the user has changed the location displayed
242 by the LocationCombo. Most useful when the LocationCombo is part of a
243 dialog; when the dialog is accept()ed, LocationCombo::isChanged() can be
244 examined to check for a change of location.
245 */
246bool LocationCombo::isChanged() const
247{
248 if ( const FileSystem *fs = storage->fileSystemOf(locations[currentItem()]) )
249 return fs->path() != d->originalPath;
250
251 return TRUE;
252}
253
254/*!
255 Returns the default (home) location for the file associated with this
256 LocationCombo.
257 */
258QString LocationCombo::installationPath() const
259{
260 return currentItem() == d->homeLocation ?
261 QString("/") : locations[ currentItem() ]+"/";
262}
263
264/*!
265 Returns the document path associated with this LocationCombo. This
266 will be "\<path\>/Documents".
267 */
268QString LocationCombo::documentPath() const
269{
270 return locations[ currentItem() ]+"/Documents/";
271}
272
273/*!
274 Returns a pointer to FileSystem object, associated with the current
275 selection of the LocationCombo.
276 */
277const FileSystem *LocationCombo::fileSystem() const
278{
279 if ( d->listEmpty )
280 return 0;
281 return storage->fileSystemOf( locations[ currentItem() ] );
282}
283
284/*!
285 \fn LocationCombo::newPath()
286 Emitted when the LocationCombo changes to a new location.
287 */
288
289
290#if 0
291void LocationCombo::apply()
292{
293}
294#endif
295