File: kbsprojectwizard.cpp

package info (click to toggle)
kboincspy 0.9.1-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,880 kB
  • ctags: 3,964
  • sloc: cpp: 29,018; sh: 9,736; perl: 2,793; makefile: 472; xml: 119
file content (388 lines) | stat: -rw-r--r-- 11,785 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/***************************************************************************
 *   Copyright (C) 2006 by Roberto Virga                                   *
 *   rvirga@users.sourceforge.net                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <qlabel.h>
#include <qlayout.h>
#include <qradiobutton.h>
#include <qvalidator.h>
#include <qtextbrowser.h>
#include <qwidgetstack.h>

#include <kapplication.h>
#include <kiconloader.h>
#include <klineedit.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpassdlg.h>
#include <kpushbutton.h>
#include <kstandarddirs.h>

#include <kbsboincdata.h>
#include <kbsboincmonitor.h>
#include <kbsrpcmonitor.h>

#include "kbsprojectwizard.h"

KBSProjectWizard::KBSProjectWizard(KBSBOINCMonitor *monitor, QWidget *parent, const char *name)
                : KDialog(parent, name), m_config(NULL), m_monitor(monitor)
{
  setupMonitor();
  setupView();
}

KBSProjectWizard::~KBSProjectWizard()
{
  if(NULL != m_config) delete m_config;
}

KBSBOINCMonitor *KBSProjectWizard::monitor() const
{
  return m_monitor;
}

int KBSProjectWizard::previous(int page) const
{
  switch(page) {
    case ProjectURL:
      return Welcome;
    case Account:
    case AccountKey:
      return ProjectURL;
    case ConnectError:
    case NetworkError:
    case AuthError:
    case DuplicateError:
    case GenericError:
      return m_legacy ? AccountKey
           : (NULL == m_config) ? ProjectURL
           : m_config->client_account_creation_disabled ? AccountKey
           : Account;
    default:
      return NoPage;
  }
}

int KBSProjectWizard::next(int page) const
{
  switch(page) {
    case Welcome:
      return ProjectURL;
    case ProjectURL:
      return !isValid(ProjectURL) ? NoPage
           : m_legacy ? AccountKey
           : ConnectHost;
    case Account:
      return isValid(Account) ? ConnectHost : NoPage;
    case AccountKey:
      return isValid(AccountKey) ? ConnectHost : NoPage;
    default:
      return NoPage;
  }
}

bool KBSProjectWizard::isTerminal(int page) const
{
  return page >= Success;
}

void KBSProjectWizard::reject()
{
  if(!isTerminal(m_page))
  {
    const QString warning = i18n("Do you really want to cancel?");
    if(KMessageBox::warningYesNo(this, warning) != KMessageBox::Yes)
      return;
  }
  
  KDialog::reject();
}

void KBSProjectWizard::setupMonitor()
{
  m_legacy = true;
  m_config = NULL;
  
  const KBSBOINCClientState *state = m_monitor->state();
  if(NULL != state) m_legacy = (state->core_client.major_version < 5);
  
  KBSRPCMonitor *rpcMonitor = m_monitor->rpcMonitor();
  if(NULL == rpcMonitor) return;
  
  connect(rpcMonitor, SIGNAL(output(const KBSBOINCProjectConfig &)),
          this, SLOT(input(const KBSBOINCProjectConfig &)));
  connect(rpcMonitor, SIGNAL(output(const QString &, const QString &)),
          this, SLOT(input(const QString &, const QString &)));
  connect(rpcMonitor, SIGNAL(error(const QString &, int, QStringList)),
          this, SLOT(error(const QString &, int, QStringList)));
}

void KBSProjectWizard::setupView()
{
  (new QVBoxLayout(this))->setAutoAdd(true);
  m_view = new KBSProjectWizardContent(this);
  
  setCaption(m_view->caption());
  setMaximumSize(0, 0);
  
  m_view->sidebar->setPixmap(UserIcon("sidebar"));
  m_view->account_key->setValidator(new QRegExpValidator(QRegExp("[0-9a-f]{32}"), this));
  
  m_view->connect_host_movie->setPixmap(UserIcon("connect_host"));
  m_view->connect_network_movie->setPixmap(UserIcon("connect_network"));
  
  connect(m_view->help, SIGNAL(clicked()), this, SLOT(help()));
  connect(m_view->previous, SIGNAL(clicked()), this, SLOT(previous()));
  connect(m_view->next, SIGNAL(clicked()), this, SLOT(next()));
  connect(m_view->finish, SIGNAL(clicked()), this, SLOT(reject()));
  
  connect(m_view->project_url, SIGNAL(textChanged(const QString &)),
          this, SLOT(validateText(const QString &)));
  connect(m_view->username, SIGNAL(textChanged(const QString &)),
          this, SLOT(validateText(const QString &)));
  connect(m_view->password, SIGNAL(textChanged(const QString &)),
          this, SLOT(validateText(const QString &)));
  connect(m_view->confirm_password, SIGNAL(textChanged(const QString &)),
          this, SLOT(validateText(const QString &)));
  connect(m_view->account_key, SIGNAL(textChanged(const QString &)),
          this, SLOT(validateText(const QString &)));
  
  show(Welcome);
}

void KBSProjectWizard::show(int page)
{
  m_page = page;
  if(NoPage == m_page) return;
  
  m_view->previous->setEnabled(previous(m_page) != NoPage);
  m_view->next->setEnabled(next(m_page) != NoPage);
  m_view->finish->setText(isTerminal(m_page) ? i18n("&Finish") : i18n("&Cancel"));
  
  switch(m_page) {
    case ProjectURL:
      if(NULL != m_config) { delete m_config; m_config = NULL; }
      validateText(m_view->project_url->text());
      break;
    case Account:
      validateText(m_view->username->text().stripWhiteSpace());
      break;
    case AccountKey:
      validateText(m_view->account_key->text());
      break;
    default:
      break;
  }
  
  m_view->pages->raiseWidget(m_page);
}

bool KBSProjectWizard::isValid(int page) const
{
  return m_valid.contains(page) ? m_valid[page] : false;
}

QString KBSProjectWizard::project(const QString &projectName) const
{
  const KBSBOINCClientState *state = m_monitor->state();
  if(NULL == state) return QString::null;
  
  for(QMap<QString,KBSBOINCProject>::const_iterator project = state->project.begin();
      project != state->project.end(); ++project)
    if((*project).project_name == projectName)
      return project.key();
  
  return QString::null;
}

void KBSProjectWizard::input(const KBSBOINCProjectConfig &config)
{
  qDebug("input config");
  
  if(NULL != m_config) { delete m_config; m_config = NULL; }
  
  if(project(config.name).isNull()) {
    m_config = new KBSBOINCProjectConfig(config);
    show(m_config->client_account_creation_disabled ? AccountKey : Account);
  } else
    show(DuplicateError);
  
  if(m_config->account_creation_disabled) {
    m_view->create_account->setEnabled(false);
    m_view->use_account->setChecked(true);
  } else {
    m_view->create_account->setEnabled(true);
    m_view->create_account->setChecked(true);
  }
  if(m_config->uses_username)
    m_view->username_label->setText(i18n("Login:"));
  else
    m_view->username_label->setText(i18n("EMail:"));
}

void KBSProjectWizard::input(const QString &, const QString &account_key)
{
  qDebug("input account key");
  
  KURL url = KURL(m_view->project_url->text());
  m_monitor->rpcMonitor()->attachProject(url, account_key);
}

void KBSProjectWizard::error(const QString &tag, int num, QStringList messages)
{
  // ignore errors on polling commands for clients < 5.x.x
  if(m_legacy && tag.endsWith("_poll")) return;
  
  if(tag == "get_project_config_poll") {
    m_monitor->rpcMonitor()->lookupWebsite("google");
    show(ConnectNetwork);
  } else if(tag == "lookup_website_poll")
    show(0 == num ? ConnectError : NetworkError);
  else if(tag == "lookup_account_poll")
    switch(num) {
      case -161:
      case -205:
      case -206:
      case -207:
        show(AuthError);
        break;
      default:
        m_view->error_messages->setText(messages.join("\n"));
        show(GenericError);
        break;
    }
  else if(tag == "project_attach")
  {
    if(num < 0) {
      m_view->error_messages->setText(messages.join("\n"));
      show(GenericError);
    } else if(m_legacy)
      show(Success);
  }
  else if(tag == "project_attach_poll")
    switch(num) {
      case 0:
        m_monitor->checkFiles();
        show(Success);
        break;
      case -212:
        show(AuthError);
        break;
      default:
        m_view->error_messages->setText(messages.join("\n"));
        show(GenericError);
        break;
    }
  
  qDebug("RPC call %s returned error %d", tag.latin1(), num);
}

void KBSProjectWizard::help()
{
  kapp->invokeHelp("attach_project");
}

void KBSProjectWizard::previous()
{
  show(previous(m_page));
}

void KBSProjectWizard::next()
{
  switch(m_page) {
    case ProjectURL:
      if(!m_legacy)
      {
        const KURL project_url(m_view->project_url->text());
        
        m_monitor->rpcMonitor()->getProjectConfig(project_url);
      }
      break;
    case Account:
      {
        const KURL project_url(m_view->project_url->text());
        bool createAccount = m_view->create_account->isChecked();
        const QString email(m_config->uses_username ? "" : m_view->username->text().stripWhiteSpace()),
                      username(m_config->uses_username ? m_view->username->text().stripWhiteSpace() : ""),
                      password(m_view->password->password());
        
        if(createAccount)
          m_monitor->rpcMonitor()->createAccount(project_url, email, username, password);
        else
          m_monitor->rpcMonitor()->lookupAccount(project_url, email, username, password);
      }
      break;
    case AccountKey:
      {
        const KURL url(m_view->project_url->text());
        const QString account_key(m_view->account_key->text());
        
        m_monitor->rpcMonitor()->attachProject(url, account_key);
      }
      break;
    default:
      break;
  }
  
  show(next(m_page));
}

void KBSProjectWizard::validateText(const QString &value)
{
  switch(m_page) {
    case ProjectURL:
      {
        const KURL url(value);
        
        m_valid[m_page] = url.isValid() && !url.host().isEmpty();
      }
      break;
    case Account:
      {
        bool createAccount = m_view->create_account->isChecked(),
             validPasswords = false;
        const QString username = m_view->username->text(),
                      password = m_view->password->password(),
                      confirmPassword = m_view->confirm_password->password();
        
        if(password.isEmpty())
          m_view->remark->clear();
        else if(password.length() < m_config->min_passwd_length)
          m_view->remark->setText(i18n("Password is too short."));
        else if(createAccount && (password != confirmPassword))
          m_view->remark->setText(i18n("Passwords don't match."));
        else {
          m_view->remark->clear();
          validPasswords = true;
        }
        
        m_valid[m_page] = !username.isEmpty() && validPasswords;
      }
      break;
    case AccountKey:
      m_valid[m_page] = m_view->account_key->hasAcceptableInput();
      break;
    default:
      break;
  }
  
  m_view->next->setEnabled(m_valid[m_page]);
}

#include "kbsprojectwizard.moc"