code cleanup
[fapman] / mainwindow.cpp
index 036d6ee..a2f6c5c 100644 (file)
 */
 
 #ifdef MYDEF_GTK_EXISTS
-#include <gdk/gdk.h>
-#include <gtk/gtk.h>
+extern "C"
+{
+       #include <gdk/gdk.h>
+       #include <gtk/gtk.h>
+}
 #endif
 
 #include <QtCore>
 #include <QtMaemo5>
 #endif
 
+extern "C"
+{
+       #include <sys/vfs.h>
+}
+
 #include "mainwindow.h"
 #include "version.h"
 #include "ui_mainwindow.h"
@@ -88,8 +96,11 @@ MainWindow::MainWindow(QWidget *parent) :
        iUpgradeAutoUpdate = true;
        iNextOperation = OpNone;
 
+       connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
+
        ui->centralWidget->loadWallpaper();
 
+       /*
        QString stylesheet_mainscreen =
                        "QPushButton {"
                        "border-radius: 16px;"
@@ -105,6 +116,7 @@ MainWindow::MainWindow(QWidget *parent) :
                        "border-style: inset;"
                        "background-color: rgba(255,255,255,150);"
                        "}";
+       */
 
        if( ((QApplication*)QApplication::instance())->styleSheet().isEmpty() )
        {
@@ -119,7 +131,7 @@ MainWindow::MainWindow(QWidget *parent) :
                }
 
                if( stylesheet_file.isEmpty() ) {
-                       ui->centralWidget->setStyleSheet(stylesheet_mainscreen);
+                       //ui->centralWidget->setStyleSheet(stylesheet_mainscreen);
                } else {
                        ((QApplication*)QApplication::instance())->setStyleSheet(stylesheet_file);
                }
@@ -142,6 +154,11 @@ MainWindow::MainWindow(QWidget *parent) :
        iMediaObject = new Phonon::MediaObject(this);
        Phonon::AudioOutput* aout = new Phonon::AudioOutput(Phonon::NotificationCategory, this);
        Phonon::createPath(iMediaObject, aout);
+
+       showFreeSpace();
+
+       show();
+       orientationChanged();
 }
 
 MainWindow::~MainWindow()
@@ -149,13 +166,14 @@ MainWindow::~MainWindow()
        // save "need repo refresh" status
        iSettings->qsettings()->setValue("need_repo_refresh", iAptInterface->needRepoRefresh());
 
-       delete iWinPackageView;
-       delete iWinRepoView;
-       delete iAptInterface;
-       delete iDpkgInterface;
-       delete iDimmer;
-       delete iSettings;
-    delete ui;
+       delete iWinPackageView; iWinPackageView=0;
+       delete iWinRepoView; iWinRepoView=0;
+       delete iAptInterface; iAptInterface=0;
+       delete iDpkgInterface; iDpkgInterface=0;
+       delete iDimmer; iDimmer=0;
+       delete iSettings; iSettings=0;
+       delete ui; ui=0;
+       //iMediaObject and aout automatically deleted by their parent
 }
 
 void MainWindow::changeEvent(QEvent *e)
@@ -313,6 +331,10 @@ void MainWindow::operationQueueFinished(QList<AAptInterface::interfaceMode> last
                GdkEventIconThemeReload();
        }
 
+       QWidget* dialogParent = this;
+       if( iWinPackageView->isVisible() )
+               dialogParent = iWinPackageView;
+
        if( iNextOperation == OpOpenPkgView && success )
        {
                iWinPackageView->openWin();
@@ -351,7 +373,8 @@ void MainWindow::operationQueueFinished(QList<AAptInterface::interfaceMode> last
                if( inst.count()>0 )
                        pkglist += QString("Total download size: %L1 kB<br>").arg(total_dl_size);
                bool mismatch = false;
-               bool warn_system_package = false;
+               bool warn_system_package_remove = false;
+               bool warn_system_package_install = false;
 
                if( remv.count()>0 ) {
                        pkglist += "<br><b><u>REMOVE:</u></b><br><font size=\"-1\">";
@@ -365,7 +388,7 @@ void MainWindow::operationQueueFinished(QList<AAptInterface::interfaceMode> last
                                }
 #ifdef Q_WS_MAEMO_5
                                if( pkg && pkg->maemoDisplayName()=="Maemo 5" ) {
-                                       warn_system_package = true;
+                                       warn_system_package_remove = true;
                                }
 #endif
                                if( remvver.count()>i ) {
@@ -401,7 +424,7 @@ void MainWindow::operationQueueFinished(QList<AAptInterface::interfaceMode> last
                                }
 #ifdef Q_WS_MAEMO_5
                                if( pkg && pkg->maemoDisplayName()=="Maemo 5" ) {
-                                       warn_system_package = true;
+                                       warn_system_package_install = true;
                                }
 #endif
                                if( instver.count()>i ) {
@@ -421,25 +444,35 @@ void MainWindow::operationQueueFinished(QList<AAptInterface::interfaceMode> last
                pkglist += "</font>";
 
                if( mismatch ) {
-                       ConfirmDialog m(false, this);
+                       ConfirmDialog m(false, dialogParent);
                        m.setText("Warning", "There is a version mismatch between your original package selections and some of the packages being installed " \
                                          "from the repositories. This could be due to your application catalogs being out of date.");
                        m.exec();
                }
                if( installing_blacklisted ) {
-                       ConfirmDialog b(false, this);
+                       ConfirmDialog b(false, dialogParent);
                        b.setText("Warning","Blacklisted package(s) will be installed");
                        b.exec();
                }
-               if( warn_system_package ) {
-                       ConfirmDialog s(false, this);
-                       s.setText("Warning","You are trying to perform an operation on a critical system package. This is an operation which has not been tested and " \
-                                         "it is unknown whether it will succeed or result in a horrible failure. You have been warned.");
+               if( warn_system_package_remove ) {
+                       ConfirmDialog s(false, dialogParent);
+                       s.setText("Warning","You are about to remove a critical system package.");
+                       s.exec();
+               }
+               if( warn_system_package_install ) {
+                       ConfirmDialog s(false, dialogParent);
+                       s.setText("Warning","You are trying to perform an install/upgrade operation on a critical system package. Doing a system upgrade with " \
+                                         "Faster Application Manager has not been tested and it could result in a horrible failure. You have been warned.");
                        s.exec();
                }
 
                busyDialog(false);
-               ConfirmDialog d(true, this);
+               ConfirmDialog d(true, dialogParent);
+               if( inst.count()==0 && remv.count()==0 )
+               {
+                       pkglist = "None of the packages can be installed";
+                       d.disableButton();
+               }
                d.setText("Confirmation",pkglist);
                if( d.exec() ) {
                        iAptInterface->addQueuedOperation(AAptInterface::ModeAptGetInstall);
@@ -480,19 +513,20 @@ void MainWindow::operationQueueFinished(QList<AAptInterface::interfaceMode> last
 
                QRect r = QApplication::desktop()->rect();
                if(r.width() < r.height()) {
-                       ConfirmDialog d(false, this);
+                       ConfirmDialog d(false, dialogParent);
                        d.setText(title,msgs.join("<br>"));
                        d.exec();
                } else {
 #ifdef Q_WS_MAEMO_5
                        QMaemo5InformationBox::information(0, text, QMaemo5InformationBox::NoTimeout);
 #else
-                       ConfirmDialog d(false, this);
+                       ConfirmDialog d(false, dialogParent);
                        d.setText(title,msgs.join("<br>"));
                        d.exec();
 #endif
                }
 
+               showFreeSpace();
        }
 
 }
@@ -560,14 +594,22 @@ void MainWindow::on_actionOptions_triggered()
 
 void MainWindow::notifyDialog(QString title, QString msg)
 {
-       ConfirmDialog d(false, this);
+       QWidget* dialogParent = this;
+       if( iWinPackageView->isVisible() )
+               dialogParent = iWinPackageView;
+
+       ConfirmDialog d(false, dialogParent);
        d.setText(title, msg);
        d.exec();
 }
 
 bool MainWindow::confirmDialog(QString title, QString msg)
 {
-       ConfirmDialog d(true, this);
+       QWidget* dialogParent = this;
+       if( iWinPackageView->isVisible() )
+               dialogParent = iWinPackageView;
+
+       ConfirmDialog d(true, dialogParent);
        d.setText(title, msg);
        return d.exec();
 }
@@ -615,3 +657,80 @@ void MainWindow::on_actionLoad_file_triggered()
                        iAptInterface->loadInstallFiles(installs);
        }
 }
+
+void MainWindow::orientationChanged()
+{
+       if( QApplication::desktop()->width() > QApplication::desktop()->height() )
+               ui->listWidget->setGridSize( QSize((ui->listWidget->width()-12)/5, (ui->listWidget->height()-12)/2) );
+       else
+               ui->listWidget->setGridSize( QSize((ui->listWidget->width()-12)/3, (ui->listWidget->height()-12)/3) );
+}
+
+
+void MainWindow::showFreeSpace()
+{
+       quint64 warn_limit_root = 5120;
+       quint64 warn_limit_opt = 51200;
+       struct statfs root_stat;
+       struct statfs opt_stat;
+       statfs("/",&root_stat);
+       statfs("/opt",&opt_stat);
+       quint64 free_root = root_stat.f_bavail * root_stat.f_bsize / 1024;
+       quint64 free_opt = opt_stat.f_bavail * opt_stat.f_bsize / 1024;
+       quint64 total_root = root_stat.f_blocks * root_stat.f_bsize / 1024;
+       quint64 total_opt = opt_stat.f_blocks * opt_stat.f_bsize / 1024;
+       qDebug() << "rootfs" << free_root << "/" << total_root << "kB free";
+       qDebug() << "opt fs" << free_opt << "/" << total_opt << "kB free";
+
+       QString rootstr = QString("rootfs: %L1 / %L2 MB free").arg(free_root/1024).arg(total_root/1024);
+       QString optstr = QString("opt: %L1 / %L2 MB free").arg(free_opt/1024).arg(total_opt/1024);
+
+       ui->label->setText("<font size=\"-1\">" + rootstr + "<br>" + optstr + "</font>");
+
+       /*
+       ui->progressBarRoot->setFormat(rootstr);
+       ui->progressBarRoot->setMaximum(total_root/1024);
+       ui->progressBarRoot->setValue(free_root/1024);
+       ui->progressBarOpt->setFormat(optstr);
+       ui->progressBarOpt->setMaximum(total_opt/1024);
+       ui->progressBarOpt->setValue(free_opt/1024);
+       */
+
+       if( free_root < warn_limit_root || free_opt < warn_limit_opt )
+       {
+               ConfirmDialog d(false, this);
+               QString t;
+               if( free_root < warn_limit_root )
+                       t += QString("Root filesystem has %L1 kB available<br>").arg(free_root);
+               if( free_opt < warn_limit_opt )
+                       t += QString("Opt (home) filesystem has %L1 kB available<br>").arg(free_opt);
+               t += "<br>You may proceed, but consider freeing up space to prevent problems in the future";
+               d.setText("Warning: Low disk space",t);
+               d.exec();
+       }
+}
+
+void MainWindow::on_listWidget_itemClicked(QListWidgetItem* item)
+{
+       qDebug() << "main menu:" << item->statusTip();
+
+       if( item->statusTip() == "manage_repos" ) {
+               on_btnRepos_clicked();
+       }
+       else if( item->statusTip() == "update_catalogs" ) {
+               on_btnUpdate_clicked();
+       }
+       else if( item->statusTip() == "install_apps" ) {
+               on_btnListInstallable_clicked();
+       }
+       else if( item->statusTip() == "remove_apps" ) {
+               on_btnListInstalled_clicked();
+       }
+       else if( item->statusTip() == "upgrade_apps" ) {
+               on_btnUpgrade_clicked();
+       }
+       else {
+               qDebug() << "Warning: Unhandled main menu item";
+       }
+       item->setSelected(false);
+}