Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

tabmanager.cpp

Go to the documentation of this file.
00001 #include "tabmanager.h"
00002 #include "app.h"
00003 #include "wait.h"
00004 #include "tabapplnk.h"
00005 
00006 #include <opie2/odebug.h>
00007 
00008 #include <qpe/applnk.h>
00009 #include <qdir.h>
00010 #include <qfile.h>
00011 #include <qtextstream.h>
00012 #include <qlistview.h>
00013 #include <qheader.h>
00014 #include <qcombobox.h>
00015 #include <qlineedit.h>
00016 #include <qlabel.h>
00017 #include <qmessagebox.h>
00018 #include <stdlib.h>
00019 #include <qpe/qcopenvelope_qws.h>
00020 #include <qpe/qpeapplication.h>
00021 #include <qpe/resource.h>
00022 
00023 
00024 #define HOME_APP_DIR QPEApplication::qpeDir()+"apps"
00025 #define HOME_APP_INSTALL_DIR "/usr/lib/ipkg/info"
00026 #define NEW_FOLDER "EmptyTab"
00027 #define NEW_APPLICATION "NewApp"
00028 #define APPLICATION_EXTENSION ".desktop"
00029 #define APPLICATION_EXTENSION_LENGTH 8
00030 
00035 TabManager::TabManager( QWidget* parent, const char* name):TabManagerBase(parent, name), changed(false), application(NULL){
00036   rescanFolder(HOME_APP_DIR);
00037 
00038   // Connect the signals and slots
00039   connect(tabList, SIGNAL(doubleClicked(QListViewItem*)), this, SLOT(editItem(QListViewItem*)));
00040   (tabList->header())->hide();
00041   connect(tabList, SIGNAL(moveItem(QListViewItem*,QListViewItem*)), this, SLOT(moveApplication(QListViewItem*,QListViewItem*)));
00042 }
00043 
00048 TabManager::~TabManager(){
00049   if(changed){
00050     // Prompt.
00051     //int answer = QMessageBox::warning(this, "Message", "Should your desktop be","Yes", "Cancel", 0, 1 );
00052     //if (answer)
00053     //  return;
00054     QCopEnvelope e("QPE/System", "linkChanged(QString)");
00055     QString link; //we'll just send an empty string
00056     e << link;
00057   }
00058 }
00059 
00067 void TabManager::rescanFolder(QString directory, QListViewItem* parent){
00068   //odebug << QString("rescanFolder: ") + directory.latin1() << oendl;
00069 
00070   QDir d;
00071   d.setPath(directory);
00072   // Show hidden files for .directories
00073   d.setFilter( QDir::Files | QDir::Hidden | QDir::Dirs);
00074 
00075   const QFileInfoList *list = d.entryInfoList();
00076   QFileInfoListIterator it( *list );      // create list iterator
00077   QFileInfo *fi;                          // pointer for traversing
00078 
00079   while ( (fi=it.current()) ) {           // for each file...
00080     // If it is a dir and not .. or . then add it as a tab and go down.
00081     if(fi->isDir()){
00082       if(fi->fileName() != ".." && fi->fileName() != ".") {
00083         QListViewItem* newItem;
00084         if(!parent)
00085           newItem = new QListViewItem(tabList, fi->fileName());
00086         else
00087           newItem = new QListViewItem(parent, fi->fileName());
00088         itemList.insert(newItem, directory + "/" + fi->fileName() + "/.directory" );
00089         rescanFolder(directory + "/" + fi->fileName(), newItem);
00090       }
00091     }
00092     else{
00093       // it is a file, if not a .directory add to parent.
00094 
00095       // Change parents name and icon to reflect icon.
00096       if(fi->fileName() == ".directory"){
00097         AppLnk app(directory + "/" + fi->fileName());
00098         if(parent){
00099           parent->setPixmap(0,app.pixmap());
00100           parent->setText(0, app.name());
00101         }
00102       }
00103       else{
00104         // Add any desktop files found.
00105         QListViewItem* newItem;
00106         if(directory != HOME_APP_DIR){
00107           if(!parent)
00108             newItem = new QListViewItem(tabList, fi->fileName());
00109           else
00110             newItem = new QListViewItem(parent, fi->fileName());
00111           if(fi->fileName().right(APPLICATION_EXTENSION_LENGTH) == APPLICATION_EXTENSION){
00112             AppLnk app(directory + "/" + fi->fileName());
00113             newItem->setPixmap(0,app.pixmap());
00114             newItem->setText(0, app.name());
00115             itemList.insert(newItem, directory + "/" + fi->fileName());
00116           }
00117         }
00118       }
00119     }
00120     ++it;                               // goto next list element
00121   }
00122 }
00123 
00129 void TabManager::newFolder(){
00130   QDir r;
00131   r.mkdir(QString(HOME_APP_DIR) + "/" + NEW_FOLDER);
00132   system((QString("echo [Desktop Entry] | cat >> ") +  HOME_APP_DIR + "/" + NEW_FOLDER "/.directory").latin1());
00133   system((QString("echo Name=" NEW_FOLDER " | cat >> ") +  HOME_APP_DIR + "/" + NEW_FOLDER "/.directory").latin1());
00134 
00135   QString homeLocation = QString(HOME_APP_DIR) + "/" + NEW_FOLDER + "/.directory";
00136   QListViewItem *newItem = new QListViewItem(tabList, NEW_FOLDER);
00137   itemList.insert(newItem, homeLocation );
00138 
00139   // We have changed something.
00140   changed = true;
00141 }
00142 
00149 void TabManager::newApplication(){
00150   QListViewItem *item = tabList->currentItem();
00151   if(!item || item->parent())
00152     return;
00153 
00154   QString parentDir = itemList[item].mid(0,itemList[item].length()-11);
00155   QString homeLocation = parentDir + "/" NEW_APPLICATION APPLICATION_EXTENSION;
00156   system((QString("echo [Desktop Entry] | cat >> ") +  homeLocation).latin1());
00157   system((QString("echo Name=" NEW_APPLICATION " | cat >> ") +  homeLocation).latin1());
00158   int slash = parentDir.findRev('/', -1);
00159   QString folderName = parentDir.mid(slash+1, parentDir.length());
00160 
00161   system((QString("echo Type=") + folderName + " | cat >> " +  homeLocation).latin1());
00162 
00163   // Insert into the tree
00164   QListViewItem *newItem = new QListViewItem(item, NEW_APPLICATION);
00165   itemList.insert(newItem, homeLocation );
00166 
00167   // We have changed something.
00168   changed = true;
00169 }
00170 
00178 void TabManager::removeItem(){
00179   // Make sure we can delete
00180   QListViewItem *item = tabList->currentItem();
00181   if(!item)
00182     return;
00183   if(item->childCount() > 0){
00184     QMessageBox::critical(this, tr("Message"), tr("Can't remove with applications\nstill in the group."), tr("Ok") );
00185     return;
00186   }
00187 
00188   // Prompt.
00189   int answer = QMessageBox::warning(this, tr("Message"), tr("Are you sure you want to delete?"), tr("Yes"), tr("Cancel"), 0, 1 );
00190   if (answer)
00191     return;
00192 
00193   bool removeSuccessful = true;
00194   QString location = itemList[item];
00195   // Remove file (.directory in a Directory case)
00196   if(!QFile::remove(location))
00197       removeSuccessful = false;
00198 
00199   // Remove directory
00200   if(item->parent() == NULL){
00201     // Remove .directory file string
00202     location = location.mid(0,location.length()-10);
00203     QDir dir;
00204     if(!dir.rmdir(location))
00205       removeSuccessful = false;
00206     else
00207       removeSuccessful = true;
00208   }
00209 
00210   // If removing failed.
00211   if(!removeSuccessful){
00212     odebug << (QString("removeItem: ") + location).latin1() << oendl;
00213     QMessageBox::critical(this, tr("Message"), tr("Can't remove."), tr("Ok") );
00214     return;
00215   }
00216 
00217   // Remove from the installer so it wont fail.
00218   // Don't need to do this sense the current install uses rm -f so no error
00219 
00220   // Remove from the gui list.
00221   itemList.remove(item);
00222   if(item->parent())
00223     item->parent()->takeItem(item);
00224   delete item;
00225 
00226   // We have changed something.
00227   changed = true;
00228 }
00229 
00234 void TabManager::editCurrentItem(){
00235   editItem(tabList->currentItem());
00236 }
00237 
00243 void TabManager::editItem( QListViewItem * item){
00244   if(!item)
00245     return;
00246 
00247   TabAppLnk app(itemList[item]);
00248   if(!app.isValid()){
00249     odebug << QString("editItem: Not a valid applnk file: ") + itemList[item].latin1() << oendl;
00250     return;
00251   }
00252 
00253   // Fill with all of the icons
00254   if(!application){
00255     Wait waitDialog(this, "Wait dialog");
00256     waitDialog.waitLabel->setText(tr("Gathering icons..."));
00257     waitDialog.show();
00258     qApp->processEvents();
00259     application = new AppEdit(this, "Application edit", true);
00260 
00261     QDir d(QPEApplication::qpeDir() + "pics/");
00262     d.setFilter( QDir::Files);
00263 
00264     const QFileInfoList *list = d.entryInfoList();
00265     QFileInfoListIterator it( *list );      // create list iterator
00266     QFileInfo *fi;                          // pointer for traversing
00267 
00268     while ( (fi=it.current()) ) {           // for each file...
00269       QString fileName = fi->fileName();
00270       if(fileName.right(4) == ".png"){
00271         fileName = fileName.mid(0,fileName.length()-4);
00272         QPixmap imageOfFile(Resource::loadPixmap(fileName));
00273         QImage foo = imageOfFile.convertToImage();
00274         foo = foo.smoothScale(16,16);
00275         imageOfFile.convertFromImage(foo);
00276         application->iconLineEdit->insertItem(imageOfFile,fileName);
00277       }
00278       //odebug << fi->fileName().latin1() << oendl;
00279       ++it;
00280     }
00281     waitDialog.hide();
00282   }
00283   int pixmap = -1;
00284   QString pixmapText = app.pixmapString();
00285   QComboBox *f = application->iconLineEdit;
00286   for(int i = 0; i < application->iconLineEdit->count(); i++){
00287     if(f->text(i) == pixmapText){
00288       pixmap = i;
00289       break;
00290     }
00291   }
00292   if(pixmap != -1)
00293     application->iconLineEdit->setCurrentItem(pixmap);
00294   else if(pixmapText.isEmpty()){
00295     application->iconLineEdit->setCurrentItem(0);
00296   }
00297   else{
00298     QPixmap imageOfFile(Resource::loadPixmap(pixmapText));
00299     QImage foo = imageOfFile.convertToImage();
00300     foo = foo.smoothScale(16,16);
00301     imageOfFile.convertFromImage(foo);
00302     application->iconLineEdit->insertItem(imageOfFile,pixmapText,0);
00303     application->iconLineEdit->setCurrentItem(0);
00304   }
00305 
00306   application->nameLineEdit->setText(app.name());
00307   application->execLineEdit->setText(app.exec());
00308   application->commentLineEdit->setText(app.comment());
00309 
00310   if(item->parent() == NULL){
00311     application->execLineEdit->setEnabled(false);
00312     application->TextLabel3->setEnabled(false);
00313     application->setCaption(tr("Tab"));
00314   }
00315   else{
00316     application->execLineEdit->setEnabled(true);
00317     application->TextLabel3->setEnabled(true);
00318     application->setCaption(tr("Application"));
00319   }
00320 
00321   // Only do somthing if they hit OK
00322   application->showMaximized();
00323   if(application->exec() == 0)
00324     return;
00325 
00326   // If nothing has changed exit (hmmm why did they hit ok?)
00327   if(app.name() == application->nameLineEdit->text() &&
00328      app.pixmapString() == application->iconLineEdit->currentText() &&
00329      app.comment() == application->commentLineEdit->text() &&
00330      app.exec() == application->execLineEdit->text())
00331     return;
00332 
00333   // Change the applnk file
00334   QString oldName = app.name();
00335   app.setName(application->nameLineEdit->text());
00336   app.setIcon(application->iconLineEdit->currentText());
00337   app.setComment(application->commentLineEdit->text());
00338   app.setExec(application->execLineEdit->text());
00339   if(!app.writeLink()){
00340     QMessageBox::critical(this, tr("Message"), "Can't save.", tr("Ok") );
00341     return;
00342   }
00343 
00344   // Update the gui icon and name
00345   item->setText(0,app.name());
00346   item->setPixmap(0,app.pixmap());
00347 
00348   // We have changed something.
00349   changed = true;
00350 
00351   // If we were dealing with a new folder or new application change
00352   // the file names.  Also change the item location in itemList
00353   if(oldName == NEW_FOLDER){
00354     QDir r;
00355     QString oldName = itemList[item];
00356     oldName = oldName.mid(0,oldName.length()-11);
00357     QString newName = oldName.mid(0,oldName.length()-9);
00358     newName = newName + "/" + app.name();
00359     r.rename(oldName, newName);
00360     itemList.remove(item);
00361     itemList.insert(item, newName + "/.directory" );
00362   }
00363   else if(oldName == NEW_APPLICATION){
00364     if(!item->parent())
00365       return;
00366     QString parentDir = itemList[item->parent()];
00367     QDir r;
00368     QString oldName = itemList[item];
00369     QString newName = oldName.mid(0, parentDir.length()-10);
00370     newName = newName + app.name() + APPLICATION_EXTENSION;
00371     r.rename(oldName, newName);
00372     itemList.remove(item);
00373     itemList.insert(item, newName);
00374   }
00375 }
00376 
00383 void TabManager::moveApplication(QListViewItem *item, QListViewItem *newGroup){
00384   // Can we even move it?
00385   if(!item || !item->parent() || newGroup->parent())
00386     return;
00387   if(item->parent() == newGroup)
00388     return;
00389 
00390   // Get the new folder, new file name,
00391   QString newFolder = itemList[newGroup];
00392   newFolder = newFolder.mid(0,newFolder.length()-11);
00393   int slash = newFolder.findRev('/', -1);
00394   QString folderName = newFolder.mid(slash+1, newFolder.length());
00395 
00396   QString desktopFile = itemList[item];
00397   slash = desktopFile.findRev('/', -1);
00398   desktopFile = desktopFile.mid(slash, desktopFile.length());
00399   newFolder = newFolder + desktopFile;
00400 
00401   // Move file
00402   QDir r;
00403   if(!r.rename(itemList[item], newFolder)){
00404     QMessageBox::critical(this, tr("Message"), "Can't move application.", tr("Ok") );
00405     return;
00406   }
00407   //odebug << (QString("moveApplication: ") + itemList[item]).latin1() << oendl;
00408   //odebug << (QString("moveApplication: ") + newFolder).latin1() << oendl;
00409 
00410   // Move in the gui
00411   item->parent()->takeItem(item);
00412   newGroup->insertItem(item);
00413   newGroup->setOpen(true);
00414 
00415   // Move file in the installer
00416   QString installedAppFile;
00417   if(findInstalledApplication(desktopFile, installedAppFile))
00418     swapInstalledLocation(installedAppFile, desktopFile, newFolder);
00419   else
00420     odebug << "moveApplication: No installed app found for dekstop file" << oendl;
00421 
00422   // Move application type
00423   AppLnk app(newFolder);
00424   app.setType(folderName);
00425   app.writeLink();
00426 
00427   // Move in our internal list
00428   itemList.remove(item);
00429   itemList.insert(item, newFolder);
00430 
00431   // We have changed something.
00432   changed = true;
00433 }
00434 
00443 bool TabManager::findInstalledApplication(QString desktopFile, QString &installedAppFile){
00444 
00445   QDir d;
00446   d.setPath(HOME_APP_INSTALL_DIR);
00447   d.setFilter( QDir::Files );
00448 
00449   const QFileInfoList *list = d.entryInfoList();
00450   QFileInfoListIterator it( *list );      // create list iterator
00451   QFileInfo *fi;                          // pointer for traversing
00452 
00453   while ( (fi=it.current()) ) { // for each file...
00454     QFile file(QString(HOME_APP_INSTALL_DIR) + "/" + fi->fileName());
00455     if ( file.open(IO_ReadOnly) ) { // file opened successfully
00456       QTextStream stream( &file ); // use a text stream
00457       QString line;
00458       while ( !stream.eof() ) { // until end of file...
00459         line = stream.readLine(); // line of text excluding '\n'
00460         if(line.contains(desktopFile)){
00461           installedAppFile = QString(HOME_APP_INSTALL_DIR) + "/" + fi->fileName();
00462           file.close();
00463           return true;
00464         }
00465       }
00466       file.close();
00467     }
00468     else
00469       odebug << (QString("findInstalledApplication: Can't open file") + HOME_APP_INSTALL_DIR + "/" + fi->fileName()).latin1() << oendl;
00470     ++it;  // goto next list element
00471   }
00472   return false;
00473 }
00474 
00481 void TabManager::swapInstalledLocation( QString installedAppFile, QString desktopFile, QString newLocation ){
00482   QFile file(installedAppFile);
00483   if ( !file.open(IO_ReadOnly) ){
00484     odebug << QString("swapInstalledLocation: Can't edit file: %1").arg(installedAppFile).latin1() << oendl;
00485     return;
00486   }
00487 
00488   QTextStream stream( &file );        // use a text stream
00489   QString allLines;
00490   while ( !stream.eof() ) {        // until end of file...
00491     QString line = stream.readLine();       // line of text excluding '\n'
00492     if(line.contains(desktopFile))
00493       allLines += newLocation;
00494     else
00495       allLines += line;
00496     allLines += '\n';
00497   }
00498   file.close();
00499 
00500   if ( !file.open(IO_ReadWrite) ){
00501     odebug << QString("swapInstalledLocation: Can't edit file: %1").arg(installedAppFile).latin1() << oendl;
00502     return;
00503   }
00504   QTextStream streamOut( &file );
00505   streamOut << allLines;
00506   file.close();
00507 }
00508 
00509 // tabmanager.cpp
00510 

Generated on Sat Nov 5 16:18:18 2005 for OPIE by  doxygen 1.4.2