source: src-qt4/pc-softwaremanager/mainUI.cpp @ 867747a

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

Clean up the Appcafe error dialog. Just use the built in Qmessagebox functionality for displaying detailed (but optional) info instead of the custom class I wrote before.

  • Property mode set to 100644
File size: 39.1 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    //Now setup the action button
155    ui->tool_install_performaction->setMenu(actionMenu);
156    ui->tool_install_performaction->setPopupMode(QToolButton::InstantPopup);
157    //Now setup any defaults for the installed tab
158    ui->tool_install_gotobrowserpage->setEnabled(FALSE); //disable it until the browser is ready
159    ui->tree_install_apps->setIconSize(QSize(22,22));
160    slotRefreshInstallTab();
161}
162
163void MainUI::formatInstalledItemDisplay(QTreeWidgetItem *item){
164  //simplification function for filling the tree widget item with the appropriate information about the PBI
165  QString ID = item->whatsThis(0);
166  if(item->text(0).isEmpty()){  //new entry - get everything
167    QStringList vals; vals <<"name"<<"version"<<"status"; //same as column order
168    if(!vals.isEmpty()){
169      vals = PBI->PBIInfo(ID, vals);
170      for(int i=0; i<vals.length(); i++){ item->setText(i,vals[i]); }
171      QString icon = PBI->PBIInfo(ID, QStringList("icon")).join("");
172        //Load a default icon if none found
173      if(icon.isEmpty()){ icon = defaultIcon; }
174      item->setIcon(0,QIcon(icon) );
175      item->setCheckState(0,Qt::Unchecked);
176    }
177  }else{ // Just update the necesary info
178    QStringList vals; vals << "version" << "status";
179    vals = PBI->PBIInfo(ID,vals);
180    if(!vals.isEmpty()){ item->setText(1,vals[0]); item->setText(2,vals[1]); }
181  }
182}
183
184QStringList MainUI::getCheckedItems(){
185  //Return the pbiID's of all the checked items
186  QStringList output;
187  for(int i=0; i<ui->tree_install_apps->topLevelItemCount(); i++){
188    if(ui->tree_install_apps->topLevelItem(i)->checkState(0) == Qt::Checked){
189      output << ui->tree_install_apps->topLevelItem(i)->whatsThis(0);
190    }
191  }
192  //If no items checked, use the item that is selected instead
193  if(output.isEmpty()){
194    //Make sure that an item is selected
195    if(ui->tree_install_apps->topLevelItemCount() >0){
196      output << ui->tree_install_apps->currentItem()->whatsThis(0); 
197    }
198  }
199  return output;       
200}
201
202// === SLOTS ===
203void MainUI::slotRefreshInstallTab(){
204  //Update the list of installed PBI's w/o clearing the list (loses selections)
205   //Get the list we need (in order)
206  QStringList installList = PBI->installedList();
207  installList.sort(); //sort in ascending order
208   //Get the list we have now
209  QStringList cList;
210  for(int i=0; i<ui->tree_install_apps->topLevelItemCount(); i++){
211    cList << ui->tree_install_apps->topLevelItem(i)->whatsThis(0);
212  }
213  //Quick finish if no items installed
214  if(installList.isEmpty()){
215    ui->tree_install_apps->clear();
216  }else{
217    //Now make adjustments as necessary
218    for(int i=0; i<installList.length(); i++){
219      //Detemine what action should be done with this item location
220      int todo = 0; //0=insert new item, 1=update current item, 2=remove current item
221      if(i < cList.length()){
222        if(installList[i] == cList[i]){ todo=1; }
223        else if( installList.contains(cList[i]) && !cList.contains(installList[i]) ){ todo=0; } //new item to be inserted here
224        else if( !installList.contains(cList[i]) ){ todo=2; } //current item in this location should be removed
225      }
226      //Now perform the action on this location
227      if(todo==0){ 
228        //insert new item
229        QTreeWidgetItem *item = new QTreeWidgetItem; //create the item
230        item->setWhatsThis(0,installList[i]);
231        //Now format the display
232        formatInstalledItemDisplay(item);
233        //Now insert this item onto the list
234        ui->tree_install_apps->insertTopLevelItem(i,item);
235        cList.insert(i,installList[i]); //reflect this inclusion into the current list
236      }else if(todo==1){
237        //Update current item
238        formatInstalledItemDisplay( ui->tree_install_apps->topLevelItem(i) );
239      }else{
240        //Remove current item
241        ui->tree_install_apps->takeTopLevelItem(i);
242        cList.removeAt(i); //reflect the change to the current list
243        i--; //Re-check the item that should be in this location
244      }
245    }
246    //Now makesure that there are no extra items at the end
247    int il = installList.length();
248    while(il < cList.length()){
249      ui->tree_install_apps->takeTopLevelItem(il);
250      cList.removeAt(il); //reflect the change to the current list
251    }
252  } //end of empty list check
253 
254  //Make sure that there is an item selected
255  if(ui->tree_install_apps->topLevelItemCount() > 0 ){
256    if( ui->tree_install_apps->selectedItems().isEmpty() ){
257      ui->tree_install_apps->setCurrentItem( ui->tree_install_apps->topLevelItem(0) );
258    }
259    //Now re-size the columns to the minimum required width
260    for(int i=0; i<3; i++){
261      ui->tree_install_apps->resizeColumnToContents(i);
262    } 
263  }
264  on_tree_install_apps_itemSelectionChanged(); //Update the info boxes
265  slotDisplayStats();
266}
267
268void MainUI::slotPBIStatusUpdate(QString pbiID){
269  for(int i=0; i<ui->tree_install_apps->topLevelItemCount(); i++){
270    QString itemID = ui->tree_install_apps->topLevelItem(i)->whatsThis(0);
271    if(itemID == pbiID){
272      QString stat = PBI->PBIInfo(pbiID,QStringList()<<"status").join("");
273      ui->tree_install_apps->topLevelItem(i)->setText(2,stat);
274      // See if we need to update the download progress bar
275      QString appID;
276      if(ui->tree_install_apps->topLevelItemCount() > 0){
277         appID = ui->tree_install_apps->currentItem()->whatsThis(0);
278      }
279      if ( appID == pbiID ) {
280        on_tree_install_apps_itemSelectionChanged();
281      }
282      return; // Found our match, we can return now
283    }
284  }
285}
286
287void MainUI::on_group_install_showinfo_toggled(bool show){
288  if(ui->tree_install_apps->topLevelItemCount() > 0){
289    ui->group_install_info->setVisible(show);
290  }else{
291    ui->group_install_info->setVisible(FALSE);   
292  }
293}
294
295void MainUI::on_tool_install_gotobrowserpage_clicked(){
296  //When you want to open up the browser page for an application
297  QString appID = Extras::nameToID( ui->tree_install_apps->currentItem()->text(0) );
298  slotGoToApp(appID);
299}
300
301void MainUI::on_tool_install_toggleall_clicked(){
302  //Determine if the current item is checked or unchecked
303  bool checkall = (ui->tree_install_apps->currentItem()->checkState(0) == Qt::Unchecked);
304  for(int i=0; i<ui->tree_install_apps->topLevelItemCount(); i++){
305    if(checkall){ ui->tree_install_apps->topLevelItem(i)->setCheckState(0,Qt::Checked); }
306    else{ui->tree_install_apps->topLevelItem(i)->setCheckState(0,Qt::Unchecked); }
307  }
308}
309
310void MainUI::on_tree_install_apps_itemSelectionChanged(){
311  //When an installed PBI is clicked on
312  QString appID;
313  if(ui->tree_install_apps->topLevelItemCount() > 0){
314    appID = ui->tree_install_apps->currentItem()->whatsThis(0);
315  }
316  if(appID.isEmpty()){
317    ui->group_install_info->setVisible(FALSE);
318    return;
319  }else{
320    ui->group_install_info->setVisible( ui->group_install_showinfo->isChecked() );       
321  }
322
323  //Get the PBI info for that item
324  QStringList vals; 
325  vals << "name" << "icon" << "author" << "website" << "version" << "license";
326  QStringList bools;
327  bools << "autoupdate" << "hasdesktopicons" << "hasmenuicons" << "requiresroot";
328  vals = PBI->PBIInfo(appID,vals);
329  bools = PBI->PBIInfo(appID,bools);
330  //Make sure the info lists are not empty
331  if(vals.isEmpty() || bools.isEmpty()){
332    ui->group_install_info->setVisible(FALSE);
333    return; 
334  }
335  //Load a default icon if none found
336  if(vals[1].isEmpty()){ vals[1] = defaultIcon; }
337  //Now set the info on the UI
338  bool desktopSC = (bools[1] == "true"); // XDG desktop entries available
339  bool menuSC= (bools[2] == "true");     // XDG menu entries available
340  bool autoupdate = (bools[0] == "true"); //auto-update enabled
341  bool rootonly = (bools[3] == "true"); //requires root
342  //Create the shortcuts string
343  QString shortcuts;
344    if(desktopSC && menuSC){ shortcuts = tr("Desktop/Menu"); }
345    else if(desktopSC){ shortcuts = tr("Desktop"); }
346    else if(menuSC){ shortcuts = tr("Menu"); }
347    else{ shortcuts = tr("None"); }
348  //Now display that info on the UI
349  ui->label_install_app->setText(vals[0]);
350  ui->label_install_icon->setPixmap( QPixmap(vals[1]) );
351  if(vals[3].isEmpty()){ 
352    ui->label_install_author->setText(vals[2]); 
353    ui->label_install_author->setToolTip("");
354  }else{ 
355    ui->label_install_author->setText("<a href="+vals[3]+">"+vals[2]+"</a>"); 
356    ui->label_install_author->setToolTip(vals[3]); //show website URL as tooltip
357  }
358  ui->label_install_license->setText(vals[5]);
359  ui->label_install_version->setText(vals[4]);
360  ui->label_install_shortcuts->setText(shortcuts);
361  ui->check_install_autoupdate->setChecked(autoupdate);
362 
363  //Adjust the quick action buttons as necessary
364  if( PBI->isWorking(appID) ){
365    //Actions pending/working only show cancel button
366    ui->tool_install_cancel->setVisible(TRUE);
367    ui->tool_install_remove->setVisible(FALSE);
368    ui->tool_install_update->setVisible(FALSE);
369  }else{
370    //Nothing pending
371    ui->tool_install_cancel->setVisible(FALSE);
372    if( !PBI->isInstalled(appID).isEmpty() ){ 
373      //Remove Button
374      ui->tool_install_remove->setVisible(TRUE);
375      if(rootonly){ ui->tool_install_remove->setIcon(QIcon(":icons/remove-root.png")); }
376      else{ ui->tool_install_remove->setIcon(QIcon(":icons/remove.png")); }
377      //Update
378      if(PBI->upgradeAvailable(appID).isEmpty()){ ui->tool_install_update->setVisible(FALSE); }
379      else{
380        ui->tool_install_update->setVisible(TRUE); 
381        if(rootonly){ ui->tool_install_update->setIcon(QIcon(":icons/app_upgrade_small-root.png")); }
382        else{ ui->tool_install_update->setIcon(QIcon(":icons/app_upgrade_small.png")); }
383      }
384    }else{ 
385      ui->tool_install_remove->setVisible(FALSE); 
386      ui->tool_install_update->setVisible(FALSE); 
387    }   
388  }
389  //Update the current status indicators
390  QString stat = PBI->currentAppStatus(appID,true); //get the raw status
391  if(stat.isEmpty() || stat == "DLSTART"  || stat == "DLDONE"){
392    //Not currently running - hide the display indicators
393    ui->group_install_appStat->setVisible(FALSE);
394  }else if(stat.startsWith("DLSTAT::")){
395    //Currently downloading - show download status indicators
396    QString percent = stat.section("::",1,1);
397    QString total = stat.section("::",2,2);
398    QString speed = stat.section("::",3,3);
399    ui->group_install_appStat->setVisible(TRUE);
400      ui->progress_install_DL->setVisible(TRUE);
401    if(total == "??"){ ui->label_install_status->setText( tr("Downloading file:") ); }
402    else{ ui->label_install_status->setText( QString(tr("Downloading %1 file:")).arg(total) ); }
403    if(percent == "??"){
404      ui->progress_install_DL->setMinimum(0); ui->progress_install_DL->setMaximum(0);
405    }else{
406      ui->progress_install_DL->setMinimum(0); ui->progress_install_DL->setMaximum(100);
407      ui->progress_install_DL->setValue( int(percent.toFloat()) );
408    }
409    if(speed == "??"){ ui->label_install_DL->setVisible(FALSE); }
410    else{
411      ui->label_install_DL->setVisible(TRUE);
412      ui->label_install_DL->setText(speed);
413    }
414  }else{
415    //Currently installing/removing/updating - show last message from process
416    ui->label_install_status->setText(stat);
417    ui->group_install_appStat->setVisible(TRUE);
418      ui->progress_install_DL->setVisible(FALSE);
419      ui->label_install_DL->setVisible(FALSE);
420  }
421}
422
423void MainUI::on_check_install_autoupdate_clicked(){
424  //Get the current item
425  QString appID;
426  if(ui->tree_install_apps->topLevelItemCount() > 0){
427    appID = ui->tree_install_apps->currentItem()->whatsThis(0);
428  }
429  if(appID.isEmpty()){return;}
430  //Now determine the current state of the checkbox
431  bool enabled = ui->check_install_autoupdate->isChecked();
432  //Now have the backend make the change
433  PBI->enableAutoUpdate(appID, enabled);
434}
435
436void MainUI::on_tool_install_update_clicked(){
437  //Get the current item
438  QString appID;
439  if(ui->tree_install_apps->topLevelItemCount() > 0){
440    appID = ui->tree_install_apps->currentItem()->whatsThis(0);
441  }
442  if(appID.isEmpty()){return;}
443  PBI->upgradePBI(QStringList() << appID);
444}
445
446void MainUI::on_tool_install_remove_clicked(){
447  //Get the current item
448  QString appID;
449  if(ui->tree_install_apps->topLevelItemCount() > 0){
450    appID = ui->tree_install_apps->currentItem()->whatsThis(0);
451  }
452  if(appID.isEmpty()){return;}
453  //Verify removal
454  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) ){
455    PBI->removePBI(QStringList() << appID);
456  }
457}
458
459void MainUI::on_tool_install_cancel_clicked(){
460  //Get the current item
461  QString appID;
462  if(ui->tree_install_apps->topLevelItemCount() > 0){
463    appID = ui->tree_install_apps->currentItem()->whatsThis(0);
464  }
465  if(appID.isEmpty()){return;}
466  PBI->cancelActions(QStringList() << appID);
467 
468}
469
470// === SELECTED PBI ACTIONS ===
471void MainUI::slotActionAddDesktop(){
472  QStringList items = getCheckedItems();
473  PBI->addDesktopIcons(items,FALSE); //only for current user
474}
475
476void MainUI::slotActionRemoveDesktop(){
477  QStringList items = getCheckedItems();
478  PBI->rmDesktopIcons(items,FALSE);  //Only for current user
479}
480
481void MainUI::slotActionAddPath(){
482  QStringList items = getCheckedItems();
483  PBI->addPaths(items,FALSE);  //Only for current user 
484}
485
486void MainUI::slotActionRemovePath(){
487  QStringList items = getCheckedItems();
488  PBI->rmPaths(items,FALSE);  //Only for current user
489}
490
491void MainUI::slotActionAddPathAll(){
492  QStringList items = getCheckedItems();
493  PBI->addPaths(items,TRUE);  //For all users (root permissions required)
494}
495
496void MainUI::slotActionAddMenu(){
497  QStringList items = getCheckedItems();
498  PBI->addMenuIcons(items,FALSE);  //Only for current user
499}
500
501void MainUI::slotActionRemoveMenu(){
502  QStringList items = getCheckedItems();
503  PBI->rmMenuIcons(items,FALSE);  //Only for current user       
504}
505
506void MainUI::slotActionAddMenuAll(){
507  QStringList items = getCheckedItems();
508  PBI->addMenuIcons(items,TRUE);  //For all users (root permissions required)   
509}
510
511void MainUI::slotActionAddMime(){
512  QStringList items = getCheckedItems();
513  PBI->addMimeTypes(items,FALSE);  //Only for current user     
514}
515
516void MainUI::slotActionRemoveMime(){
517  QStringList items = getCheckedItems();
518  PBI->rmMimeTypes(items,FALSE);  //Only for current user       
519}
520
521void MainUI::slotActionAddMimeAll(){
522    QStringList items = getCheckedItems();
523  PBI->addMimeTypes(items,TRUE);  //For all users (root permissions required)   
524}
525
526void MainUI::slotActionUpdate(){
527  QStringList checkedID = getCheckedItems();
528  if(!checkedID.isEmpty()){
529    PBI->upgradePBI(checkedID); 
530  }
531}
532
533void MainUI::slotActionRemove(){
534  QStringList checkedID = getCheckedItems();
535  if(!checkedID.isEmpty()){
536    //Verify that the user really wants to remove these apps
537    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) ){
538      PBI->removePBI(checkedID);
539    }
540  }
541}
542
543void MainUI::slotActionCancel(){
544  QStringList checkedID = getCheckedItems();
545  if(!checkedID.isEmpty()){
546    PBI->cancelActions(checkedID); 
547  }
548}
549
550
551// ==========================
552// ====== BROWSER TAB =======
553// ==========================
554void MainUI::initializeBrowserTab(){
555  ui->tab_browse->setEnabled(FALSE);
556  //Always make sure that the browser starts off on the "home" page
557  ui->stacked_browser->setCurrentWidget(ui->page_home);
558  //With invisible shortcut buttons
559  ui->tool_browse_cat->setVisible(FALSE);
560  ui->tool_browse_app->setVisible(FALSE);
561  //Clear any items left over from the designer form
562  clearScrollArea(ui->scroll_br_home_newapps);
563  clearScrollArea(ui->scroll_br_home_cats);
564  //Search functionality
565  searchTimer = new QTimer();
566    searchTimer->setSingleShot(TRUE);
567    searchTimer->setInterval(300); // 0.3 sec wait before a search
568    connect(searchTimer,SIGNAL(timeout()),this,SLOT(slotGoToSearch()) );
569  connect(ui->tool_browse_search,SIGNAL(clicked()),this,SLOT(slotGoToSearch()) );
570  connect(ui->line_browse_searchbar,SIGNAL(returnPressed()),this,SLOT(slotGoToSearch()) );
571}
572
573// === SLOTS ===
574void MainUI::slotDisableBrowser(bool shownotification){
575  if(shownotification){ qDebug() << "No Repo Available: De-activating the Browser"; }
576  ui->tabWidget->setCurrentWidget(ui->tab_installed);
577  ui->tool_install_gotobrowserpage->setEnabled(FALSE);
578  ui->tab_browse->setEnabled(FALSE);
579  slotDisplayStats();
580}
581
582void MainUI::slotEnableBrowser(){
583  qDebug() << "Repo Ready: - generating browser home page";
584  //Now create the browser home page
585  slotUpdateBrowserHome();
586  //And allow the user to go there
587  ui->tool_install_gotobrowserpage->setEnabled(TRUE);
588  ui->tab_browse->setEnabled(TRUE);
589  slotDisplayStats();
590}
591
592void MainUI::slotUpdateBrowserHome(){
593  //Load the Categories
594  QStringList cats = PBI->browserCategories();
595  clearScrollArea(ui->scroll_br_home_cats);
596  QVBoxLayout *catlayout = new QVBoxLayout;
597  QStringList info; info << "name" << "description" << "icon";
598  cats.sort(); //sort them alphabetically
599  for(int i=0; i<cats.length(); i++){
600    QStringList data = PBI->CatInfo(cats[i],info);
601    if(!data.isEmpty()){
602      LargeItemWidget *item = new LargeItemWidget(cats[i],data[0],data[1],data[2]);
603      connect(item,SIGNAL(appClicked(QString)),this,SLOT(slotGoToCategory(QString)) );
604      catlayout->addWidget(item);
605    }
606  }
607  catlayout->addStretch(); //add a spacer to the end
608  ui->scroll_br_home_cats->widget()->setLayout(catlayout);
609  //Load the newest applications
610  clearScrollArea(ui->scroll_br_home_newapps);
611  QHBoxLayout *newapplayout = new QHBoxLayout;
612  QStringList newapps = PBI->getRecentApps();
613  for(int i=0; i<newapps.length(); i++){
614    QStringList appdata = PBI->AppInfo(newapps[i],QStringList() << "name" << "icon" << "latestversion");
615    if(!appdata.isEmpty()){
616      SmallItemWidget *item = new SmallItemWidget(newapps[i],appdata[0],appdata[1],appdata[2]);
617      connect(item,SIGNAL(appClicked(QString)),this,SLOT(slotGoToApp(QString)) );
618      newapplayout->addWidget(item);
619    }
620  }
621  newapplayout->addStretch(); //add a spacer to the end
622  newapplayout->setContentsMargins(0,0,0,0);
623  newapplayout->setSpacing(0);
624  ui->scroll_br_home_newapps->widget()->setLayout(newapplayout);
625  //Make sure that the newapps scrollarea is the proper fit vertically (no vertical scrolling)
626  ui->scroll_br_home_newapps->setMinimumHeight(ui->scroll_br_home_newapps->widget()->minimumSizeHint().height()+ui->scroll_br_home_newapps->horizontalScrollBar()->height());
627 
628  //Make sure the new apps area is invisible if no items available
629  if(newapps.isEmpty()){ ui->group_br_home_newapps->setVisible(FALSE); }
630  else{ ui->group_br_home_newapps->setVisible(TRUE); }
631  //make sure the home page is visible in the browser (slotGoToHome without changing tabs)
632  ui->stacked_browser->setCurrentWidget(ui->page_home); 
633  //Make sure the shortcut buttons are disabled
634  ui->tool_browse_cat->setVisible(FALSE);
635  ui->tool_browse_app->setVisible(FALSE); 
636 
637}
638
639void MainUI::slotGoToHome(){
640  ui->tabWidget->setCurrentWidget(ui->tab_browse);
641  ui->stacked_browser->setCurrentWidget(ui->page_home); 
642  //Make sure the shortcut buttons are disabled
643  ui->tool_browse_cat->setVisible(FALSE);
644  ui->tool_browse_app->setVisible(FALSE);
645}
646
647void MainUI::slotGoToCategory(QString cat){
648  qDebug() << "Show Category:" << cat;
649  //Get the apps in this category
650  QStringList apps = PBI->browserApps(cat);
651  if(apps.isEmpty()){
652    qDebug() << "Invalid category:" << cat;
653    return;
654  }
655  clearScrollArea(ui->scroll_br_cat_apps);
656  apps.sort();
657  QVBoxLayout *applayout = new QVBoxLayout;
658  QStringList info; info << "name" << "description" << "icon";
659  for(int i=0; i<apps.length(); i++){
660    QStringList data = PBI->AppInfo(apps[i],info);
661    if(!data.isEmpty()){
662      //Trim the description if necessary
663      if(data[1].size()>100){ data[1] = data[1].left(100).append("..."); }
664      LargeItemWidget *item = new LargeItemWidget(apps[i],data[0],data[1],data[2]);
665      connect(item,SIGNAL(appClicked(QString)),this,SLOT(slotGoToApp(QString)) );
666      applayout->addWidget(item); 
667    }
668  }
669  applayout->addStretch();
670  ui->scroll_br_cat_apps->widget()->setLayout(applayout);
671  //Now enable/disable the shortcut buttons
672  ui->tool_browse_app->setVisible(FALSE);
673  ui->tool_browse_cat->setVisible(TRUE);
674    QStringList catinfo = PBI->CatInfo(cat,QStringList() << "name" << "icon");
675    ui->tool_browse_cat->setText(catinfo[0]);
676    if(catinfo[1].isEmpty()){ catinfo[1] = defaultIcon; }
677    ui->tool_browse_cat->setIcon(QIcon(catinfo[1]));
678  ui->tabWidget->setCurrentWidget(ui->tab_browse);
679  ui->stacked_browser->setCurrentWidget(ui->page_cat);
680  //Now save that this category is currently displayed
681  cCat = cat;
682}
683
684void MainUI::slotGoToApp(QString appID){
685  qDebug() << "Show App:" << appID;
686  //Get the general application info
687  QStringList data; 
688  data << "name" << "icon" << "author" << "website" << "license" << "type" << "description" \
689       << "category" << "requiresroot" << "latestversion" << "latestarch" <<"latestsize" \
690       << "backupversion" << "backuparch" << "backupsize";
691  data = PBI->AppInfo(appID,data);
692  if(data.isEmpty()){
693    qDebug() << "Invalid App:" << appID;
694    return;
695  }
696  //Start the search for similar apps
697  PBI->searchSimilar = appID;
698  ui->group_bapp_similar->setVisible(FALSE);
699  QTimer::singleShot(0,PBI,SLOT(startSimilarSearch()));
700  //Now Check icon
701  if(data[1].isEmpty()){ data[1] = defaultIcon; }
702  //Now fill the UI with the data
703  ui->label_bapp_name->setText(data[0]);
704  ui->label_bapp_icon->setPixmap(QPixmap(data[1]));
705  if(data[3].isEmpty()){ ui->label_bapp_authorweb->setText(data[2]); }
706  else{ ui->label_bapp_authorweb->setText("<a href="+data[3]+">"+data[2]+"</a>"); }
707  ui->label_bapp_authorweb->setToolTip(data[3]);
708  ui->label_bapp_license->setText(data[4]);
709  ui->label_bapp_type->setText(data[5]);
710  ui->text_bapp_description->setText(data[6]);
711  //Now determine the appropriate version info
712  QString pbiID = PBI->isInstalled(appID); //get pbiID
713  QString cVer;
714  if(!pbiID.isEmpty()){ cVer = PBI->PBIInfo(pbiID,QStringList("version")).join(""); }
715  bool useLatest=FALSE;
716  bool nobackup = data[12].isEmpty();
717  if(cVer.isEmpty()){ useLatest=TRUE; } //not currently installed
718  else if(cVer != data[9]){ useLatest=TRUE;} //not the latest version
719  //Now put the proper version info on the UI
720  if(useLatest || nobackup){
721    ui->label_bapp_version->setText(data[9]);
722    ui->label_bapp_arch->setText(data[10]);
723    if(data[11].isEmpty()){ ui->label_bapp_size->setText(tr("Unknown")); }
724    else{ ui->label_bapp_size->setText( Extras::sizeKToDisplay(data[11]) ); }
725  }else{
726    ui->label_bapp_version->setText(data[12]);
727    ui->label_bapp_arch->setText(data[13]);
728    if(data[14].isEmpty()){ ui->label_bapp_size->setText(tr("Unknown")); }
729    else{ ui->label_bapp_size->setText( Extras::sizeKToDisplay(data[14]) ); }
730  }
731  //Now update the download button appropriately
732  QString ico;
733  QString working = PBI->currentAppStatus(appID);
734  if(!working.isEmpty()){ //app currently pending or actually doing something
735    ui->tool_bapp_download->setText(working);
736    ui->tool_bapp_download->setIcon(QIcon(":icons/working.png"));
737    ui->tool_bapp_download->setEnabled(FALSE);
738  }else if(useLatest && cVer.isEmpty()){ //new installation
739    ui->tool_bapp_download->setText(tr("Install Now!"));
740    ico = ":icons/app_download.png";
741    ui->tool_bapp_download->setEnabled(TRUE);
742  }else if(useLatest){ //Update available
743    ui->tool_bapp_download->setText(tr("Update"));
744    ico = ":icons/app_upgrade.png";
745    ui->tool_bapp_download->setEnabled(TRUE);
746  }else if(!nobackup){  //Downgrade available
747    ui->tool_bapp_download->setText(tr("Downgrade"));
748    ico = ":icons/app_downgrade.png";
749    ui->tool_bapp_download->setEnabled(TRUE);
750  }else{ //already installed (no downgrade available)
751    ui->tool_bapp_download->setText(tr("Installed"));
752    ui->tool_bapp_download->setIcon(QIcon(":icons/dialog-ok.png"));
753    ui->tool_bapp_download->setEnabled(FALSE);
754  }
755  //Now set the icon appropriately if it requires root permissions
756  if(!ico.isEmpty()){
757    if(data[8]=="true"){ //requires root permissions to install
758      ico.replace(".png","-root.png");
759    }
760    ui->tool_bapp_download->setIcon(QIcon(ico));
761  }
762  ui->tool_bapp_download->setWhatsThis(appID); //set for slot
763  //Now enable/disable the shortcut buttons
764  ui->tool_browse_app->setVisible(TRUE);
765    ui->tool_browse_app->setText(data[0]);
766    ui->tool_browse_app->setIcon(QIcon(data[1]));
767  QStringList catinfo = PBI->CatInfo(Extras::nameToID(data[7]),QStringList() << "name" << "icon");
768  if(!catinfo.isEmpty()){
769    ui->tool_browse_cat->setVisible(TRUE);
770    ui->tool_browse_cat->setText(catinfo[0]);
771    if(catinfo[1].isEmpty()){ catinfo[1] = defaultIcon; }
772    ui->tool_browse_cat->setIcon(QIcon(catinfo[1]));
773  }
774  ui->tabWidget->setCurrentWidget(ui->tab_browse);
775  ui->stacked_browser->setCurrentWidget(ui->page_app);
776       
777}
778
779void MainUI::slotGoToSearch(){
780  searchTimer->stop(); //just in case "return" was pressed to start the search
781  QString search = ui->line_browse_searchbar->text();
782  if(search.isEmpty()){ return; }
783  PBI->searchTerm = search;
784  QTimer::singleShot(1,PBI,SLOT(startAppSearch()));
785  ui->label_bsearch_info->setText( tr("Searching the application database. Please Wait....") );
786    ui->label_bsearch_info->setVisible(TRUE);
787    ui->group_bsearch_best->setVisible(FALSE);
788    ui->group_bsearch_other->setVisible(FALSE);
789 
790}
791       
792void MainUI::slotShowSimilarApps(QStringList apps){
793  qDebug() << " - Similar applications:" << apps;
794  if(apps.isEmpty()){ ui->group_bapp_similar->setVisible(FALSE); }
795  else{
796    clearScrollArea(ui->scroll_bapp_similar);
797    QHBoxLayout *layout = new QHBoxLayout;
798    for(int i=0; i<apps.length(); i++){
799      QStringList appdata = PBI->AppInfo(apps[i],QStringList() << "name" << "icon");
800      if(!appdata.isEmpty()){
801        SmallItemWidget *item = new SmallItemWidget(apps[i],appdata[0],appdata[1],"");
802        connect(item,SIGNAL(appClicked(QString)),this,SLOT(slotGoToApp(QString)) );
803        layout->addWidget(item);
804      }
805    }
806    layout->addStretch(); //add a spacer to the end
807    layout->setContentsMargins(1,1,1,1);
808    ui->scroll_bapp_similar->widget()->setLayout(layout);
809    //Make sure that the similar scrollarea is the proper fit vertically (no vertical scrolling)
810    ui->scroll_bapp_similar->setMinimumHeight(ui->scroll_bapp_similar->widget()->minimumSizeHint().height()+ui->scroll_bapp_similar->horizontalScrollBar()->height());
811    //Now make the group visible as appropriate
812    ui->group_bapp_similar->setVisible(TRUE);
813    if(ui->group_bapp_similar->isChecked()){ ui->scroll_bapp_similar->setVisible(TRUE); }
814    else{ ui->scroll_bapp_similar->setVisible(FALSE); }
815    //Now make sure the app page has the proper layout dimensions
816    ui->page_app->updateGeometry();
817  }
818}
819
820void MainUI::slotShowSearchResults(QStringList best, QStringList rest){
821  //Now display the search results
822  if(best.isEmpty()){
823    ui->label_bsearch_info->setText( QString(tr("No Search Results Found for the term: %1")).arg(ui->line_browse_searchbar->text()) );
824    ui->label_bsearch_info->setVisible(TRUE);
825    ui->group_bsearch_best->setVisible(FALSE);
826    ui->group_bsearch_other->setVisible(FALSE);
827  }else{
828    ui->label_bsearch_info->setVisible(FALSE);
829    //Now fill the "best" section
830    clearScrollArea(ui->scroll_bsearch_best);
831    QVBoxLayout *layout = new QVBoxLayout;
832    QStringList info; info << "name" << "description" << "icon";
833    for(int i=0; i<best.length(); i++){
834      QStringList data = PBI->AppInfo(best[i],info);
835      if(!data.isEmpty()){
836        //Trim the description if necessary
837        if(data[1].size()>100){ data[1] = data[1].left(100).append("..."); }
838        LargeItemWidget *item = new LargeItemWidget(best[i],data[0],data[1],data[2]);
839        connect(item,SIGNAL(appClicked(QString)),this,SLOT(slotGoToApp(QString)) );
840        layout->addWidget(item); 
841      }
842    }
843    layout->addStretch();
844    ui->scroll_bsearch_best->widget()->setLayout(layout);
845    ui->group_bsearch_best->setVisible(TRUE);
846   
847    //Now fill the other results
848    if(rest.isEmpty()){ ui->group_bsearch_other->setVisible(FALSE); }
849    else{
850      ui->group_bsearch_other->setVisible(TRUE);
851      clearScrollArea(ui->scroll_bsearch_other);
852      QVBoxLayout *layout2 = new QVBoxLayout;
853      for(int i=0; i<rest.length(); i++){
854        QStringList data = PBI->AppInfo(rest[i],info);
855        if(!data.isEmpty()){
856          //Trim the description if necessary
857          if(data[1].size()>100){ data[1] = data[1].left(100).append("..."); }
858          LargeItemWidget *item = new LargeItemWidget(rest[i],data[0],data[1],data[2]);
859          connect(item,SIGNAL(appClicked(QString)),this,SLOT(slotGoToApp(QString)) );
860          layout2->addWidget(item); 
861        }
862      }
863      layout2->addStretch();
864      ui->scroll_bsearch_other->widget()->setLayout(layout2);
865    }
866  }
867       
868  //need to make sure the search bar still has keyboard focus (just in case)
869  ui->tabWidget->setCurrentWidget(ui->tab_browse);
870  ui->stacked_browser->setCurrentWidget(ui->page_search);
871 
872}
873
874void MainUI::on_tabWidget_currentChanged(){
875  if(ui->tabWidget->currentWidget() == ui->tab_browse){
876    //Refresh the app page if that is the one currently showing
877    if(ui->stacked_browser->currentWidget() == ui->page_app){ on_tool_browse_app_clicked(); }     
878  }
879}
880
881void MainUI::on_tool_browse_home_clicked(){
882  slotGoToHome();
883}
884
885void MainUI::on_tool_browse_cat_clicked(){
886  QString cat = Extras::nameToID(ui->tool_browse_cat->text());
887  if(cCat == cat){ //already loaded - just show it (prevents resetting scroll positions)
888    ui->stacked_browser->setCurrentWidget(ui->page_cat);
889  }else{ //load and show the category
890    slotGoToCategory(cat);
891  }
892}
893
894void MainUI::on_tool_browse_app_clicked(){
895  QString appID = Extras::nameToID(ui->tool_browse_app->text());
896  slotGoToApp(appID);
897}
898
899void MainUI::on_line_browse_searchbar_textChanged(){
900  //Connect this to a singleshot timer, so the search functionality is only
901  //  run once after a short wait rather than every time a new character is typed
902 
903  //Live search only after 3 characters have been typed
904  if(ui->line_browse_searchbar->text().length() > 2){
905    searchTimer->start();
906  }else{
907    searchTimer->stop();         
908  }
909}
910
911void MainUI::on_tool_bapp_download_clicked(){
912  QString appID = ui->tool_bapp_download->whatsThis();
913  PBI->installApp(QStringList() << appID);
914  ui->tool_bapp_download->setEnabled(FALSE); //make sure it cannot be clicked more than once before page refresh
915  //Now show the Installed tab
916  ui->tabWidget->setCurrentWidget(ui->tab_installed);
917}
918
919void MainUI::on_group_br_home_newapps_toggled(bool show){
920  ui->scroll_br_home_newapps->setVisible(show);
921}
922
923void MainUI::on_group_bapp_similar_toggled(bool show){
924  ui->scroll_bapp_similar->setVisible(show);
925}
926
927// ================================
928// ==========  OTHER ==============
929// ================================
930void MainUI::clearScrollArea(QScrollArea* area){
931  QWidget *wgt = area->takeWidget();
932  delete wgt; //delete the widget and all children
933  area->setWidget( new QWidget() ); //create a new widget in the scroll area
934  area->widget()->setContentsMargins(0,0,0,0);
935}
936
937void MainUI::slotDisplayError(QString title,QString message,QStringList log){
938  QMessageBox *dlg = new QMessageBox(this);
939    dlg->setWindowTitle(title);
940    dlg->setText(message);
941    dlg->setDetailedText(log.join("\n"));
942  dlg->exec();
943}
944
945void MainUI::slotDisplayStats(){
946  int avail = PBI->numAvailable;
947  int installed = PBI->numInstalled;
948  QString text;
949  if(avail == -1){
950    text = QString(tr("Installed: %1")).arg(QString::number(installed)); 
951  }else{
952    text = QString(tr("Installed: %1")+"\t"+tr("Available: %2")).arg(QString::number(installed), QString::number(avail)); 
953  }
954  //Get the number of installed/available applications and display it
955  statusLabel->setText(text);   
956}
Note: See TracBrowser for help on using the repository browser.