summaryrefslogtreecommitdiff
path: root/noncore/settings/tabmanager/tabmanager.cpp
authorllornkcor <llornkcor>2002-05-06 17:45:44 (UTC)
committer llornkcor <llornkcor>2002-05-06 17:45:44 (UTC)
commitd49663597393e1b9e63041964a080069e4b6a664 (patch) (unidiff)
treeba788e8123b5e98d80ab2eea4012f31119968aa8 /noncore/settings/tabmanager/tabmanager.cpp
parent4a5885f1dd1328432e3bd3ef7cb2efd9a336b78b (diff)
downloadopie-d49663597393e1b9e63041964a080069e4b6a664.zip
opie-d49663597393e1b9e63041964a080069e4b6a664.tar.gz
opie-d49663597393e1b9e63041964a080069e4b6a664.tar.bz2
initial
Diffstat (limited to 'noncore/settings/tabmanager/tabmanager.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/settings/tabmanager/tabmanager.cpp473
1 files changed, 473 insertions, 0 deletions
diff --git a/noncore/settings/tabmanager/tabmanager.cpp b/noncore/settings/tabmanager/tabmanager.cpp
new file mode 100644
index 0000000..db65d26
--- a/dev/null
+++ b/noncore/settings/tabmanager/tabmanager.cpp
@@ -0,0 +1,473 @@
1/*
2
3               =. This file is part of the OPIE Project
4             .=l. Copyright (c) 2002 Benjamin Meyer <meyerb@sharpsec.com>
5           .>+-=
6 _;:,     .>    :=|. This library is free software; you can
7.> <`_,   >  .   <= redistribute it and/or modify it under
8:`=1 )Y*s>-.--   : the terms of the GNU Library General Public
9.="- .-=="i,     .._ License as published by the Free Software
10 - .   .-<_>     .<> Foundation; either version 2 of the License,
11     ._= =}       : or (at your option) any later version.
12    .%`+i>       _;_.
13    .i_,=:_.      -<s. This library is distributed in the hope that
14     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
15    : ..    .:,     . . . without even the implied warranty of
16    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
17  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
18..}^=.=       =       ; Library General Public License for more
19++=   -.     .`     .: details.
20 :     =  ...= . :.=-
21 -.   .:....=;==+<; You should have received a copy of the GNU
22  -_. . .   )=.  = Library General Public License along with
23    --        :-=` this library; see the file COPYING.LIB.
24 If not, write to the Free Software Foundation,
25 Inc., 59 Temple Place - Suite 330,
26 Boston, MA 02111-1307, USA.
27
28*/
29
30#include "tabmanager.h"
31#include "app.h"
32#include "tabapplnk.h"
33
34#include <qpe/applnk.h>
35#include <qpe/qpeapplication.h>
36#include <qdir.h>
37#include <qfile.h>
38#include <qtextstream.h>
39#include <qlistview.h>
40#include <qheader.h>
41#include <qlineedit.h>
42#include <qlabel.h>
43#include <qmessagebox.h>
44#include <stdlib.h>
45#include <qpe/qcopenvelope_qws.h>
46
47#define HOME_APP_DIR QPEApplication::qpeDir()+"/apps" // <-- made this a little more flexible
48#define HOME_APP_INSTALL_DIR "/usr/lib/ipkg/info"
49#define NEW_FOLDER "EmptyTab"
50#define NEW_APPLICATION "NewApp"
51#define APPLICATION_EXTENSION ".desktop"
52#define APPLICATION_EXTENSION_LENGTH 8
53
54/**
55 * Constructor. Sets up signals. Performs initial scan of applications
56 * and tabs
57 */
58TabManager::TabManager( QWidget* parent, const char* name):TabManagerBase(parent, name), changed(false){
59 rescanFolder(HOME_APP_DIR);
60
61 // Connect the signals and slots
62 connect(tabList, SIGNAL(doubleClicked(QListViewItem *)), this, SLOT(editItem(QListViewItem*)));
63 (tabList->header())->hide();
64 connect(tabList, SIGNAL(moveItem(QListViewItem *, QListViewItem *)), this, SLOT(moveApplication(QListViewItem *, QListViewItem *)));
65}
66
67/**
68 * If anything in the tab's have been changed then update the system or alert
69 * the user.
70 */
71TabManager::~TabManager(){
72 if(changed){
73 //QMessageBox::critical(this, "Message", "Please restart Qtopia to see all new changes.",QString("Ok") );
74 //filename = fn;
75 QCopEnvelope e("QPE/System", "linkChanged(QString)");
76 //e << description << filename << mimetype;
77 QString link; //we'll just send an empty string
78 e << link;
79 }
80}
81
82/**
83 * Scans root directory for any tabs or applications. Will recursivly go down,
84 * but will not follow symlinks.
85 * @param directory - the directory to look in.
86 * @param parent - the parent to place any new tabs or apps into. If parent is
87 * NULL then the item is a tab and should be placed as a child of the window.
88 */
89void TabManager::rescanFolder(QString directory, QListViewItem* parent){
90 //qDebug(QString("rescanFolder: ") + directory.latin1());
91
92 QDir d;
93 d.setPath(directory);
94 // Show hidden files for .directories
95 d.setFilter( QDir::Files | QDir::Hidden | QDir::Dirs);
96
97 const QFileInfoList *list = d.entryInfoList();
98 QFileInfoListIterator it( *list ); // create list iterator
99 QFileInfo *fi; // pointer for traversing
100
101 while ( (fi=it.current()) ) { // for each file...
102 // If it is a dir and not .. or . then add it as a tab and go down.
103 if(fi->isDir()){
104 if(fi->fileName() != ".." && fi->fileName() != ".") {
105 QListViewItem* newItem;
106 if(!parent)
107 newItem = new QListViewItem(tabList, fi->fileName());
108 else
109 newItem = new QListViewItem(parent, fi->fileName());
110 itemList.insert(newItem, directory + "/" + fi->fileName() + "/.directory" );
111 rescanFolder(directory + "/" + fi->fileName(), newItem);
112 }
113 }
114 else{
115 // it is a file, if not a .directory add to parent.
116
117 // Change parents name and icon to reflect icon.
118 if(fi->fileName() == ".directory"){
119 AppLnk app(directory + "/" + fi->fileName());
120 if(parent){
121 parent->setPixmap(0,app.pixmap());
122 parent->setText(0, app.name());
123 }
124 }
125 else{
126 // Add any desktop files found.
127 QListViewItem* newItem;
128 if(directory != HOME_APP_DIR){
129 if(!parent)
130 newItem = new QListViewItem(tabList, fi->fileName());
131 else
132 newItem = new QListViewItem(parent, fi->fileName());
133 if(fi->fileName().right(APPLICATION_EXTENSION_LENGTH) == APPLICATION_EXTENSION){
134 AppLnk app(directory + "/" + fi->fileName());
135 newItem->setPixmap(0,app.pixmap());
136 newItem->setText(0, app.name());
137 itemList.insert(newItem, directory + "/" + fi->fileName());
138 }
139 }
140 }
141 }
142 ++it; // goto next list element
143 }
144}
145
146/**
147 * Create a new blank Tab.
148 * Create a physical folder with .directory file
149 * Create a item on the list
150 */
151void TabManager::newFolder(){
152 QDir r;
153 r.mkdir(QString(HOME_APP_DIR) + "/" + NEW_FOLDER);
154 system((QString("echo [Desktop Entry] | cat >> ") + HOME_APP_DIR + "/" + NEW_FOLDER "/.directory").latin1());
155 system((QString("echo Name=" NEW_FOLDER " | cat >> ") + HOME_APP_DIR + "/" + NEW_FOLDER "/.directory").latin1());
156
157 QString homeLocation = QString(HOME_APP_DIR) + "/" + NEW_FOLDER + "/.directory";
158 QListViewItem *newItem = new QListViewItem(tabList, NEW_FOLDER);
159 itemList.insert(newItem, homeLocation );
160
161 // We have changed something.
162 changed = true;
163}
164
165/**
166 * Create a new blank application
167 * Make sure a tab is selected
168 * create physical file
169 * fill file with default information (entry, name, type).
170 */
171void TabManager::newApplication(){
172 QListViewItem *item = tabList->currentItem();
173 if(!item || item->parent())
174 return;
175
176 QString parentDir = itemList[item].mid(0,itemList[item].length()-11);
177 QString homeLocation = parentDir + "/" NEW_APPLICATION APPLICATION_EXTENSION;
178 system((QString("echo [Desktop Entry] | cat >> ") + homeLocation).latin1());
179 system((QString("echo Name=" NEW_APPLICATION " | cat >> ") + homeLocation).latin1());
180 int slash = parentDir.findRev('/', -1);
181 QString folderName = parentDir.mid(slash+1, parentDir.length());
182
183 system((QString("echo Type=") + folderName + " | cat >> " + homeLocation).latin1());
184
185 // Insert into the tree
186 QListViewItem *newItem = new QListViewItem(item, NEW_APPLICATION);
187 itemList.insert(newItem, homeLocation );
188
189 // We have changed something.
190 changed = true;
191}
192
193/**
194 * Remove the item.
195 * Check if we can
196 * Prompt user
197 * Delete physical file (Dir, remove .dir, then dir. File, remove file)
198 * Remove from installer if need too.
199 */
200void TabManager::removeItem(){
201 // Make sure we can delete
202 QListViewItem *item = tabList->currentItem();
203 if(!item)
204 return;
205 if(item->childCount() > 0){
206 QMessageBox::critical(this, "Message", "Can't remove with applications\nstill in the group.",QString("Ok") );
207 return;
208 }
209
210 // Prompt.
211 int answer = QMessageBox::warning(this, "Message", "Are you sure you want to delete?","Yes", "Cancel", 0, 1 );
212 if (answer)
213 return;
214
215 bool removeSuccessfull = true;
216 QString location = itemList[item];
217 // Remove file (.directory in a Directory case)
218 if(!QFile::remove(location))
219 removeSuccessfull = false;
220
221 // Remove directory
222 if(item->parent() == NULL){
223 // Remove .directory file string
224 location = location.mid(0,location.length()-10);
225 QDir dir;
226 if(!dir.rmdir(location))
227 removeSuccessfull = false;
228 }
229
230 // If removing failed.
231 if(!removeSuccessfull){
232 qDebug((QString("removeItem: ") + location).latin1());
233 QMessageBox::critical(this, "Message", "Can't remove.",QString("Ok") );
234 return;
235 }
236
237 // Remove from the installer so it wont fail.
238 // Don't need to do this sense the current install uses rm -f so no error
239
240 // Remove from the gui list.
241 itemList.remove(item);
242 if(item->parent())
243 item->parent()->takeItem(item);
244 delete item;
245
246 // We have changed something.
247 changed = true;
248}
249
250/**
251 * Helper function. Edits the current item.
252 * calls editItem with the currently selected item.
253 */
254void TabManager::editCurrentItem(){
255 editItem(tabList->currentItem());
256}
257
258/**
259 * Edit the item that is passed in.
260 * Show application dialog and if anything changed
261 * @param item the item to edit.
262 */
263void TabManager::editItem( QListViewItem * item){
264 if(!item)
265 return;
266
267 TabAppLnk app(itemList[item]);
268 if(!app.isValid()){
269 qDebug(QString("editItem: Not a valid applnk file: ") + itemList[item].latin1());
270 return;
271 }
272
273 AppEdit application(this, "Application edit", true);
274 application.nameLineEdit->setText(app.name());
275 application.iconLineEdit->setText(app.pixmapString());
276 application.execLineEdit->setText(app.exec());
277 application.commentLineEdit->setText(app.comment());
278
279 if(item->parent() == NULL){
280 application.execLineEdit->setEnabled(false);
281 application.TextLabel3->setEnabled(false);
282 application.setCaption("Tab");
283 }
284
285 // Only do somthing if they hit OK
286 if(application.exec() == 0)
287 return;
288
289 // If nothing has changed exit (hmmm why did they hit ok?)
290 if(app.name() == application.nameLineEdit->text() &&
291 app.pixmapString() == application.iconLineEdit->text() &&
292 app.comment() == application.commentLineEdit->text() &&
293 app.exec() == application.execLineEdit->text())
294 return;
295
296 // Change the applnk file
297 QString oldName = app.name();
298 app.setName(application.nameLineEdit->text());
299 app.setIcon(application.iconLineEdit->text());
300 app.setComment(application.commentLineEdit->text());
301 app.setExec(application.execLineEdit->text());
302 if(!app.writeLink()){
303 QMessageBox::critical(this, "Message", "Can't save.",QString("Ok") );
304 return;
305 }
306
307 // Update the gui icon and name
308 item->setText(0,app.name());
309 item->setPixmap(0,app.pixmap());
310
311 // We have changed something.
312 changed = true;
313
314 // If we were dealing with a new folder or new application change
315 // the file names. Also change the item location in itemList
316 if(oldName == NEW_FOLDER){
317 QDir r;
318 QString oldName = itemList[item];
319 oldName = oldName.mid(0,oldName.length()-11);
320 QString newName = oldName.mid(0,oldName.length()-9);
321 newName = newName + "/" + app.name();
322 r.rename(oldName, newName);
323 itemList.remove(item);
324 itemList.insert(item, newName + "/.directory" );
325 }
326 else if(oldName == NEW_APPLICATION){
327 if(!item->parent())
328 return;
329 QString parentDir = itemList[item->parent()];
330 QDir r;
331 QString oldName = itemList[item];
332 QString newName = oldName.mid(0, parentDir.length()-10);
333 newName = newName + app.name() + APPLICATION_EXTENSION;
334 r.rename(oldName, newName);
335 itemList.remove(item);
336 itemList.insert(item, newName);
337 }
338}
339
340/**
341 * Move an application from one directory to another.
342 * Move in the gui, move in the applnk file, move in the installer.
343 * @param item the application to move
344 * @pearam newGroup the new parent of this application
345 */
346void TabManager::moveApplication(QListViewItem *item, QListViewItem *newGroup){
347 // Can we even move it?
348 if(!item || !item->parent() || newGroup->parent())
349 return;
350 if(item->parent() == newGroup)
351 return;
352
353 // Get the new folder, new file name,
354 QString newFolder = itemList[newGroup];
355 newFolder = newFolder.mid(0,newFolder.length()-11);
356 int slash = newFolder.findRev('/', -1);
357 QString folderName = newFolder.mid(slash+1, newFolder.length());
358
359 QString desktopFile = itemList[item];
360 slash = desktopFile.findRev('/', -1);
361 desktopFile = desktopFile.mid(slash, desktopFile.length());
362 newFolder = newFolder + desktopFile;
363
364 // Move file
365 QDir r;
366 if(!r.rename(itemList[item], newFolder)){
367 QMessageBox::critical(this, "Message", "Can't move application.",QString("Ok") );
368 return;
369 }
370 //qDebug((QString("moveApplication: ") + itemList[item]).latin1());
371 //qDebug((QString("moveApplication: ") + newFolder).latin1());
372
373 // Move in the gui
374 item->parent()->takeItem(item);
375 newGroup->insertItem(item);
376 newGroup->setOpen(true);
377
378 // Move file in the installer
379 QString installedAppFile;
380 if(findInstalledApplication(desktopFile, installedAppFile))
381 swapInstalledLocation(installedAppFile, desktopFile, newFolder);
382 else
383 qDebug("moveApplication: No installed app found for dekstop file");
384
385 // Move application type
386 AppLnk app(newFolder);
387 app.setType(folderName);
388 app.writeLink();
389
390 // Move in our internal list
391 itemList.remove(item);
392 itemList.insert(item, newFolder);
393
394 // We have changed something.
395 changed = true;
396}
397
398/**
399 * File the installed application that has this desktop file.
400 * Go through each file in HOME_APP_INSTALL_DIR and see if it contains desktop
401 * file
402 * @param desktopFile - the .desktop file to search for [foo.desktop]
403 * @param installedAppFile - location of the app install list
404 * @return true if successfull, false if file not found.
405 */
406bool TabManager::findInstalledApplication(QString desktopFile, QString &installedAppFile){
407
408 QDir d;
409 d.setPath(HOME_APP_INSTALL_DIR);
410 d.setFilter( QDir::Files );
411
412 const QFileInfoList *list = d.entryInfoList();
413 QFileInfoListIterator it( *list ); // create list iterator
414 QFileInfo *fi; // pointer for traversing
415
416 while ( (fi=it.current()) ) { // for each file...
417 QFile file(QString(HOME_APP_INSTALL_DIR) + "/" + fi->fileName());
418 if ( file.open(IO_ReadOnly) ) { // file opened successfully
419 QTextStream stream( &file ); // use a text stream
420 QString line;
421 while ( !stream.eof() ) { // until end of file...
422 line = stream.readLine(); // line of text excluding '\n'
423 if(line.contains(desktopFile)){
424 installedAppFile = QString(HOME_APP_INSTALL_DIR) + "/" + fi->fileName();
425 file.close();
426 return true;
427 }
428 }
429 file.close();
430 }
431 else
432 qDebug((QString("findInstalledApplication: Can't open file") + HOME_APP_INSTALL_DIR + "/" + fi->fileName()).latin1());
433 ++it; // goto next list element
434 }
435 return false;
436}
437
438/**
439 * Open a file and replace a file containing the old desktop file with the new.
440 * @param installedAppFile application installed list
441 * @param desktopFile old .desktop file
442 * @param newLocation new .desktop file
443 */
444void TabManager::swapInstalledLocation( QString installedAppFile, QString desktopFile, QString newLocation ){
445 QFile file(installedAppFile);
446 if ( !file.open(IO_ReadOnly) ){
447 qDebug(QString("swapInstalledLocation: Can't edit file: %1").arg(installedAppFile).latin1());
448 return;
449 }
450
451 QTextStream stream( &file ); // use a text stream
452 QString allLines;
453 while ( !stream.eof() ) { // until end of file...
454 QString line = stream.readLine(); // line of text excluding '\n'
455 if(line.contains(desktopFile))
456 allLines += newLocation;
457 else
458 allLines += line;
459 allLines += '\n';
460 }
461 file.close();
462
463 if ( !file.open(IO_ReadWrite) ){
464 qDebug(QString("swapInstalledLocation: Can't edit file: %1").arg(installedAppFile).latin1());
465 return;
466 }
467 QTextStream streamOut( &file );
468 streamOut << allLines;
469 file.close();
470}
471
472// tabmanager.cpp
473