source: src-qt4/pc-softwaremanager/mainUI.cpp @ 51d6c4b

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

Have the AppCafe? use the selected item if none are checked, and try out a fix for crashes if there are no PBI's installed and you cancel the only download.

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