source: src-qt4/pc-softwaremanager/mainUI.cpp @ 9a7d00b8

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 9a7d00b8 was 9a7d00b8, checked in by Ken Moore <ken@…>, 18 months ago

A couple minor graphical adjustments to the AppCafe?, and setup case-insensitive sorting of installed applications

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