Began refactoring settings handling.
[vlc-remote] / 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
28
29
30 QListWidgetItem asyncTestItem(const QListWidgetItem &item)
31 {
32
33     QListWidgetItem item2= item;
34     item2.setText("boby");
35
36     return item;
37
38 }
39
40
41
42
43 AccountDialog::AccountDialog(QWidget *parent) :
44         QDialog(parent),
45         ui(new Ui::AccountDialog)
46 {
47     ui->setupUi(this);
48
49     mFuturWatcher = new QFutureWatcher<QListWidgetItem>(this);
50     connect(mFuturWatcher,SIGNAL(resultReadyAt(int)),this,SLOT(setAsyncItem(int)));
51
52     connect(ui->addButton,SIGNAL(clicked()),this,SLOT(add()));
53     connect(ui->editButton,SIGNAL(clicked()),this,SLOT(edit()));
54     connect(ui->remButton,SIGNAL(clicked()),this,SLOT(rem()));
55     connect(ui->useButton,SIGNAL(clicked()),this,SLOT(use()));
56     connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(enableUi()));
57
58     load();
59
60 }
61
62 AccountDialog::~AccountDialog()
63 {
64     delete ui;
65 }
66
67 void AccountDialog::add()
68 {
69
70     NewAccountDialog *dialog = new NewAccountDialog(this);
71     dialog->exec();
72     load();
73 }
74 QString AccountDialog::currentIp()
75 {
76
77     QSettings settings;
78     QString useKey = settings.value("config/currentKey").toString();
79     QString useIp ;
80     if ( !useKey.isEmpty())
81         useIp = settings.value("account/"+useKey).toString();
82
83     else return QString();
84
85     return useIp;
86 }
87
88 void AccountDialog::load()
89 {
90     ui->editButton->setEnabled(false);
91     ui->remButton->setEnabled(false);
92     ui->useButton->setEnabled(false);
93     ui->listWidget->clear(); // tjr effacer , sinon on rajoute
94
95     QSettings settings;
96
97     QString useKey = settings.value("config/currentKey").toString();
98
99     QList <QListWidgetItem> asycItems;
100
101     settings.beginGroup("account");
102     foreach ( QString key, settings.allKeys())
103     {
104         QListWidgetItem * item = new QListWidgetItem;
105
106         item->setText(key);
107         item->setData(Qt::UserRole, settings.value(key));
108         if (useKey == key) {
109             QFont font = item->font();
110             font.setBold(true);
111             item->setFont(font);
112         }
113         ui->listWidget->addItem(item);
114         asycItems.append(*item);
115     }
116     settings.endGroup();    
117
118
119     // ... create and add in the list widget
120
121     //  QFuture<QListWidgetItem> itemFutur = QtConcurrent::mapped(asycItems, asyncTestItem);
122
123
124     mFuturWatcher->setFuture(QtConcurrent::mapped(asycItems, asyncTestItem));
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(1000))
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     QSettings settings;
200     settings.setValue("config/currentKey", currentKey);
201     load();
202     emit accept();
203 }
204
205 void AccountDialog::enableUi()
206 {
207     ui->editButton->setEnabled(true);
208     ui->remButton->setEnabled(true);
209     ui->useButton->setEnabled(true);
210 }