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);
79 void AccountDialog::load()
81 ui->editButton->setEnabled(false);
82 ui->remButton->setEnabled(false);
83 ui->useButton->setEnabled(false);
84 ui->listWidget->clear(); // tjr effacer , sinon on rajoute
88 QString useKey = AppSettings::getCurrentKey(); // settings.value("config/currentKey").toString();
90 QList <QListWidgetItem> asycItems;
92 //settings.beginGroup("account");
93 foreach ( QString key, AppSettings::getAllAccounts()) //settings.allKeys())
95 QListWidgetItem * item = new QListWidgetItem;
98 item->setData(Qt::UserRole, settings.value(key));
100 QFont font = item->font();
104 ui->listWidget->addItem(item);
105 if (AppSettings::isConnected()) {
106 asycItems.append(*item);
109 //settings.endGroup();
112 // ... create and add in the list widget
114 // QFuture<QListWidgetItem> itemFutur = QtConcurrent::mapped(asycItems, asyncTestItem);
117 if (AppSettings::isConnected()) {
118 mFuturWatcher->setFuture(QtConcurrent::mapped(asycItems, asyncTestItem));
121 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
122 QMaemo5InformationBox::information(this, tr("No network connection available!"), QMaemo5InformationBox::DefaultTimeout);
127 QListWidgetItem AccountDialog::asyncTestItem(const QListWidgetItem& item)
129 //==========> NEED TO USE POINTER TO AVOID setAsyncItem! But I don't know how;..
130 QListWidgetItem newItem = item;
132 QTcpSocket * socket = new QTcpSocket;
134 QString host = settings.value("account/"+item.text()).toString();
136 if(host.contains(":"))
138 QStringList hostSplit = host.split(":");
139 QString ip = hostSplit.at(0);
140 QString port = hostSplit.at(1);
141 socket->connectToHost(ip,port.toInt());
145 socket->connectToHost(host,8080);
147 if (socket->waitForConnected(AppSettings::getPingTimeout()))
148 newItem.setIcon(QIcon::fromTheme("widgets_tickmark_list"));
150 newItem.setIcon(QIcon::fromTheme("statusarea_presence_busy_error"));
159 void AccountDialog::setAsyncItem(int row) // EDIT THE ROW AFTER ASYNC FUNCTION FINISHED
161 QListWidgetItem newItem = mFuturWatcher->resultAt(row);
162 QListWidgetItem * item = ui->listWidget->item(row);
164 item->setIcon(newItem.icon());
172 void AccountDialog::edit()
174 QString currentIp = ui->listWidget->currentItem()->data(Qt::UserRole).toString();
175 QString currentKey = ui->listWidget->currentItem()->text();
177 NewAccountDialog *dialog = new NewAccountDialog(this);
178 dialog->edit(currentKey, currentIp);
184 void AccountDialog::rem()
186 QString key = ui->listWidget->currentItem()->text();
189 settings.beginGroup("account");
190 settings.remove(key);
193 load(); // On recharge les compte
196 void AccountDialog::use()
198 QString currentKey = ui->listWidget->currentItem()->text();
199 AppSettings::setCurrentKey(currentKey);
204 void AccountDialog::enableUi()
206 ui->editButton->setEnabled(true);
207 ui->remButton->setEnabled(true);
208 ui->useButton->setEnabled(true);