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>
30 QListWidgetItem asyncTestItem(const QListWidgetItem &item)
33 QListWidgetItem item2= item;
34 item2.setText("boby");
43 AccountDialog::AccountDialog(QWidget *parent) :
45 ui(new Ui::AccountDialog)
49 mFuturWatcher = new QFutureWatcher<QListWidgetItem>(this);
50 connect(mFuturWatcher,SIGNAL(resultReadyAt(int)),this,SLOT(setAsyncItem(int)));
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()));
62 AccountDialog::~AccountDialog()
67 void AccountDialog::add()
70 NewAccountDialog *dialog = new NewAccountDialog(this);
74 QString AccountDialog::currentIp()
78 QString useKey = settings.value("config/currentKey").toString();
80 if ( !useKey.isEmpty())
81 useIp = settings.value("account/"+useKey).toString();
83 else return QString();
88 void AccountDialog::load()
90 ui->editButton->setEnabled(false);
91 ui->remButton->setEnabled(false);
92 ui->useButton->setEnabled(false);
93 ui->listWidget->clear(); // tjr effacer , sinon on rajoute
97 QString useKey = settings.value("config/currentKey").toString();
99 QList <QListWidgetItem> asycItems;
101 settings.beginGroup("account");
102 foreach ( QString key, settings.allKeys())
104 QListWidgetItem * item = new QListWidgetItem;
107 item->setData(Qt::UserRole, settings.value(key));
109 QFont font = item->font();
113 ui->listWidget->addItem(item);
114 asycItems.append(*item);
119 // ... create and add in the list widget
121 // QFuture<QListWidgetItem> itemFutur = QtConcurrent::mapped(asycItems, asyncTestItem);
124 mFuturWatcher->setFuture(QtConcurrent::mapped(asycItems, asyncTestItem));
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(1000))
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();
200 settings.setValue("config/currentKey", currentKey);
205 void AccountDialog::enableUi()
207 ui->editButton->setEnabled(true);
208 ui->remButton->setEnabled(true);
209 ui->useButton->setEnabled(true);