Add mce-dev into control build templates for dbus support
[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
79 void AccountDialog::load()
80 {
81     ui->editButton->setEnabled(false);
82     ui->remButton->setEnabled(false);
83     ui->useButton->setEnabled(false);
84     ui->listWidget->clear(); // tjr effacer , sinon on rajoute
85
86     QSettings settings;
87
88     QString useKey = AppSettings::getCurrentKey(); // settings.value("config/currentKey").toString();
89
90     QList <QListWidgetItem> asycItems;
91
92     //settings.beginGroup("account");
93     foreach ( QString key, AppSettings::getAllAccounts()) //settings.allKeys())
94     {
95         QListWidgetItem * item = new QListWidgetItem;
96
97         item->setText(key);
98         item->setData(Qt::UserRole, settings.value(key));
99         if (useKey == key) {
100             QFont font = item->font();
101             font.setBold(true);
102             item->setFont(font);
103         }
104         ui->listWidget->addItem(item);
105         if (AppSettings::isConnected()) {
106             asycItems.append(*item);
107         }
108     }
109     //settings.endGroup();
110
111
112     // ... create and add in the list widget
113
114     //  QFuture<QListWidgetItem> itemFutur = QtConcurrent::mapped(asycItems, asyncTestItem);
115
116
117     if (AppSettings::isConnected()) {
118         mFuturWatcher->setFuture(QtConcurrent::mapped(asycItems, asyncTestItem));
119     }
120     else {
121 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
122         QMaemo5InformationBox::information(this, tr("No network connection available!"), QMaemo5InformationBox::DefaultTimeout);
123 #endif
124     }
125 }
126
127 QListWidgetItem AccountDialog::asyncTestItem(const QListWidgetItem& item)
128 {
129     //==========> NEED TO USE POINTER TO AVOID setAsyncItem! But I don't know how;..
130     QListWidgetItem newItem = item;
131
132     QTcpSocket * socket = new QTcpSocket;
133     QSettings settings;
134     QString host = settings.value("account/"+item.text()).toString();
135
136     if(host.contains(":"))
137     {
138         QStringList hostSplit = host.split(":");
139         QString ip   = hostSplit.at(0);
140         QString port = hostSplit.at(1);
141         socket->connectToHost(ip,port.toInt());
142     }
143
144     else
145         socket->connectToHost(host,8080);
146
147     if (socket->waitForConnected(AppSettings::getPingTimeout()))
148         newItem.setIcon(QIcon::fromTheme("widgets_tickmark_list"));
149     else
150         newItem.setIcon(QIcon::fromTheme("statusarea_presence_busy_error"));
151
152     return newItem;
153
154
155 }
156
157
158
159 void AccountDialog::setAsyncItem(int row)  // EDIT THE ROW AFTER ASYNC FUNCTION FINISHED
160 {
161     QListWidgetItem newItem = mFuturWatcher->resultAt(row);
162     QListWidgetItem * item = ui->listWidget->item(row);
163
164     item->setIcon(newItem.icon());
165
166
167
168 }
169
170
171
172 void AccountDialog::edit()
173 {
174     QString currentIp = ui->listWidget->currentItem()->data(Qt::UserRole).toString();
175     QString currentKey = ui->listWidget->currentItem()->text();
176
177     NewAccountDialog *dialog = new NewAccountDialog(this);
178     dialog->edit(currentKey, currentIp);
179     dialog->exec();
180
181     load();
182 }
183
184 void AccountDialog::rem()
185 {
186     QString key = ui->listWidget->currentItem()->text();
187
188     QSettings settings;
189     settings.beginGroup("account");
190     settings.remove(key);
191     settings.endGroup();
192
193     load(); // On recharge les compte
194 }
195
196 void AccountDialog::use()
197 {
198     QString currentKey = ui->listWidget->currentItem()->text();
199     AppSettings::setCurrentKey(currentKey);
200     load();
201     emit accept();
202 }
203
204 void AccountDialog::enableUi()
205 {
206     ui->editButton->setEnabled(true);
207     ui->remButton->setEnabled(true);
208     ui->useButton->setEnabled(true);
209 }