source: src-qt4/pc-softwaremanager/mainUI.cpp @ 80faf74

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since 80faf74 was 80faf74, checked in by Ken Moore <ken@…>, 12 months ago

Add a new quick-launch button for starting up applications. This replaces using the icon as a button as well.
Also clean up some old commented out code in the process manager.

  • Property mode set to 100644
File size: 40.6 KB
Line 
1/***************************************************************************
2 *   Copyright (C) 2011 - iXsystems                                       *
3 *   kris@pcbsd.org  *
4 *   tim@pcbsd.org   *
5 *   ken@pcbsd.org   *
6 *                                                                         *
7 *   Permission is hereby granted, free of charge, to any person obtaining *
8 *   a copy of this software and associated documentation files (the       *
9 *   "Software"), to deal in the Software without restriction, including   *
10 *   without limitation the rights to use, copy, modify, merge, publish,   *
11 *   distribute, sublicense, and/or sell copies of the Software, and to    *
12 *   permit persons to whom the Software is furnished to do so, subject to *
13 *   the following conditions:                                             *
14 *                                                                         *
15 *   The above copyright notice and this permission notice shall be        *
16 *   included in all copies or substantial portions of the Software.       *
17 *                                                                         *
18 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
19 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
20 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
21 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
22 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
23 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
24 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
25 ***************************************************************************/
26#include "mainUI.h"
27#include "ui_mainUI.h"
28
29MainUI::MainUI(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainUI){
30  //initialization function
31  ui->setupUi(this); //load the Qt-Designer file
32  defaultIcon = ":/application.png";
33  statusLabel = new QLabel();
34  ui->statusbar->addWidget(statusLabel);
35}
36
37void MainUI::setWardenMode(QString dir, QString ip){
38  wardenDir = dir; wardenIP = ip;
39}
40
41void MainUI::ProgramInit()
42{ 
43   qDebug("Application starting...");
44   //Initial Checks
45     bool wardenMode = !wardenDir.isEmpty();
46     bool ok = Extras::checkUser(wardenMode);
47     if(!ok && wardenMode){  //Not root
48       QMessageBox::information( this, tr("Error!"), tr("The AppCafe must be run as root when in Warden compatibility mode") );
49       close();     
50     }else if( !ok && !wardenMode){
51       QMessageBox::information( this, tr("Error!"), tr("The AppCafe must be started with user permissions!")+"\n"+tr("The user must also be a part of the \"operator\" group") );
52       close();
53     }
54   
55   //Initialize the backend worker class
56   //qDebug() << "Initialize Backend";
57   PBI = new PBIBackend();
58     if(wardenMode){ PBI->setWardenMode(wardenDir, wardenIP); }
59     
60   //Initialize the Installed tab
61   //qDebug() << "Initialize Installed Tab";
62   initializeInstalledTab();
63   //Initialize the PBI Browser
64   //qDebug() << "Initialize Browser Tab";
65   initializeBrowserTab();
66   
67   //Now startup the backend
68   //qDebug() << "Startup Backend";
69     connect(PBI,SIGNAL(LocalPBIChanges()),this,SLOT(slotRefreshInstallTab()) );
70     connect(PBI,SIGNAL(PBIStatusChange(QString)),this,SLOT(slotPBIStatusUpdate(QString)) );
71     connect(PBI,SIGNAL(RepositoryInfoReady()),this,SLOT(slotEnableBrowser()) );
72     connect(PBI,SIGNAL(NoRepoAvailable()),this,SLOT(slotDisableBrowser()) );
73     connect(PBI,SIGNAL(SearchComplete(QStringList,QStringList)),this,SLOT(slotShowSearchResults(QStringList, QStringList)) );
74     connect(PBI,SIGNAL(SimilarFound(QStringList)),this,SLOT(slotShowSimilarApps(QStringList)) );
75     connect(PBI,SIGNAL(Error(QString,QString,QStringList)),this,SLOT(slotDisplayError(QString,QString,QStringList)) );
76     PBI->start();
77
78   //Make sure we start on the installed tab
79   ui->tabWidget->setCurrentWidget(ui->tab_installed);
80}
81
82void MainUI::slotSingleInstance(){
83  this->raise();
84  this->showNormal();
85  this->activateWindow();
86}
87
88void MainUI::closeEvent(QCloseEvent *event){
89  bool safe = PBI->safeToQuit();
90  if(!safe){
91    //Verify that they want to continue
92    QMessageBox::StandardButton button = QMessageBox::warning(this, tr("AppCafe Processes Running"), tr("The AppCafe currently has actions pending. Do you want to cancel all running processes and quit anyway?"), QMessageBox::Yes | QMessageBox::Cancel,QMessageBox::Cancel);
93    if(button == QMessageBox::Yes){ //close down
94      PBI->cancelActions( PBI->installedList() ); //close down safely
95    }else{
96      event->ignore();
97      return;
98    }
99  }
100  this->close();
101}
102// ========================
103// ===== MENU OPTIONS =====
104// ========================
105void MainUI::on_actionImport_PBI_List_triggered(){
106  QString file = QFileDialog::getOpenFileName( this, tr("Import PBI File List"), QDir::homePath(), tr("PBI List (*.pbilist)"));
107  if(file.isEmpty()){ return; } //action cancelled
108  bool ok = PBI->importPbiListFromFile(file);
109  if(!ok){ qDebug() << QMessageBox::warning(this,tr("Import Error"),tr("There was an error importing the PBI list")+"\n"+tr("Please make sure that the file has not been corrupted and try again")); }
110}
111
112void MainUI::on_actionExport_PBI_List_triggered(){
113  QString file = QFileDialog::getSaveFileName( this, tr("Export PBI File List"), QDir::homePath()+"/exportfile.pbilist", tr("PBI List (*.pbilist)"));
114  if(file.isEmpty()){ return; } //action cancelled
115  bool ok = PBI->exportPbiListToFile(file);
116  if(!ok){ qDebug() << QMessageBox::warning(this,tr("Export Error"),tr("There was an error exporting the PBI list")+"\n"+tr("Please make sure that you have the proper directory permissions and try again")); }
117}
118
119void MainUI::on_actionQuit_triggered(){
120  this->close();
121}
122
123void MainUI::on_actionAppCafe_Settings_triggered(){
124  PBI->openConfigurationDialog();
125}
126
127// =========================
128// ===== INSTALLED TAB =====
129// =========================
130void MainUI::initializeInstalledTab(){
131  //Setup the action menu for installed applications
132  actionMenu = new QMenu();
133    actionMenu->addAction( QIcon(":icons/view-refresh.png"), tr("Update"), this, SLOT(slotActionUpdate()) );
134    actionMenu->addSeparator();
135    QMenu *dmenu = actionMenu->addMenu( QIcon(":icons/xdg_desktop.png"), tr("Desktop Icons"));
136      dmenu->addAction( QIcon(":icons/add.png"),tr("Add"),this,SLOT(slotActionAddDesktop()) );
137      dmenu->addAction( QIcon(":icons/remove.png"),tr("Remove"),this,SLOT(slotActionRemoveDesktop()) );
138    QMenu *mmenu = actionMenu->addMenu( QIcon(":icons/xdg_menu.png"), tr("Menu Icons"));
139      mmenu->addAction( QIcon(":icons/add.png"),tr("Add"),this,SLOT(slotActionAddMenu()) );
140      mmenu->addAction( QIcon(":icons/remove.png"),tr("Remove"),this,SLOT(slotActionRemoveMenu()) ); 
141      mmenu->addAction( QIcon(":icons/add-root.png"),tr("Add (All Users)"),this,SLOT(slotActionAddMenuAll()) );
142    QMenu *pmenu = actionMenu->addMenu( QIcon(":icons/xdg_paths.png"), tr("Path Links"));
143      pmenu->addAction( QIcon(":icons/add.png"),tr("Add"),this,SLOT(slotActionAddPath()) );
144      pmenu->addAction( QIcon(":icons/remove.png"),tr("Remove"),this,SLOT(slotActionRemovePath()) ); 
145      pmenu->addAction( QIcon(":icons/add-root.png"),tr("Add (All Users)"),this,SLOT(slotActionAddPathAll()) );
146    QMenu *fmenu = actionMenu->addMenu( QIcon(":icons/xdg_mime.png"), tr("File Associations"));
147      fmenu->addAction( QIcon(":icons/add.png"),tr("Add"),this,SLOT(slotActionAddMime()) );
148      fmenu->addAction( QIcon(":icons/remove.png"),tr("Remove"),this,SLOT(slotActionRemoveMime()) ); 
149      fmenu->addAction( QIcon(":icons/add-root.png"),tr("Add (All Users)"),this,SLOT(slotActionAddMimeAll()) );
150    actionMenu->addSeparator();
151    actionMenu->addAction( QIcon(":icons/remove.png"), tr("Uninstall"), this, SLOT(slotActionRemove()) );
152    actionMenu->addSeparator();
153    actionMenu->addAction( QIcon(":icons/dialog-cancel.png"), tr("Cancel Actions"), this, SLOT(slotActionCancel()) );
154  //Setup the binary menu for installed applications
155  appBinMenu = new QMenu();
156  ui->tool_install_startApp->setMenu(appBinMenu);
157    connect(appBinMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotStartApp(QAction*)) );
158  //Now setup the action button
159  ui->tool_install_performaction->setMenu(actionMenu);
160  ui->tool_install_performaction->setPopupMode(QToolButton::InstantPopup);
161  //Now setup any defaults for the installed tab
162  ui->tool_install_gotobrowserpage->setEnabled(FALSE); //disable it until the browser is ready
163  ui->tree_install_apps->setIconSize(QSize(22,22));
164  slotRefreshInstallTab();
165}
166
167void MainUI::formatInstalledItemDisplay(QTreeWidgetItem *item){
168  //simplification function for filling the tree widget item with the appropriate information about the PBI
169  QString ID = item->whatsThis(0);
170  if(item->text(0).isEmpty()){  //new entry - get everything
171    QStringList vals; vals <<"name"<<"version"<<"status"; //same as column order
172    if(!vals.isEmpty()){
173      vals = PBI->PBIInfo(ID, vals);
174      for(int i=0; i<vals.length(); i++){ item->setText(i,vals[i]); }
175      QString icon = PBI->PBIInfo(ID, QStringList("icon")).join("");
176        //Load a default icon if none found
177      if(icon.isEmpty()){ icon = defaultIcon; }
178      item->setIcon(0,QIcon(icon) );
179      item->setCheckState(0,Qt::Unchecked);
180    }
181  }else{ // Just update the necesary info
182    QStringList vals; vals << "version" << "status";
183    vals = PBI->PBIInfo(ID,vals);
184    if(!vals.isEmpty()){ item->setText(1,vals[0]); item->setText(2,vals[1]); }
185  }
186}
187
188QStringList MainUI::getCheckedItems(){
189  //Return the pbiID's of all the checked items
190  QStringList output;
191  for(int i=0; i<ui->tree_install_apps->topLevelItemCount(); i++){
192    if(ui->tree_install_apps->topLevelItem(i)->checkState(0) == Qt::Checked){
193      output << ui->tree_install_apps->topLevelItem(i)->whatsThis(0);
194    }
195  }
196  //If no items checked, use the item that is selected instead
197  if(output.isEmpty()){
198    //Make sure that an item is selected
199    if(ui->tree_install_apps->topLevelItemCount() >0){
200      output << ui->tree_install_apps->currentItem()->whatsThis(0); 
201    }
202  }
203  return output;       
204}
205
206// === SLOTS ===
207void MainUI::slotRefreshInstallTab(){
208  //Update the list of installed PBI's w/o clearing the list (loses selections)
209   //Get the list we need (in order)
210  QStringList installList = PBI->installedList();
211  installList.sort(); //sort in ascending order
212   //Get the list we have now
213  QStringList cList;
214  for(int i=0; i<ui->tree_install_apps->topLevelItemCount(); i++){
215    cList << ui->tree_install_apps->topLevelItem(i)->whatsThis(0);
216  }
217  //Quick finish if no items installed
218  if(installList.isEmpty()){
219    ui->tree_install_apps->clear();
220  }else{
221    //Now make adjustments as necessary
222    for(int i=0; i<installList.length(); i++){
223      //Detemine what action should be done with this item location
224      int todo = 0; //0=insert new item, 1=update current item, 2=remove current item
225      if(i < cList.length()){
226        if(installList[i] == cList[i]){ todo=1; }
227        else if( installList.contains(cList[i]) && !cList.contains(installList[i]) ){ todo=0; } //new item to be inserted here
228        else if( !installList.contains(cList[i]) ){ todo=2; } //current item in this location should be removed
229      }
230      //Now perform the action on this location
231      if(todo==0){ 
232        //insert new item
233        QTreeWidgetItem *item = new QTreeWidgetItem; //create the item
234        item->setWhatsThis(0,installList[i]);
235        //Now format the display
236        formatInstalledItemDisplay(item);
237        //Now insert this item onto the list
238        ui->tree_install_apps->insertTopLevelItem(i,item);
239        cList.insert(i,installList[i]); //reflect this inclusion into the current list
240      }else if(todo==1){
241        //Update current item
242        formatInstalledItemDisplay( ui->tree_install_apps->topLevelItem(i) );
243      }else{
244        //Remove current item
245        ui->tree_install_apps->takeTopLevelItem(i);
246        cList.removeAt(i); //reflect the change to the current list
247        i--; //Re-check the item that should be in this location
248      }
249    }
250    //Now makesure that there are no extra items at the end
251    int il = installList.length();
252    while(il < cList.length()){
253      ui->tree_install_apps->takeTopLevelItem(il);
254      cList.removeAt(il); //reflect the change to the current list
255    }
256  } //end of empty list check
257 
258  //Make sure that there is an item selected
259  if(ui->tree_install_apps->topLevelItemCount() > 0 ){
260    if( ui->tree_install_apps->selectedItems().isEmpty() ){
261      ui->tree_install_apps->setCurrentItem( ui->tree_install_apps->topLevelItem(0) );
262    }
263    //Now re-size the columns to the minimum required width
264    for(int i=0; i<3; i++){
265      ui->tree_install_apps->resizeColumnToContents(i);
266    } 
267  }
268  slotUpdateSelectedPBI();; //Update the info boxes
269  slotDisplayStats();
270}
271
272void MainUI::slotPBIStatusUpdate(QString pbiID){
273  for(int i=0; i<ui->tree_install_apps->topLevelItemCount(); i++){
274    QString itemID = ui->tree_install_apps->topLevelItem(i)->whatsThis(0);
275    if(itemID == pbiID){
276      QString stat = PBI->PBIInfo(pbiID,QStringList()<<"status").join("");
277      ui->tree_install_apps->topLevelItem(i)->setText(2,stat);
278      // See if we need to update the download progress bar
279      QString appID;
280      if(ui->tree_install_apps->topLevelItemCount() > 0){
281         appID = ui->tree_install_apps->currentItem()->whatsThis(0);
282      }
283      if ( appID == pbiID ) {
284        slotUpdateSelectedPBI(true);; //only update status
285      }
286      return; // Found our match, we can return now
287    }
288  }
289}
290
291void MainUI::on_group_install_showinfo_toggled(bool show){
292  if(ui->tree_install_apps->topLevelItemCount() > 0){
293    ui->group_install_info->setVisible(show);
294  }else{
295    ui->group_install_info->setVisible(FALSE);   
296  }
297}
298
299void MainUI::on_tool_install_gotobrowserpage_clicked(){
300  //When you want to open up the browser page for an application
301  QString appID = Extras::nameToID( ui->tree_install_apps->currentItem()->text(0) );
302  slotGoToApp(appID);
303}
304
305void MainUI::on_tool_install_toggleall_clicked(){
306  //Determine if the current item is checked or unchecked
307  bool checkall = (ui->tree_install_apps->currentItem()->checkState(0) == Qt::Unchecked);
308  for(int i=0; i<ui->tree_install_apps->topLevelItemCount(); i++){
309    if(checkall){ ui->tree_install_apps->topLevelItem(i)->setCheckState(0,Qt::Checked); }
310    else{ui->tree_install_apps->topLevelItem(i)->setCheckState(0,Qt::Unchecked); }
311  }
312}
313
314void MainUI::on_tree_install_apps_itemSelectionChanged(){
315  //When an installed PBI is clicked on
316  slotUpdateSelectedPBI();
317}
318
319void MainUI::on_check_install_autoupdate_clicked(){
320  //Get the current item
321  QString appID;
322  if(ui->tree_install_apps->topLevelItemCount() > 0){
323    appID = ui->tree_install_apps->currentItem()->whatsThis(0);
324  }
325  if(appID.isEmpty()){return;}
326  //Now determine the current state of the checkbox
327  bool enabled = ui->check_install_autoupdate->isChecked();
328  //Now have the backend make the change
329  PBI->enableAutoUpdate(appID, enabled);
330}
331
332void MainUI::on_tool_install_update_clicked(){
333  //Get the current item
334  QString appID;
335  if(ui->tree_install_apps->topLevelItemCount() > 0){
336    appID = ui->tree_install_apps->currentItem()->whatsThis(0);
337  }
338  if(appID.isEmpty()){return;}
339  PBI->upgradePBI(QStringList() << appID);
340}
341
342void MainUI::on_tool_install_remove_clicked(){
343  //Get the current item
344  QString appID;
345  if(ui->tree_install_apps->topLevelItemCount() > 0){
346    appID = ui->tree_install_apps->currentItem()->whatsThis(0);
347  }
348  if(appID.isEmpty()){return;}
349  //Verify removal
350  if( QMessageBox::Yes == QMessageBox::question(this,tr("Verify PBI Removal"), tr("Are you sure you wish to remove this application?")+"\n\n"+appID,QMessageBox::Yes | QMessageBox::Cancel,QMessageBox::Cancel) ){
351    PBI->removePBI(QStringList() << appID);
352  }
353}
354
355void MainUI::on_tool_install_cancel_clicked(){
356  //Get the current item
357  QString appID;
358  if(ui->tree_install_apps->topLevelItemCount() > 0){
359    appID = ui->tree_install_apps->currentItem()->whatsThis(0);
360  }
361  if(appID.isEmpty()){return;}
362  PBI->cancelActions(QStringList() << appID);
363 
364}
365
366// === SELECTED PBI ACTIONS ===
367void MainUI::slotActionAddDesktop(){
368  QStringList items = getCheckedItems();
369  PBI->addDesktopIcons(items,FALSE); //only for current user
370}
371
372void MainUI::slotActionRemoveDesktop(){
373  QStringList items = getCheckedItems();
374  PBI->rmDesktopIcons(items,FALSE);  //Only for current user
375}
376
377void MainUI::slotActionAddPath(){
378  QStringList items = getCheckedItems();
379  PBI->addPaths(items,FALSE);  //Only for current user 
380}
381
382void MainUI::slotActionRemovePath(){
383  QStringList items = getCheckedItems();
384  PBI->rmPaths(items,FALSE);  //Only for current user
385}
386
387void MainUI::slotActionAddPathAll(){
388  QStringList items = getCheckedItems();
389  PBI->addPaths(items,TRUE);  //For all users (root permissions required)
390}
391
392void MainUI::slotActionAddMenu(){
393  QStringList items = getCheckedItems();
394  PBI->addMenuIcons(items,FALSE);  //Only for current user
395}
396
397void MainUI::slotActionRemoveMenu(){
398  QStringList items = getCheckedItems();
399  PBI->rmMenuIcons(items,FALSE);  //Only for current user       
400}
401
402void MainUI::slotActionAddMenuAll(){
403  QStringList items = getCheckedItems();
404  PBI->addMenuIcons(items,TRUE);  //For all users (root permissions required)   
405}
406
407void MainUI::slotActionAddMime(){
408  QStringList items = getCheckedItems();
409  PBI->addMimeTypes(items,FALSE);  //Only for current user     
410}
411
412void MainUI::slotActionRemoveMime(){
413  QStringList items = getCheckedItems();
414  PBI->rmMimeTypes(items,FALSE);  //Only for current user       
415}
416
417void MainUI::slotActionAddMimeAll(){
418    QStringList items = getCheckedItems();
419  PBI->addMimeTypes(items,TRUE);  //For all users (root permissions required)   
420}
421
422void MainUI::slotActionUpdate(){
423  QStringList checkedID = getCheckedItems();
424  if(!checkedID.isEmpty()){
425    PBI->upgradePBI(checkedID); 
426  }
427}
428
429void MainUI::slotActionRemove(){
430  QStringList checkedID = getCheckedItems();
431  if(!checkedID.isEmpty()){
432    //Verify that the user really wants to remove these apps
433    if( QMessageBox::Yes == QMessageBox::question(this,tr("Verify PBI Removal"), tr("Are you sure you wish to remove these applications?")+"\n\n"+checkedID.join("\n"),QMessageBox::Yes | QMessageBox::Cancel,QMessageBox::Cancel) ){
434      PBI->removePBI(checkedID);
435    }
436  }
437}
438
439void MainUI::slotActionCancel(){
440  QStringList checkedID = getCheckedItems();
441  if(!checkedID.isEmpty()){
442    PBI->cancelActions(checkedID); 
443  }
444}
445
446void MainUI::slotStartApp(QAction* act){
447  qDebug() << "Starting external application:" << act->text();
448  //Get the command from the action
449  QString desktopfile = act->whatsThis();
450  QString cmd = "xdg-open "+desktopfile;
451  //Startup the command externally
452  QProcess::startDetached(cmd);
453}
454
455void MainUI::slotUpdateSelectedPBI(bool statusonly){
456  QString appID;
457  if(ui->tree_install_apps->topLevelItemCount() > 0){
458    appID = ui->tree_install_apps->currentItem()->whatsThis(0);
459  }
460  if(appID.isEmpty()){
461    ui->group_install_info->setVisible(FALSE);
462    return;
463  }else{
464    ui->group_install_info->setVisible( ui->group_install_showinfo->isChecked() );       
465  }
466  if(!statusonly){
467    //Get the PBI info for that item
468    QStringList vals; 
469    vals << "name" << "icon" << "author" << "website" << "version" << "license";
470    QStringList bools;
471    bools << "autoupdate" << "hasdesktopicons" << "hasmenuicons" << "requiresroot";
472    vals = PBI->PBIInfo(appID,vals);
473    bools = PBI->PBIInfo(appID,bools);
474    //Make sure the info lists are not empty
475    if(vals.isEmpty() || bools.isEmpty()){
476      ui->group_install_info->setVisible(FALSE);
477      return; 
478    }
479    //Load a default icon if none found
480    if(vals[1].isEmpty()){ vals[1] = defaultIcon; }
481    //Now set the info on the UI
482    bool desktopSC = (bools[1] == "true"); // XDG desktop entries available
483    bool menuSC= (bools[2] == "true");   // XDG menu entries available
484    bool autoupdate = (bools[0] == "true"); //auto-update enabled
485    bool rootonly = (bools[3] == "true"); //requires root
486    //Create the shortcuts string
487    QString shortcuts;
488      if(desktopSC && menuSC){ shortcuts = tr("Desktop/Menu"); }
489      else if(desktopSC){ shortcuts = tr("Desktop"); }
490      else if(menuSC){ shortcuts = tr("Menu"); }
491      else{ shortcuts = tr("None"); }
492    //Now display that info on the UI
493    ui->label_install_app->setText(vals[0]);
494    ui->label_install_icon->setPixmap( QPixmap(vals[1]) );
495    if(vals[3].isEmpty()){ 
496      ui->label_install_author->setText(vals[2]); 
497      ui->label_install_author->setToolTip("");
498    }else{ 
499      ui->label_install_author->setText("<a href="+vals[3]+">"+vals[2]+"</a>"); 
500      ui->label_install_author->setToolTip(vals[3]); //show website URL as tooltip
501    }
502    ui->label_install_license->setText(vals[5]);
503    ui->label_install_version->setText(vals[4]);
504    ui->label_install_shortcuts->setText(shortcuts);
505    ui->check_install_autoupdate->setChecked(autoupdate);
506 
507    //Adjust the quick action buttons as necessary
508    if( PBI->isWorking(appID) ){
509      //Actions pending/working only show cancel button
510      ui->tool_install_cancel->setVisible(TRUE);
511      ui->tool_install_remove->setVisible(FALSE);
512      ui->tool_install_update->setVisible(FALSE);
513      ui->tool_install_startApp->setVisible(FALSE);
514    }else{
515      //Nothing pending
516      ui->tool_install_cancel->setVisible(FALSE);
517      if( !PBI->isInstalled(appID).isEmpty() ){ 
518        //Remove Button
519        ui->tool_install_remove->setVisible(TRUE);
520        if(rootonly){ ui->tool_install_remove->setIcon(QIcon(":icons/remove-root.png")); }
521        else{ ui->tool_install_remove->setIcon(QIcon(":icons/remove.png")); }
522        //Update
523        if(PBI->upgradeAvailable(appID).isEmpty()){ ui->tool_install_update->setVisible(FALSE); }
524        else{
525          ui->tool_install_update->setVisible(TRUE); 
526          if(rootonly){ ui->tool_install_update->setIcon(QIcon(":icons/app_upgrade_small-root.png")); }
527          else{ ui->tool_install_update->setIcon(QIcon(":icons/app_upgrade_small.png")); }
528        }
529        //Start Application binaries
530        QStringList bins = PBI->pbiBinList(appID);
531        appBinMenu->clear();
532        for(int i=0; i<bins.length(); i++){
533          QAction *act = new QAction(this);
534            act->setText(bins[i].section("::::",0,0)); //set name
535            act->setWhatsThis(bins[i].section("::::",1,10)); //set command string
536          appBinMenu->addAction(act);
537        }
538        if(appBinMenu->isEmpty()){ ui->tool_install_startApp->setVisible(FALSE); }
539        else{ ui->tool_install_startApp->setVisible(TRUE); }
540      }else{ 
541        ui->tool_install_remove->setVisible(FALSE); 
542        ui->tool_install_update->setVisible(FALSE); 
543        ui->tool_install_startApp->setVisible(FALSE);
544      }   
545    }
546  }
547  //Update the current status indicators
548  QString stat = PBI->currentAppStatus(appID,true); //get the raw status
549  if(stat.isEmpty() || stat == "DLSTART"  || stat == "DLDONE"){
550    //Not currently running - hide the display indicators
551    ui->group_install_appStat->setVisible(FALSE);
552  }else if(stat.startsWith("DLSTAT::")){
553    //Currently downloading - show download status indicators
554    QString percent = stat.section("::",1,1);
555    QString total = stat.section("::",2,2);
556    QString speed = stat.section("::",3,3);
557    ui->group_install_appStat->setVisible(TRUE);
558      ui->progress_install_DL->setVisible(TRUE);
559    if(total == "??"){ ui->label_install_status->setText( tr("Downloading file:") ); }
560    else{ ui->label_install_status->setText( QString(tr("Downloading %1 file:")).arg(total) ); }
561    if(percent == "??"){
562      ui->progress_install_DL->setMinimum(0); ui->progress_install_DL->setMaximum(0);
563    }else{
564      ui->progress_install_DL->setMinimum(0); ui->progress_install_DL->setMaximum(100);
565      ui->progress_install_DL->setValue( int(percent.toFloat()) );
566    }
567    if(speed == "??"){ ui->label_install_DL->setVisible(FALSE); }
568    else{
569      ui->label_install_DL->setVisible(TRUE);
570      ui->label_install_DL->setText(speed);
571    }
572  }else{
573    //Currently installing/removing/updating - show last message from process
574    ui->label_install_status->setText(stat);
575    ui->group_install_appStat->setVisible(TRUE);
576      ui->progress_install_DL->setVisible(FALSE);
577      ui->label_install_DL->setVisible(FALSE);
578  }
579}
580
581// ==========================
582// ====== BROWSER TAB =======
583// ==========================
584void MainUI::initializeBrowserTab(){
585  ui->tab_browse->setEnabled(FALSE);
586  //Always make sure that the browser starts off on the "home" page
587  ui->stacked_browser->setCurrentWidget(ui->page_home);
588  //With invisible shortcut buttons
589  ui->tool_browse_cat->setVisible(FALSE);
590  ui->tool_browse_app->setVisible(FALSE);
591  //Clear any items left over from the designer form
592  clearScrollArea(ui->scroll_br_home_newapps);
593  clearScrollArea(ui->scroll_br_home_cats);
594  //Search functionality
595  searchTimer = new QTimer();
596    searchTimer->setSingleShot(TRUE);
597    searchTimer->setInterval(300); // 0.3 sec wait before a search
598    connect(searchTimer,SIGNAL(timeout()),this,SLOT(slotGoToSearch()) );
599  connect(ui->tool_browse_search,SIGNAL(clicked()),this,SLOT(slotGoToSearch()) );
600  connect(ui->line_browse_searchbar,SIGNAL(returnPressed()),this,SLOT(slotGoToSearch()) );
601}
602
603// === SLOTS ===
604void MainUI::slotDisableBrowser(bool shownotification){
605  if(shownotification){ qDebug() << "No Repo Available: De-activating the Browser"; }
606  ui->tabWidget->setCurrentWidget(ui->tab_installed);
607  ui->tool_install_gotobrowserpage->setEnabled(FALSE);
608  ui->tab_browse->setEnabled(FALSE);
609  slotDisplayStats();
610}
611
612void MainUI::slotEnableBrowser(){
613  qDebug() << "Repo Ready: - generating browser home page";
614  //Now create the browser home page
615  slotUpdateBrowserHome();
616  //And allow the user to go there
617  ui->tool_install_gotobrowserpage->setEnabled(TRUE);
618  ui->tab_browse->setEnabled(TRUE);
619  slotDisplayStats();
620}
621
622void MainUI::slotUpdateBrowserHome(){
623  //Load the Categories
624  QStringList cats = PBI->browserCategories();
625  clearScrollArea(ui->scroll_br_home_cats);
626  QVBoxLayout *catlayout = new QVBoxLayout;
627  QStringList info; info << "name" << "description" << "icon";
628  cats.sort(); //sort them alphabetically
629  for(int i=0; i<cats.length(); i++){
630    QStringList data = PBI->CatInfo(cats[i],info);
631    if(!data.isEmpty()){
632      LargeItemWidget *item = new LargeItemWidget(cats[i],data[0],data[1],data[2]);
633      connect(item,SIGNAL(appClicked(QString)),this,SLOT(slotGoToCategory(QString)) );
634      catlayout->addWidget(item);
635    }
636  }
637  catlayout->addStretch(); //add a spacer to the end
638  ui->scroll_br_home_cats->widget()->setLayout(catlayout);
639  //Load the newest applications
640  clearScrollArea(ui->scroll_br_home_newapps);
641  QHBoxLayout *newapplayout = new QHBoxLayout;
642  QStringList newapps = PBI->getRecentApps();
643  for(int i=0; i<newapps.length(); i++){
644    QStringList appdata = PBI->AppInfo(newapps[i],QStringList() << "name" << "icon" << "latestversion");
645    if(!appdata.isEmpty()){
646      SmallItemWidget *item = new SmallItemWidget(newapps[i],appdata[0],appdata[1],appdata[2]);
647      connect(item,SIGNAL(appClicked(QString)),this,SLOT(slotGoToApp(QString)) );
648      newapplayout->addWidget(item);
649    }
650  }
651  newapplayout->addStretch(); //add a spacer to the end
652  newapplayout->setContentsMargins(0,0,0,0);
653  newapplayout->setSpacing(0);
654  ui->scroll_br_home_newapps->widget()->setLayout(newapplayout);
655  //Make sure that the newapps scrollarea is the proper fit vertically (no vertical scrolling)
656  ui->scroll_br_home_newapps->setMinimumHeight(ui->scroll_br_home_newapps->widget()->minimumSizeHint().height()+ui->scroll_br_home_newapps->horizontalScrollBar()->height());
657 
658  //Make sure the new apps area is invisible if no items available
659  if(newapps.isEmpty()){ ui->group_br_home_newapps->setVisible(FALSE); }
660  else{ ui->group_br_home_newapps->setVisible(TRUE); }
661  //make sure the home page is visible in the browser (slotGoToHome without changing tabs)
662  ui->stacked_browser->setCurrentWidget(ui->page_home); 
663  //Make sure the shortcut buttons are disabled
664  ui->tool_browse_cat->setVisible(FALSE);
665  ui->tool_browse_app->setVisible(FALSE); 
666 
667}
668
669void MainUI::slotGoToHome(){
670  ui->tabWidget->setCurrentWidget(ui->tab_browse);
671  ui->stacked_browser->setCurrentWidget(ui->page_home); 
672  //Make sure the shortcut buttons are disabled
673  ui->tool_browse_cat->setVisible(FALSE);
674  ui->tool_browse_app->setVisible(FALSE);
675}
676
677void MainUI::slotGoToCategory(QString cat){
678  qDebug() << "Show Category:" << cat;
679  //Get the apps in this category
680  QStringList apps = PBI->browserApps(cat);
681  if(apps.isEmpty()){
682    qDebug() << "Invalid category:" << cat;
683    return;
684  }
685  clearScrollArea(ui->scroll_br_cat_apps);
686  apps.sort();
687  QVBoxLayout *applayout = new QVBoxLayout;
688  QStringList info; info << "name" << "description" << "icon";
689  for(int i=0; i<apps.length(); i++){
690    QStringList data = PBI->AppInfo(apps[i],info);
691    if(!data.isEmpty()){
692      //Trim the description if necessary
693      if(data[1].size()>100){ data[1] = data[1].left(100).append("..."); }
694      LargeItemWidget *item = new LargeItemWidget(apps[i],data[0],data[1],data[2]);
695      connect(item,SIGNAL(appClicked(QString)),this,SLOT(slotGoToApp(QString)) );
696      applayout->addWidget(item); 
697    }
698  }
699  applayout->addStretch();
700  ui->scroll_br_cat_apps->widget()->setLayout(applayout);
701  //Now enable/disable the shortcut buttons
702  ui->tool_browse_app->setVisible(FALSE);
703  ui->tool_browse_cat->setVisible(TRUE);
704    QStringList catinfo = PBI->CatInfo(cat,QStringList() << "name" << "icon");
705    ui->tool_browse_cat->setText(catinfo[0]);
706    if(catinfo[1].isEmpty()){ catinfo[1] = defaultIcon; }
707    ui->tool_browse_cat->setIcon(QIcon(catinfo[1]));
708  ui->tabWidget->setCurrentWidget(ui->tab_browse);
709  ui->stacked_browser->setCurrentWidget(ui->page_cat);
710  //Now save that this category is currently displayed
711  cCat = cat;
712}
713
714void MainUI::slotGoToApp(QString appID){
715  qDebug() << "Show App:" << appID;
716  //Get the general application info
717  QStringList data; 
718  data << "name" << "icon" << "author" << "website" << "license" << "type" << "description" \
719       << "category" << "requiresroot" << "latestversion" << "latestarch" <<"latestsize" \
720       << "backupversion" << "backuparch" << "backupsize";
721  data = PBI->AppInfo(appID,data);
722  if(data.isEmpty()){
723    qDebug() << "Invalid App:" << appID;
724    return;
725  }
726  //Start the search for similar apps
727  PBI->searchSimilar = appID;
728  ui->group_bapp_similar->setVisible(FALSE);
729  QTimer::singleShot(0,PBI,SLOT(startSimilarSearch()));
730  //Now Check icon
731  if(data[1].isEmpty()){ data[1] = defaultIcon; }
732  //Now fill the UI with the data
733  ui->label_bapp_name->setText(data[0]);
734  ui->label_bapp_icon->setPixmap(QPixmap(data[1]));
735  if(data[3].isEmpty()){ ui->label_bapp_authorweb->setText(data[2]); }
736  else{ ui->label_bapp_authorweb->setText("<a href="+data[3]+">"+data[2]+"</a>"); }
737  ui->label_bapp_authorweb->setToolTip(data[3]);
738  ui->label_bapp_license->setText(data[4]);
739  ui->label_bapp_type->setText(data[5]);
740  ui->text_bapp_description->setText(data[6]);
741  //Now determine the appropriate version info
742  QString pbiID = PBI->isInstalled(appID); //get pbiID
743  QString cVer;
744  if(!pbiID.isEmpty()){ cVer = PBI->PBIInfo(pbiID,QStringList("version")).join(""); }
745  bool useLatest=FALSE;
746  bool nobackup = data[12].isEmpty();
747  if(cVer.isEmpty()){ useLatest=TRUE; } //not currently installed
748  else if(cVer != data[9]){ useLatest=TRUE;} //not the latest version
749  //Now put the proper version info on the UI
750  if(useLatest || nobackup){
751    ui->label_bapp_version->setText(data[9]);
752    ui->label_bapp_arch->setText(data[10]);
753    if(data[11].isEmpty()){ ui->label_bapp_size->setText(tr("Unknown")); }
754    else{ ui->label_bapp_size->setText( Extras::sizeKToDisplay(data[11]) ); }
755  }else{
756    ui->label_bapp_version->setText(data[12]);
757    ui->label_bapp_arch->setText(data[13]);
758    if(data[14].isEmpty()){ ui->label_bapp_size->setText(tr("Unknown")); }
759    else{ ui->label_bapp_size->setText( Extras::sizeKToDisplay(data[14]) ); }
760  }
761  //Now update the download button appropriately
762  QString ico;
763  QString working = PBI->currentAppStatus(appID);
764  if(!working.isEmpty()){ //app currently pending or actually doing something
765    ui->tool_bapp_download->setText(working);
766    ui->tool_bapp_download->setIcon(QIcon(":icons/working.png"));
767    ui->tool_bapp_download->setEnabled(FALSE);
768  }else if(useLatest && cVer.isEmpty()){ //new installation
769    ui->tool_bapp_download->setText(tr("Install Now!"));
770    ico = ":icons/app_download.png";
771    ui->tool_bapp_download->setEnabled(TRUE);
772  }else if(useLatest){ //Update available
773    ui->tool_bapp_download->setText(tr("Update"));
774    ico = ":icons/app_upgrade.png";
775    ui->tool_bapp_download->setEnabled(TRUE);
776  }else if(!nobackup){  //Downgrade available
777    ui->tool_bapp_download->setText(tr("Downgrade"));
778    ico = ":icons/app_downgrade.png";
779    ui->tool_bapp_download->setEnabled(TRUE);
780  }else{ //already installed (no downgrade available)
781    ui->tool_bapp_download->setText(tr("Installed"));
782    ui->tool_bapp_download->setIcon(QIcon(":icons/dialog-ok.png"));
783    ui->tool_bapp_download->setEnabled(FALSE);
784  }
785  //Now set the icon appropriately if it requires root permissions
786  if(!ico.isEmpty()){
787    if(data[8]=="true"){ //requires root permissions to install
788      ico.replace(".png","-root.png");
789    }
790    ui->tool_bapp_download->setIcon(QIcon(ico));
791  }
792  ui->tool_bapp_download->setWhatsThis(appID); //set for slot
793  //Now enable/disable the shortcut buttons
794  ui->tool_browse_app->setVisible(TRUE);
795    ui->tool_browse_app->setText(data[0]);
796    ui->tool_browse_app->setIcon(QIcon(data[1]));
797  QStringList catinfo = PBI->CatInfo(Extras::nameToID(data[7]),QStringList() << "name" << "icon");
798  if(!catinfo.isEmpty()){
799    ui->tool_browse_cat->setVisible(TRUE);
800    ui->tool_browse_cat->setText(catinfo[0]);
801    if(catinfo[1].isEmpty()){ catinfo[1] = defaultIcon; }
802    ui->tool_browse_cat->setIcon(QIcon(catinfo[1]));
803  }
804  ui->tabWidget->setCurrentWidget(ui->tab_browse);
805  ui->stacked_browser->setCurrentWidget(ui->page_app);
806       
807}
808
809void MainUI::slotGoToSearch(){
810  searchTimer->stop(); //just in case "return" was pressed to start the search
811  QString search = ui->line_browse_searchbar->text();
812  if(search.isEmpty()){ return; }
813  PBI->searchTerm = search;
814  QTimer::singleShot(1,PBI,SLOT(startAppSearch()));
815  ui->label_bsearch_info->setText( tr("Searching the application database. Please Wait....") );
816    ui->label_bsearch_info->setVisible(TRUE);
817    ui->group_bsearch_best->setVisible(FALSE);
818    ui->group_bsearch_other->setVisible(FALSE);
819 
820}
821       
822void MainUI::slotShowSimilarApps(QStringList apps){
823  qDebug() << " - Similar applications:" << apps;
824  if(apps.isEmpty()){ ui->group_bapp_similar->setVisible(FALSE); }
825  else{
826    clearScrollArea(ui->scroll_bapp_similar);
827    QHBoxLayout *layout = new QHBoxLayout;
828    for(int i=0; i<apps.length(); i++){
829      QStringList appdata = PBI->AppInfo(apps[i],QStringList() << "name" << "icon");
830      if(!appdata.isEmpty()){
831        SmallItemWidget *item = new SmallItemWidget(apps[i],appdata[0],appdata[1],"");
832        connect(item,SIGNAL(appClicked(QString)),this,SLOT(slotGoToApp(QString)) );
833        layout->addWidget(item);
834      }
835    }
836    layout->addStretch(); //add a spacer to the end
837    layout->setContentsMargins(1,1,1,1);
838    ui->scroll_bapp_similar->widget()->setLayout(layout);
839    //Make sure that the similar scrollarea is the proper fit vertically (no vertical scrolling)
840    ui->scroll_bapp_similar->setMinimumHeight(ui->scroll_bapp_similar->widget()->minimumSizeHint().height()+ui->scroll_bapp_similar->horizontalScrollBar()->height());
841    //Now make the group visible as appropriate
842    ui->group_bapp_similar->setVisible(TRUE);
843    if(ui->group_bapp_similar->isChecked()){ ui->scroll_bapp_similar->setVisible(TRUE); }
844    else{ ui->scroll_bapp_similar->setVisible(FALSE); }
845    //Now make sure the app page has the proper layout dimensions
846    ui->page_app->updateGeometry();
847  }
848}
849
850void MainUI::slotShowSearchResults(QStringList best, QStringList rest){
851  //Now display the search results
852  if(best.isEmpty()){
853    ui->label_bsearch_info->setText( QString(tr("No Search Results Found for the term: %1")).arg(ui->line_browse_searchbar->text()) );
854    ui->label_bsearch_info->setVisible(TRUE);
855    ui->group_bsearch_best->setVisible(FALSE);
856    ui->group_bsearch_other->setVisible(FALSE);
857  }else{
858    ui->label_bsearch_info->setVisible(FALSE);
859    //Now fill the "best" section
860    clearScrollArea(ui->scroll_bsearch_best);
861    QVBoxLayout *layout = new QVBoxLayout;
862    QStringList info; info << "name" << "description" << "icon";
863    for(int i=0; i<best.length(); i++){
864      QStringList data = PBI->AppInfo(best[i],info);
865      if(!data.isEmpty()){
866        //Trim the description if necessary
867        if(data[1].size()>100){ data[1] = data[1].left(100).append("..."); }
868        LargeItemWidget *item = new LargeItemWidget(best[i],data[0],data[1],data[2]);
869        connect(item,SIGNAL(appClicked(QString)),this,SLOT(slotGoToApp(QString)) );
870        layout->addWidget(item); 
871      }
872    }
873    layout->addStretch();
874    ui->scroll_bsearch_best->widget()->setLayout(layout);
875    ui->group_bsearch_best->setVisible(TRUE);
876   
877    //Now fill the other results
878    if(rest.isEmpty()){ ui->group_bsearch_other->setVisible(FALSE); }
879    else{
880      ui->group_bsearch_other->setVisible(TRUE);
881      clearScrollArea(ui->scroll_bsearch_other);
882      QVBoxLayout *layout2 = new QVBoxLayout;
883      for(int i=0; i<rest.length(); i++){
884        QStringList data = PBI->AppInfo(rest[i],info);
885        if(!data.isEmpty()){
886          //Trim the description if necessary
887          if(data[1].size()>100){ data[1] = data[1].left(100).append("..."); }
888          LargeItemWidget *item = new LargeItemWidget(rest[i],data[0],data[1],data[2]);
889          connect(item,SIGNAL(appClicked(QString)),this,SLOT(slotGoToApp(QString)) );
890          layout2->addWidget(item); 
891        }
892      }
893      layout2->addStretch();
894      ui->scroll_bsearch_other->widget()->setLayout(layout2);
895    }
896  }
897       
898  //need to make sure the search bar still has keyboard focus (just in case)
899  ui->tabWidget->setCurrentWidget(ui->tab_browse);
900  ui->stacked_browser->setCurrentWidget(ui->page_search);
901 
902}
903
904void MainUI::on_tabWidget_currentChanged(){
905  if(ui->tabWidget->currentWidget() == ui->tab_browse){
906    //Refresh the app page if that is the one currently showing
907    if(ui->stacked_browser->currentWidget() == ui->page_app){ on_tool_browse_app_clicked(); }     
908  }
909}
910
911void MainUI::on_tool_browse_home_clicked(){
912  slotGoToHome();
913}
914
915void MainUI::on_tool_browse_cat_clicked(){
916  QString cat = Extras::nameToID(ui->tool_browse_cat->text());
917  if(cCat == cat){ //already loaded - just show it (prevents resetting scroll positions)
918    ui->stacked_browser->setCurrentWidget(ui->page_cat);
919  }else{ //load and show the category
920    slotGoToCategory(cat);
921  }
922}
923
924void MainUI::on_tool_browse_app_clicked(){
925  QString appID = Extras::nameToID(ui->tool_browse_app->text());
926  slotGoToApp(appID);
927}
928
929void MainUI::on_line_browse_searchbar_textChanged(){
930  //Connect this to a singleshot timer, so the search functionality is only
931  //  run once after a short wait rather than every time a new character is typed
932 
933  //Live search only after 3 characters have been typed
934  if(ui->line_browse_searchbar->text().length() > 2){
935    searchTimer->start();
936  }else{
937    searchTimer->stop();         
938  }
939}
940
941void MainUI::on_tool_bapp_download_clicked(){
942  QString appID = ui->tool_bapp_download->whatsThis();
943  //Verify the app installation
944  if( QMessageBox::Yes != QMessageBox::question(this,tr("Verify Installation"), tr("Are you sure you want to install this application?")+"\n\n"+appID,QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes)){
945    return;
946  }
947  PBI->installApp(QStringList() << appID);
948  ui->tool_bapp_download->setEnabled(FALSE); //make sure it cannot be clicked more than once before page refresh
949  //Now show the Installed tab
950  ui->tabWidget->setCurrentWidget(ui->tab_installed);
951}
952
953void MainUI::on_group_br_home_newapps_toggled(bool show){
954  ui->scroll_br_home_newapps->setVisible(show);
955}
956
957void MainUI::on_group_bapp_similar_toggled(bool show){
958  ui->scroll_bapp_similar->setVisible(show);
959}
960
961// ================================
962// ==========  OTHER ==============
963// ================================
964void MainUI::clearScrollArea(QScrollArea* area){
965  QWidget *wgt = area->takeWidget();
966  delete wgt; //delete the widget and all children
967  area->setWidget( new QWidget() ); //create a new widget in the scroll area
968  area->widget()->setContentsMargins(0,0,0,0);
969}
970
971void MainUI::slotDisplayError(QString title,QString message,QStringList log){
972  QMessageBox *dlg = new QMessageBox(this);
973    dlg->setWindowTitle(title);
974    dlg->setText(message);
975    dlg->setDetailedText(log.join("\n"));
976  dlg->exec();
977}
978
979void MainUI::slotDisplayStats(){
980  int avail = PBI->numAvailable;
981  int installed = PBI->numInstalled;
982  QString text;
983  if(avail == -1){
984    text = QString(tr("Installed: %1")).arg(QString::number(installed)); 
985  }else{
986    text = QString(tr("Installed: %1")+"\t"+tr("Available: %2")).arg(QString::number(installed), QString::number(avail)); 
987  }
988  //Get the number of installed/available applications and display it
989  statusLabel->setText(text);   
990}
Note: See TracBrowser for help on using the repository browser.