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
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
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.
18 #include "accountdialog.h"
19 #include "ui_accountdialog.h"
20 #include "newaccountdialog.h"
22 #include <QInputDialog>
26 #include <QtConcurrentMap>
27 #include "appsettings.h"
28 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
29 #include <QMaemo5InformationBox>
34 QListWidgetItem asyncTestItem(const QListWidgetItem &item)
37 QListWidgetItem item2= item;
38 item2.setText("boby");
47 AccountDialog::AccountDialog(QWidget *parent) :
49 ui(new Ui::AccountDialog)
53 mFuturWatcher = new QFutureWatcher<QListWidgetItem>(this);
54 connect(mFuturWatcher,SIGNAL(resultReadyAt(int)),this,SLOT(setAsyncItem(int)));
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()));
66 AccountDialog::~AccountDialog()
71 void AccountDialog::add()
74 NewAccountDialog *dialog = new NewAccountDialog(this);
78 QString AccountDialog::currentIp()
82 QString useKey = settings.value("config/currentKey").toString();
84 if ( !useKey.isEmpty())
85 useIp = settings.value("account/"+useKey).toString();
87 else return QString();
92 void AccountDialog::load()
94 ui->editButton->setEnabled(false);
95 ui->remButton->setEnabled(false);
96 ui->useButton->setEnabled(false);
97 ui->listWidget->clear(); // tjr effacer , sinon on rajoute
101 QString useKey = settings.value("config/currentKey").toString();
103 QList <QListWidgetItem> asycItems;
105 settings.beginGroup("account");
106 foreach ( QString key, settings.allKeys())
108 QListWidgetItem * item = new QListWidgetItem;
111 item->setData(Qt::UserRole, settings.value(key));
113 QFont font = item->font();
117 ui->listWidget->addItem(item);
118 if (AppSettings::isConnected()) {
119 asycItems.append(*item);
125 // ... create and add in the list widget
127 // QFuture<QListWidgetItem> itemFutur = QtConcurrent::mapped(asycItems, asyncTestItem);
130 if (AppSettings::isConnected()) {
131 mFuturWatcher->setFuture(QtConcurrent::mapped(asycItems, asyncTestItem));
134 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
135 QMaemo5InformationBox::information(this, tr("No network connection available!"), QMaemo5InformationBox::DefaultTimeout);
140 QListWidgetItem AccountDialog::asyncTestItem(const QListWidgetItem& item)
142 //==========> NEED TO USE POINTER TO AVOID setAsyncItem! But I don't know how;..
143 QListWidgetItem newItem = item;
145 QTcpSocket * socket = new QTcpSocket;
147 QString host = settings.value("account/"+item.text()).toString();
149 if(host.contains(":"))
151 QStringList hostSplit = host.split(":");
152 QString ip = hostSplit.at(0);
153 QString port = hostSplit.at(1);
154 socket->connectToHost(ip,port.toInt());
158 socket->connectToHost(host,8080);
160 if (socket->waitForConnected(1000))
161 newItem.setIcon(QIcon::fromTheme("widgets_tickmark_list"));
163 newItem.setIcon(QIcon::fromTheme("statusarea_presence_busy_error"));
172 void AccountDialog::setAsyncItem(int row) // EDIT THE ROW AFTER ASYNC FUNCTION FINISHED
174 QListWidgetItem newItem = mFuturWatcher->resultAt(row);
175 QListWidgetItem * item = ui->listWidget->item(row);
177 item->setIcon(newItem.icon());
185 void AccountDialog::edit()
187 QString currentIp = ui->listWidget->currentItem()->data(Qt::UserRole).toString();
188 QString currentKey = ui->listWidget->currentItem()->text();
190 NewAccountDialog *dialog = new NewAccountDialog(this);
191 dialog->edit(currentKey, currentIp);
197 void AccountDialog::rem()
199 QString key = ui->listWidget->currentItem()->text();
202 settings.beginGroup("account");
203 settings.remove(key);
206 load(); // On recharge les compte
209 void AccountDialog::use()
211 QString currentKey = ui->listWidget->currentItem()->text();
213 settings.setValue("config/currentKey", currentKey);
218 void AccountDialog::enableUi()
220 ui->editButton->setEnabled(true);
221 ui->remButton->setEnabled(true);
222 ui->useButton->setEnabled(true);