Moved timeout constants to appsettings in preparatino for adding to user configuratio...
[vlc-remote] / src / accountdialog.cpp
1 /*   VLC-REMOTE for MAEMO 5
2 *   Copyright (C) 2010 Schutz Sacha <istdasklar@gmail.com>, Dru Moore <usr@dru-id.co.uk>, Yann Nave <yannux@onbebop.net>
3 *   This program is free software; you can redistribute it and/or modify
4 *   it under the terms of the GNU General Public License version 2,
5 *   or (at your option) any later version, as published by the Free
6 *   Software Foundation
7 *
8 *   This program is distributed in the hope that it will be useful,
9 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *   GNU General Public License for more details
12 *
13 *   You should have received a copy of the GNU General Public
14 *   License along with this program; if not, write to the
15 *   Free Software Foundation, Inc.,
16 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 */
18 #include "accountdialog.h"
19 #include "ui_accountdialog.h"
20 #include "newaccountdialog.h"
21 #include <QDebug>
22 #include <QInputDialog>
23 #include <QSettings>
24 #include <QTcpSocket>
25 #include <QFuture>
26 #include <QtConcurrentMap>
27 #include "appsettings.h"
28 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
29 #include <QMaemo5InformationBox>
30 #endif
31
32
33
34 QListWidgetItem asyncTestItem(const QListWidgetItem &item)
35 {
36
37     QListWidgetItem item2= item;
38     item2.setText("boby");
39
40     return item;
41
42 }
43
44
45
46
47 AccountDialog::AccountDialog(QWidget *parent) :
48         QDialog(parent),
49         ui(new Ui::AccountDialog)
50 {
51     ui->setupUi(this);
52
53     mFuturWatcher = new QFutureWatcher<QListWidgetItem>(this);
54     connect(mFuturWatcher,SIGNAL(resultReadyAt(int)),this,SLOT(setAsyncItem(int)));
55
56     connect(ui->addButton,SIGNAL(clicked()),this,SLOT(add()));
57     connect(ui->editButton,SIGNAL(clicked()),this,SLOT(edit()));
58     connect(ui->remButton,SIGNAL(clicked()),this,SLOT(rem()));
59     connect(ui->useButton,SIGNAL(clicked()),this,SLOT(use()));
60     connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(enableUi()));
61
62     load();
63
64 }
65
66 AccountDialog::~AccountDialog()
67 {
68     delete ui;
69 }
70
71 void AccountDialog::add()
72 {
73
74     NewAccountDialog *dialog = new NewAccountDialog(this);
75     dialog->exec();
76     load();
77 }
78 QString AccountDialog::currentIp()
79 {
80
81     QSettings settings;
82     QString useKey = settings.value("config/currentKey").toString();
83     QString useIp ;
84     if ( !useKey.isEmpty())
85         useIp = settings.value("account/"+useKey).toString();
86
87     else return QString();
88
89     return useIp;
90 }
91
92 void AccountDialog::load()
93 {
94     ui->editButton->setEnabled(false);
95     ui->remButton->setEnabled(false);
96     ui->useButton->setEnabled(false);
97     ui->listWidget->clear(); // tjr effacer , sinon on rajoute
98
99     QSettings settings;
100
101     QString useKey = settings.value("config/currentKey").toString();
102
103     QList <QListWidgetItem> asycItems;
104
105     settings.beginGroup("account");
106     foreach ( QString key, settings.allKeys())
107     {
108         QListWidgetItem * item = new QListWidgetItem;
109
110         item->setText(key);
111         item->setData(Qt::UserRole, settings.value(key));
112         if (useKey == key) {
113             QFont font = item->font();
114             font.setBold(true);
115             item->setFont(font);
116         }
117         ui->listWidget->addItem(item);
118         if (AppSettings::isConnected()) {
119             asycItems.append(*item);
120         }
121     }
122     settings.endGroup();    
123
124
125     // ... create and add in the list widget
126
127     //  QFuture<QListWidgetItem> itemFutur = QtConcurrent::mapped(asycItems, asyncTestItem);
128
129
130     if (AppSettings::isConnected()) {
131         mFuturWatcher->setFuture(QtConcurrent::mapped(asycItems, asyncTestItem));
132     }
133     else {
134 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
135         QMaemo5InformationBox::information(this, tr("No network connection available!"), QMaemo5InformationBox::DefaultTimeout);
136 #endif
137     }
138 }
139
140 QListWidgetItem AccountDialog::asyncTestItem(const QListWidgetItem& item)
141 {
142     //==========> NEED TO USE POINTER TO AVOID setAsyncItem! But I don't know how;..
143     QListWidgetItem newItem = item;
144
145     QTcpSocket * socket = new QTcpSocket;
146     QSettings settings;
147     QString host = settings.value("account/"+item.text()).toString();
148
149     if(host.contains(":"))
150     {
151         QStringList hostSplit = host.split(":");
152         QString ip   = hostSplit.at(0);
153         QString port = hostSplit.at(1);
154         socket->connectToHost(ip,port.toInt());
155     }
156
157     else
158         socket->connectToHost(host,8080);
159
160     if (socket->waitForConnected(AppSettings::getPingTimeout()))
161         newItem.setIcon(QIcon::fromTheme("widgets_tickmark_list"));
162     else
163         newItem.setIcon(QIcon::fromTheme("statusarea_presence_busy_error"));
164
165     return newItem;
166
167
168 }
169
170
171
172 void AccountDialog::setAsyncItem(int row)  // EDIT THE ROW AFTER ASYNC FUNCTION FINISHED
173 {
174     QListWidgetItem newItem = mFuturWatcher->resultAt(row);
175     QListWidgetItem * item = ui->listWidget->item(row);
176
177     item->setIcon(newItem.icon());
178
179
180
181 }
182
183
184
185 void AccountDialog::edit()
186 {
187     QString currentIp = ui->listWidget->currentItem()->data(Qt::UserRole).toString();
188     QString currentKey = ui->listWidget->currentItem()->text();
189
190     NewAccountDialog *dialog = new NewAccountDialog(this);
191     dialog->edit(currentKey, currentIp);
192     dialog->exec();
193
194     load();
195 }
196
197 void AccountDialog::rem()
198 {
199     QString key = ui->listWidget->currentItem()->text();
200
201     QSettings settings;
202     settings.beginGroup("account");
203     settings.remove(key);
204     settings.endGroup();
205
206     load(); // On recharge les compte
207 }
208
209 void AccountDialog::use()
210 {
211     QString currentKey = ui->listWidget->currentItem()->text();
212     QSettings settings;
213     settings.setValue("config/currentKey", currentKey);
214     load();
215     emit accept();
216 }
217
218 void AccountDialog::enableUi()
219 {
220     ui->editButton->setEnabled(true);
221     ui->remButton->setEnabled(true);
222     ui->useButton->setEnabled(true);
223 }