Conditional compilation for Maemo 5 specific code
[irwi] / src / selectremotedlg.cpp
index 2ea45fc..7bd3f80 100644 (file)
@@ -2,6 +2,8 @@
 
 #include "remote.h"
 #include "remotelistwidgetitem.h"
+#include "onlinepollerthread.h"
+#include "remotetable.h"
 
 #include <QHBoxLayout>
 #include <QLabel>
 SelectRemoteDlg::SelectRemoteDlg(QWidget *parent)
     : QDialog(parent)
 {
+    onlinePollerThread = NULL;
     this->setWindowTitle(tr("Select remote"));
     this->setMinimumHeight(320);
 
     layout = new QHBoxLayout(this);
 
     alphabetList = new QListWidget(this);
-    alphabetList->setMaximumWidth(96);
+    alphabetList->setMaximumWidth(64);
     layout->addWidget(alphabetList);
     connect(alphabetList,
             SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
@@ -31,13 +34,14 @@ SelectRemoteDlg::SelectRemoteDlg(QWidget *parent)
             SLOT(alphabetItemChanged(QListWidgetItem*, QListWidgetItem*)));
     
     mfgList = new QListWidget(this);
+    mfgList->setMaximumWidth(192);
     layout->addWidget(mfgList);
     connect(mfgList,
             SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
             this,
             SLOT(mfgItemChanged(QListWidgetItem*, QListWidgetItem*)));
 
-    modelList = new QListWidget(this);
+    modelList = new RemoteTable(this);
     layout->addWidget(modelList);
 
     downloadBtn = new QPushButton(tr("Download"), this);
@@ -46,16 +50,19 @@ SelectRemoteDlg::SelectRemoteDlg(QWidget *parent)
             this, SLOT(downloadRemote()));
 
     this->setLayout(layout);
-    setBusy(true);
     connect(&remoteDBMgr, SIGNAL(dbReady(RemoteDB*)),
             this, SLOT(setDB(RemoteDB*)));
-    remoteDBMgr.getDBAsync();
+    connect(&remoteDBMgr, SIGNAL(downloadFailed(int)),
+            this, SLOT(onDBError(int)));
 }
 
-
 SelectRemoteDlg::~SelectRemoteDlg()
 {
     delete layout;
+    if (onlinePollerThread != NULL) {
+        delete onlinePollerThread;
+        onlinePollerThread = NULL;
+    }
 }
 
 void SelectRemoteDlg::setDB(RemoteDB *db)
@@ -67,7 +74,9 @@ void SelectRemoteDlg::setDB(RemoteDB *db)
 
 void SelectRemoteDlg::setBusy(bool busy)
 {
+#ifdef Q_WS_MAEMO_5
     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, busy);
+#endif
     setEnabled(!busy);
 }
 
@@ -75,9 +84,8 @@ void SelectRemoteDlg::alphabetItemChanged(QListWidgetItem *current,
                                       QListWidgetItem * /*previous*/)
 {
     mfgList->clear();
-    modelList->clear();
-    if (current)
-    {
+    modelList->removeAllRows();
+    if (current) {
         mfgList->addItems((*remoteDB)[current->text()].keys());
     }
 }
@@ -85,29 +93,62 @@ void SelectRemoteDlg::alphabetItemChanged(QListWidgetItem *current,
 void SelectRemoteDlg::mfgItemChanged(QListWidgetItem *current,
                                      QListWidgetItem * /*previous*/)
 {
-    modelList->clear();
-    if (current)
-    {
+    modelList->removeAllRows();
+    if (current) {
         RemoteList remotes =
             (*remoteDB)[alphabetList->currentItem()->text()][current->text()];
         foreach(Remote *remote, remotes) {
-            modelList->addItem(new RemoteListWidgetItem(remote));
+            modelList->addItem(remote);
         }
     }
 }
 
 void SelectRemoteDlg::downloadRemote()
 {
-    RemoteListWidgetItem *currentModel =
-        static_cast<RemoteListWidgetItem *>(modelList->currentItem());
-    if (currentModel)
-    {
+    Remote *currentModel = modelList->selected();
+    if (currentModel) {
+        setResult(QDialog::Accepted);
         setBusy();
-        connect(currentModel->remote(), SIGNAL(saveFinished()),
+        connect(currentModel, SIGNAL(saveFinished()),
                 this, SLOT(close()));
-        currentModel->remote()->saveToFile();
+        currentModel->saveToFile();
 
-        emit remoteChanged(*(currentModel->remote()));
+        emit remoteChanged(*currentModel);
     }
 }
 
+void SelectRemoteDlg::getDB()
+{
+    if (onlinePollerThread != NULL) {
+        delete onlinePollerThread;
+        onlinePollerThread = NULL;
+    }
+    remoteDBMgr.getDBAsync();
+}
+
+void SelectRemoteDlg::refreshDB()
+{
+    setBusy(true);
+    if (onlinePollerThread != NULL) {
+        delete onlinePollerThread;
+        onlinePollerThread = NULL;
+    }
+    onlinePollerThread = new OnlinePollerThread();
+    connect(onlinePollerThread, SIGNAL(online()),
+            this, SLOT(getDB()));
+    onlinePollerThread->start();
+}
+
+void SelectRemoteDlg::showEvent(QShowEvent *event)
+{
+    refreshDB();
+    QDialog::showEvent(event);
+}
+
+void SelectRemoteDlg::onDBError(int error)
+{
+    this->setWindowTitle(tr("Network error") + " " + QString::number(error));
+    setBusy(false);
+    downloadBtn->setVisible(false);
+}
+