vkwindow.cpp 24.8 KB
Newer Older
1
2
3
/* ============================================================
 *
 * This file is a part of kipi-plugins project
4
 * http://www.digikam.org
5
6
7
8
9
10
11
12
 *
 * Date        : 2010-11-15
 * Description : a kipi plugin to export images to VKontakte web service
 *
 * Copyright (C) 2011 by Alexander Potashev <aspotashev at gmail dot com>
 *
 * GUI based on Yandex.Fotki KIPI Plugin
 * Copyright (C) 2005-2008 by Vardhman Jain <vardhman at gmail dot com>
Gilles Caulier's avatar
Gilles Caulier committed
13
 * Copyright (C) 2008-2012 by Gilles Caulier <caulier dot gilles at gmail dot com>
Gilles Caulier's avatar
Gilles Caulier committed
14
15
 * Copyright (C) 2009      by Luka Renko <lure at kubuntu dot org>
 * Copyright (C) 2010      by Roman Tsisyk <roman at tsisyk dot com>
16
17
18
19
20
21
22
23
24
25
26
27
28
 *
 * 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, 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.
 *
 * ============================================================ */

Gilles Caulier's avatar
Gilles Caulier committed
29
#include "vkwindow.moc"
30
31
32

// Qt includes

smit mehta's avatar
Polish    
smit mehta committed
33
34
35
36
37
38
39
40
41
42
43
44
#include <QFileInfo>
#include <QCheckBox>
#include <QGroupBox>
#include <QCloseEvent>
#include <QButtonGroup>
#include <QLabel>
#include <QSpinBox>
#include <QRadioButton>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QToolButton>
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

// KDE includes

#include <kdeversion.h>
#include <kde_file.h>
#include <kdebug.h>
#include <kconfig.h>
#include <klocale.h>
#include <kdialog.h>
#include <klineedit.h>
#include <kcombobox.h>
#include <kpushbutton.h>
#include <kmenu.h>
#include <khelpmenu.h>
#include <kmessagebox.h>
#include <kpassworddialog.h>
#include <kprogressdialog.h>
#include <kstandarddirs.h>
#include <ktoolinvocation.h>
#include <kio/renamedialog.h>

// libkvkontakte includes

#include <libkvkontakte/getapplicationpermissionsjob.h>
#include <libkvkontakte/authenticationdialog.h>
#include <libkvkontakte/albumlistjob.h>
#include <libkvkontakte/createalbumjob.h>
Alexander Potashev's avatar
Alexander Potashev committed
72
#include <libkvkontakte/editalbumjob.h>
73
#include <libkvkontakte/deletealbumjob.h>
74
#include <libkvkontakte/uploadphotosjob.h>
75
#include <libkvkontakte/getvariablejob.h>
76
77
78
79
80
81
82
83
84
85

// LibKDcraw includes

#include <libkdcraw/version.h>
#include <libkdcraw/kdcraw.h>

// LibKIPI includes

#include <libkipi/interface.h>
#include <libkipi/uploadwidget.h>
Gilles Caulier's avatar
compile    
Gilles Caulier committed
86
#include <libkipi/imagecollection.h>
87
88
89

// Local includes

Gilles Caulier's avatar
compile    
Gilles Caulier committed
90
#include "kpaboutdata.h"
Gilles Caulier's avatar
Gilles Caulier committed
91
#include "kpversion.h"
Gilles Caulier's avatar
Gilles Caulier committed
92
#include "kpimageslist.h"
93
#include "vkalbumdialog.h"
smit mehta's avatar
Polish    
smit mehta committed
94
#include "kpprogresswidget.h"
95

96
97
98
99
100
101
102
103
104
105
#define SLOT_JOB_DONE_INIT(JobClass) \
    JobClass *job = dynamic_cast<JobClass *>(kjob); \
    Q_ASSERT(job);          \
    m_jobs.removeAll(job);  \
    if (job->error())       \
    {                       \
        handleVkError(job); \
        return;             \
    }

106
107
108
namespace KIPIVkontaktePlugin
{

Gilles Caulier's avatar
Gilles Caulier committed
109
VkontakteWindow::VkontakteWindow(bool import, QWidget* const parent)
Gilles Caulier's avatar
Gilles Caulier committed
110
    : KPToolDialog(parent)
111
112
{
    m_authenticated = false;
113
114
115

    // read settings from file
    readSettings();
Gilles Caulier's avatar
Gilles Caulier committed
116

Gilles Caulier's avatar
Gilles Caulier committed
117
118
    connect(this, SIGNAL(finished()), 
            this, SLOT(slotFinished()));
119

Gilles Caulier's avatar
Gilles Caulier committed
120
    m_import                = import;
Gilles Caulier's avatar
Gilles Caulier committed
121
122
    m_mainWidget            = new QWidget(this);
    QHBoxLayout* mainLayout = new QHBoxLayout(m_mainWidget);
Gilles Caulier's avatar
Gilles Caulier committed
123
    m_imgList               = new KPImagesList(this);
Gilles Caulier's avatar
Gilles Caulier committed
124
    m_imgList->setControlButtonsPlacement(KPImagesList::ControlButtonsBelow);
125
126
127
128
129
    m_imgList->setAllowRAW(false); // TODO: implement conversion
    m_imgList->loadImagesFromCurrentSelection();
    m_imgList->listView()->setWhatsThis(
        i18n("This is the list of images to upload to your VKontakte album."));

Alexander Potashev's avatar
cleanup    
Alexander Potashev committed
130
131
    QWidget *settingsBox = new QWidget(this);
    QVBoxLayout *settingsBoxLayout = new QVBoxLayout(settingsBox);
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

    m_headerLabel = new QLabel(settingsBox);
    m_headerLabel->setWhatsThis(i18n("This is a clickable link to open the "
                                     "VKontakte service in a web browser."));
    m_headerLabel->setOpenExternalLinks(true);
    m_headerLabel->setFocusPolicy(Qt::NoFocus);

    /*
     * Account box
     */
    m_accountBox = new QGroupBox(i18n("Account"), settingsBox);
    m_accountBox->setWhatsThis(i18n("This account is used for authentication."));

    QGridLayout* accountBoxLayout = new QGridLayout(m_accountBox);
    QLabel* loginDescLabel = new QLabel(m_accountBox);
    loginDescLabel->setText(i18n("Name:"));
    loginDescLabel->setWhatsThis(i18n("Your VKontakte login"));

    m_loginLabel = new QLabel(m_accountBox);
    m_changeUserButton = new KPushButton(
        KGuiItem(i18n("Change Account"), "system-switch-user",
                 i18n("Change VKontakte account used to authenticate")), m_accountBox);
Alexander Potashev's avatar
Alexander Potashev committed
154
    m_changeUserButton->hide(); // changing account does not work anyway
155
156
157
158
159
160
161
162

    accountBoxLayout->addWidget(loginDescLabel, 0, 0);
    accountBoxLayout->addWidget(m_loginLabel, 0, 1);

    accountBoxLayout->addWidget(m_changeUserButton, 1, 1);
    accountBoxLayout->setSpacing(KDialog::spacingHint());
    accountBoxLayout->setMargin(KDialog::spacingHint());

Gilles Caulier's avatar
Gilles Caulier committed
163
164
    connect(m_changeUserButton, SIGNAL(clicked()), 
            this, SLOT(slotChangeUserClicked()));
165
166
167
168

    /*
     * Album box
     */
Alexander Potashev's avatar
Alexander Potashev committed
169
    m_albumsBox = new QGroupBox(i18nc("@title:group Header above controls for managing albums", "Album"), settingsBox);
170
171
    m_albumsBox->setWhatsThis(
        i18n("This is the VKontakte album that will be used for the transfer."));
Alexander Potashev's avatar
Alexander Potashev committed
172
    QVBoxLayout *albumsBoxLayout = new QVBoxLayout(m_albumsBox);
173
174
175
176
177
178
179
180
181
182
183

    m_albumsCombo = new KComboBox(m_albumsBox);
    m_albumsCombo->setEditable(false);

    m_newAlbumButton = new KPushButton(
        KGuiItem(i18n("New Album"), "list-add",
                 i18n("Create new VKontakte album")), m_albumsBox);
    m_reloadAlbumsButton = new KPushButton(
        KGuiItem(i18nc("reload albums list", "Reload"), "view-refresh",
                 i18n("Reload albums list")), m_albumsBox);

Alexander Potashev's avatar
Alexander Potashev committed
184
185
186
187
    m_editAlbumButton = new QToolButton(m_albumsBox);
    m_editAlbumButton->setToolTip(i18n("Edit selected album"));
    m_editAlbumButton->setEnabled(false);
    m_editAlbumButton->setIcon(KIcon("document-edit"));
188

189
190
191
192
193
    m_deleteAlbumButton = new QToolButton(m_albumsBox);
    m_deleteAlbumButton->setToolTip(i18n("Delete selected album"));
    m_deleteAlbumButton->setEnabled(false);
    m_deleteAlbumButton->setIcon(KIcon("edit-delete"));

Alexander Potashev's avatar
Alexander Potashev committed
194
195
196
197
    QWidget *currentAlbumWidget = new QWidget(m_albumsBox);
    QHBoxLayout *currentAlbumWidgetLayout = new QHBoxLayout(currentAlbumWidget);
    currentAlbumWidgetLayout->addWidget(m_albumsCombo);
    currentAlbumWidgetLayout->addWidget(m_editAlbumButton);
198
    currentAlbumWidgetLayout->addWidget(m_deleteAlbumButton);
199

Alexander Potashev's avatar
Alexander Potashev committed
200
201
202
203
204
    QWidget *albumButtons = new QWidget(m_albumsBox);
    QHBoxLayout *albumButtonsLayout = new QHBoxLayout(albumButtons);
    albumButtonsLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
    albumButtonsLayout->addWidget(m_newAlbumButton);
    albumButtonsLayout->addWidget(m_reloadAlbumsButton);
205

Alexander Potashev's avatar
Alexander Potashev committed
206
207
208
    albumsBoxLayout->addWidget(currentAlbumWidget);
    albumsBoxLayout->addWidget(albumButtons);

Gilles Caulier's avatar
Gilles Caulier committed
209
210
211
212
213
214
215
216
217
218
219
    connect(m_editAlbumButton, SIGNAL(clicked()), 
            this, SLOT(slotEditAlbumRequest()));

    connect(m_deleteAlbumButton, SIGNAL(clicked()),
            this, SLOT(slotDeleteAlbumRequest()));

    connect(m_newAlbumButton, SIGNAL(clicked()),
            this, SLOT(slotNewAlbumRequest()));

    connect(m_reloadAlbumsButton, SIGNAL(clicked()),
            this, SLOT(slotReloadAlbumsRequest()));
220
221
222

    // ------------------------------------------------------------------------

Gilles Caulier's avatar
Gilles Caulier committed
223
    QGroupBox* uploadBox         = new QGroupBox(i18n("Destination"), settingsBox);
Gilles Caulier's avatar
Gilles Caulier committed
224
    uploadBox->setWhatsThis(i18n("This is the location where VKontakte images will be downloaded."));
225
    QVBoxLayout* uploadBoxLayout = new QVBoxLayout(uploadBox);
Gilles Caulier's avatar
Gilles Caulier committed
226
    m_uploadWidget               = iface()->uploadWidget(uploadBox);
227
228
229
230
    uploadBoxLayout->addWidget(m_uploadWidget);

    // ------------------------------------------------------------------------

Alexander Potashev's avatar
Alexander Potashev committed
231
#if 0
232
    QGroupBox *optionsBox = new QGroupBox(i18n("Options"), settingsBox);
233
234
    optionsBox->setWhatsThis(
        i18n("These are options that will be applied to images before upload."));
Alexander Potashev's avatar
Alexander Potashev committed
235
#endif
236

237
238
//     m_checkKeepOriginal = new QCheckBox(i18n("Save in high resolution"), settingsBox); // store state in kipirc

Alexander Potashev's avatar
Alexander Potashev committed
239
//     QVBoxLayout *optionsBoxLayout = new QVBoxLayout(optionsBox);
240
241
//     optionsBoxLayout->addWidget(m_checkKeepOriginal);

smit mehta's avatar
Polish    
smit mehta committed
242
    m_progressBar = new KPProgressWidget(settingsBox);
243
244
245
246
247
248
249
250
251
252
    m_progressBar->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    m_progressBar->hide();

    /*
     * Layouts
     */
    settingsBoxLayout->addWidget(m_headerLabel);
    settingsBoxLayout->addWidget(m_accountBox);
    settingsBoxLayout->addWidget(m_albumsBox);
    settingsBoxLayout->addWidget(uploadBox);
Alexander Potashev's avatar
Alexander Potashev committed
253
//     settingsBoxLayout->addWidget(optionsBox);
254
    settingsBoxLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
255
256
257
258
259
260
261
262
263
264
265
    settingsBoxLayout->addWidget(m_progressBar);
    settingsBoxLayout->setSpacing(KDialog::spacingHint());
    settingsBoxLayout->setMargin(KDialog::spacingHint());

    mainLayout->addWidget(m_imgList);
    mainLayout->addWidget(settingsBox);
    mainLayout->setSpacing(KDialog::spacingHint());
    mainLayout->setMargin(0);

    setMainWidget(m_mainWidget);
    setWindowIcon(KIcon("vkontakte"));
Alexander Potashev's avatar
cleanup    
Alexander Potashev committed
266
    setButtons(KDialog::Help | KDialog::User1 | KDialog::Close);
267
268
269
270
271
    setDefaultButton(Close);
    setModal(false);

    if (!m_import)
    {
Alexander Potashev's avatar
Alexander Potashev committed
272
        setWindowTitle(i18nc("@title:window", "Export to VKontakte Web Service"));
273
274
275
276
277
278
279
280
281
282
283
        setButtonGuiItem(KDialog::User1,
                         KGuiItem(i18n("Start Upload"), "network-workgroup",
                                  i18n("Start upload to VKontakte service")));
        setMinimumSize(700, 520);
        uploadBox->hide();
    }
    else
    {
        // TODO: import support
        m_imgList->hide();
        m_newAlbumButton->hide();
Alexander Potashev's avatar
Alexander Potashev committed
284
//         optionsBox->hide();
285
286
    }

Gilles Caulier's avatar
Gilles Caulier committed
287
288
289
290
291
292
293
294
295
296
    KPAboutData* about = new KPAboutData(ki18n("VKontakte Plugin"),
                                         0,
                                         KAboutData::License_GPL,
                                         ki18n("A Kipi plugin to export image collections to "
                                               "VKontakte web service."),
                                         ki18n("(c) 2007-2009, Vardhman Jain\n"
                                               "(c) 2008-2010, Gilles Caulier\n"
                                               "(c) 2009, Luka Renko\n"
                                               "(c) 2010, Roman Tsisyk\n"
                                               "(c) 2011, Alexander Potashev"));
297

Alexander Potashev's avatar
cleanup    
Alexander Potashev committed
298
    about->addAuthor(ki18n("Alexander Potashev"), ki18n("Author"), "aspotashev@gmail.com");
299

300
    about->setHandbookEntry("VKontakte");
Gilles Caulier's avatar
Gilles Caulier committed
301
    setAboutData(about);
302
303
304
305

    /*
     * UI slots
     */
Gilles Caulier's avatar
Gilles Caulier committed
306
307
308
309
310
    connect(this, SIGNAL(user1Clicked()),
            this, SLOT(slotStartTransfer()));

    connect(this, SIGNAL(signalAuthenticationDone()),
            this, SLOT(startAlbumsUpdate()));
311

Gilles Caulier's avatar
Gilles Caulier committed
312
313
    connect(this, SIGNAL(signalAuthenticationDone()),
            this, SLOT(startGetFullName()));
314

Gilles Caulier's avatar
Gilles Caulier committed
315
316
    connect(this, SIGNAL(signalAuthenticationDone()),
            this, SLOT(startGetUserId()));
317
318

    // for startReactivation()
Gilles Caulier's avatar
Gilles Caulier committed
319
320
    connect(this, SIGNAL(signalAuthenticationDone()),
            this, SLOT(show()));
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
}

VkontakteWindow::~VkontakteWindow()
{
    reset();
}

void VkontakteWindow::startReactivation()
{
    m_imgList->loadImagesFromCurrentSelection();

    reset();
    startAuthentication(false); // show() will be called after that
}

void VkontakteWindow::reset()
{
    //m_talker.reset();
    updateControls(true);
    updateLabels();
}

void VkontakteWindow::updateControls(bool val)
{
    if (val)
    {
        if (isAuthenticated())
        {
            m_albumsBox->setEnabled(true);
            enableButton(User1, true);
        }
        else
        {
            m_albumsBox->setEnabled(false);
            enableButton(User1, false);
        }

358
        //m_changeUserButton->setEnabled(true); // does not work anyway
359
360
361
362
363
364
365
366
367
368
        setCursor(Qt::ArrowCursor);

        setButtonGuiItem(KDialog::Close,
                         KGuiItem(i18n("Close"), "dialog-close",
                                  i18n("Close window")));
    }
    else
    {
        setCursor(Qt::WaitCursor);
        m_albumsBox->setEnabled(false);
369
        //m_changeUserButton->setEnabled(false); // does not work anyway
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
        enableButton(User1, false);

        setButtonGuiItem(KDialog::Close,
                         KGuiItem(i18n("Cancel"), "dialog-cancel",
                                  i18n("Cancel current operation")));
    }
}

void VkontakteWindow::updateLabels()
{
    QString loginText;
    QString urlText;

    if (isAuthenticated())
    {
385
        loginText = m_userFullName;
386
387
388
389
390
391
392
393
        m_albumsBox->setEnabled(true);
    }
    else
    {
        loginText = i18n("Unauthorized");
        m_albumsCombo->clear();
    }

394
395
396
397
398
    if (isAuthenticated() && m_userId != -1)
        urlText = QString("http://vkontakte.ru/albums%1").arg(m_userId);
    else
        urlText = "http://vkontakte.ru/";

399
400
    m_loginLabel->setText(QString("<b>%1</b>").arg(loginText));
    m_headerLabel->setText(
Alexander Potashev's avatar
cleanup    
Alexander Potashev committed
401
        QString("<b><h2><a href=\"%1\"><font color=\"black\">%2</font></a></h2></b>")
402
403
404
            .arg(urlText).arg(i18n("VKontakte")));
}

405
406
407
408
409
410
411
412
413
Vkontakte::AlbumInfoPtr VkontakteWindow::currentAlbum()
{
    int index = m_albumsCombo->currentIndex();
    if (index >= 0)
        return m_albums.at(index);
    else
        return Vkontakte::AlbumInfoPtr();
}

414
415
416
417
418
419
420
421
422
423
void VkontakteWindow::selectAlbum(int aid)
{
    for (int i = 0; i < m_albums.size(); i ++)
        if (m_albums.at(i)->aid() == aid)
        {
            m_albumsCombo->setCurrentIndex(i);
            break;
        }
}

Gilles Caulier's avatar
Gilles Caulier committed
424
//---------------------------------------------------------------------------
425
426
427
428
429
430

void VkontakteWindow::readSettings()
{
    KConfig config("kipirc");
    KConfigGroup grp = config.group("VKontakte Settings");

Gilles Caulier's avatar
Gilles Caulier committed
431
432
    m_appId         = grp.readEntry("VkAppId", "2446321");
    m_accessToken   = grp.readEntry("AccessToken", "");
433
434
    m_albumToSelect = grp.readEntry("SelectedAlbumId", -1);
}
435

436
437
438
439
440
441
442
443
444
445
void VkontakteWindow::writeSettings()
{
    KConfig config("kipirc");
    KConfigGroup grp = config.group("VKontakte Settings");

    grp.writeEntry("VkAppId", m_appId);

    if (!m_accessToken.isEmpty())
        grp.writeEntry("AccessToken", m_accessToken);

446
    Vkontakte::AlbumInfoPtr album = currentAlbum();
Gilles Caulier's avatar
Gilles Caulier committed
447

448
    if (album.isNull())
449
        grp.deleteEntry("SelectedAlbumId");
450
451
    else
        grp.writeEntry("SelectedAlbumId", album->aid());
452
}
453

Gilles Caulier's avatar
Gilles Caulier committed
454
//---------------------------------------------------------------------------
455
456
457
458
459
460
461
462
463
464
465
466

QString VkontakteWindow::getDestinationPath() const
{
    return m_uploadWidget->selectedImageCollection().uploadPath().path();
}

void VkontakteWindow::slotChangeUserClicked()
{
    // force authenticate window
    startAuthentication(true);
}

467
void VkontakteWindow::slotFinished()
468
{
469
    writeSettings();
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
    reset();
}

void VkontakteWindow::slotButtonClicked(int button)
{
    switch (button)
    {
        case KDialog::User1:
            slotStartTransfer();
            break;
        case KDialog::Close:
            if (!isButtonEnabled(KDialog::User1))
            {
                //m_talker.cancel();
                updateControls(true);
                break;
            }
        default:
            KDialog::slotButtonClicked(button);
    }
}

Alexander Potashev's avatar
Alexander Potashev committed
492
void VkontakteWindow::startAuthentication(bool forceLogout)
493
{
Alexander Potashev's avatar
Alexander Potashev committed
494
    m_userFullName.clear();
495
    m_userId = -1;
Alexander Potashev's avatar
Alexander Potashev committed
496
    m_editAlbumButton->setEnabled(false);
497
    m_deleteAlbumButton->setEnabled(false);
498

Alexander Potashev's avatar
Alexander Potashev committed
499
    if (forceLogout)
Alexander Potashev's avatar
Alexander Potashev committed
500
        m_accessToken.clear();
501
502
503

    if (!m_accessToken.isEmpty())
    {
Gilles Caulier's avatar
Gilles Caulier committed
504
505
506
507
508
        Vkontakte::GetApplicationPermissionsJob* job = new Vkontakte::GetApplicationPermissionsJob(m_accessToken);

        connect(job, SIGNAL(result(KJob*)), 
                this, SLOT(slotApplicationPermissionCheckDone(KJob*)));

509
510
511
512
513
514
515
        m_jobs.append(job);
        job->start();
    }
    else
    {
        QStringList permissions;
        permissions << "photos" << "offline";
Alexander Potashev's avatar
Alexander Potashev committed
516
        Vkontakte::AuthenticationDialog *authDialog = new Vkontakte::AuthenticationDialog(this);
517
        authDialog->setAppId(m_appId);
518
        authDialog->setPermissions(permissions);
Gilles Caulier's avatar
Gilles Caulier committed
519

520
521
        connect(authDialog, SIGNAL(authenticated(QString)),
                this, SLOT(slotAuthenticationDialogDone(QString)));
Gilles Caulier's avatar
Gilles Caulier committed
522

523
524
        connect(authDialog, SIGNAL(canceled()),
                this, SLOT(slotAuthenticationDialogCanceled()));
Gilles Caulier's avatar
Gilles Caulier committed
525

526
527
528
529
        authDialog->start();
    }
}

Gilles Caulier's avatar
Gilles Caulier committed
530
void VkontakteWindow::slotApplicationPermissionCheckDone(KJob* kjob)
531
{
Gilles Caulier's avatar
Gilles Caulier committed
532
    Vkontakte::GetApplicationPermissionsJob* job = dynamic_cast<Vkontakte::GetApplicationPermissionsJob*>(kjob);
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
    Q_ASSERT(job);
    m_jobs.removeAll(job);
    if (job->error() || (job->permissions() & 4) != 4)
        startAuthentication(true);
    else
    {
        m_authenticated = true;
        emit signalAuthenticationDone();
    }
}

void VkontakteWindow::slotAuthenticationDialogCanceled()
{
}

void VkontakteWindow::slotAuthenticationDialogDone(const QString &accessToken)
{
    m_accessToken = accessToken;
    m_authenticated = true;
    emit signalAuthenticationDone();
}

Gilles Caulier's avatar
Gilles Caulier committed
555
//---------------------------------------------------------------------------
556
557
558

void VkontakteWindow::handleVkError(KJob *kjob)
{
559
    KMessageBox::error(this, kjob->errorText(), i18nc("@title:window", "Request to VKontakte failed"));
560
561
}

Gilles Caulier's avatar
Gilles Caulier committed
562
//---------------------------------------------------------------------------
Alexander Potashev's avatar
cleanup    
Alexander Potashev committed
563

564
565
void VkontakteWindow::startAlbumsUpdate()
{
Gilles Caulier's avatar
Gilles Caulier committed
566
567
568
569
570
    Vkontakte::AlbumListJob* job = new Vkontakte::AlbumListJob(m_accessToken);

    connect(job, SIGNAL(result(KJob*)), 
            this, SLOT(slotAlbumsUpdateDone(KJob*)));

571
572
573
574
575
576
    m_jobs.append(job);
    job->start();
}

void VkontakteWindow::slotAlbumsUpdateDone(KJob *kjob)
{
577
    SLOT_JOB_DONE_INIT(Vkontakte::AlbumListJob)
578
579
580

    m_albumsCombo->clear();
    m_albums = job->list();
Gilles Caulier's avatar
Gilles Caulier committed
581

Andi Clemens's avatar
Andi Clemens committed
582
    foreach(const Vkontakte::AlbumInfoPtr &album, m_albums)
583
        m_albumsCombo->addItem(KIcon("folder-image"), album->title());
584
585
586
587
588

    if (m_albumToSelect != -1)
    {
        selectAlbum(m_albumToSelect);
        m_albumToSelect = -1;
589
590
    }
    m_albumsCombo->setEnabled(true);
Alexander Potashev's avatar
Alexander Potashev committed
591

592

Alexander Potashev's avatar
Alexander Potashev committed
593
    if (!m_albums.empty())
594
    {
Alexander Potashev's avatar
Alexander Potashev committed
595
        m_editAlbumButton->setEnabled(true);
596
597
        m_deleteAlbumButton->setEnabled(true);
    }
Alexander Potashev's avatar
Alexander Potashev committed
598

599
600
601
    updateControls(true);
}

Gilles Caulier's avatar
Gilles Caulier committed
602
//---------------------------------------------------------------------------
603

604
605
void VkontakteWindow::startGetFullName()
{
Gilles Caulier's avatar
Gilles Caulier committed
606
607
608
609
610
    Vkontakte::GetVariableJob* job = new Vkontakte::GetVariableJob(m_accessToken, 1281);

    connect(job, SIGNAL(result(KJob*)),
            this, SLOT(slotGetFullNameDone(KJob*)));

611
612
613
614
615
616
    m_jobs.append(job);
    job->start();
}

void VkontakteWindow::slotGetFullNameDone(KJob *kjob)
{
617
    SLOT_JOB_DONE_INIT(Vkontakte::GetVariableJob)
618
619
620
621
622

    m_userFullName = job->variable().toString();
    updateLabels();
}

Gilles Caulier's avatar
Gilles Caulier committed
623
//---------------------------------------------------------------------------
624

625
626
void VkontakteWindow::startGetUserId()
{
Gilles Caulier's avatar
Gilles Caulier committed
627
628
629
630
631
    Vkontakte::GetVariableJob* job = new Vkontakte::GetVariableJob(m_accessToken, 1280);

    connect(job, SIGNAL(result(KJob*)),
            this, SLOT(slotGetUserIdDone(KJob*)));

632
633
634
635
636
637
    m_jobs.append(job);
    job->start();
}

void VkontakteWindow::slotGetUserIdDone(KJob *kjob)
{
638
    SLOT_JOB_DONE_INIT(Vkontakte::GetVariableJob)
639
640
641
642
643

    m_userId = job->variable().toInt();
    updateLabels();
}

Gilles Caulier's avatar
Gilles Caulier committed
644
//---------------------------------------------------------------------------
645

Alexander Potashev's avatar
Alexander Potashev committed
646
void VkontakteWindow::startAlbumCreation(Vkontakte::AlbumInfoPtr album)
647
{
Gilles Caulier's avatar
Gilles Caulier committed
648
    Vkontakte::CreateAlbumJob* job = new Vkontakte::CreateAlbumJob(
649
650
651
        m_accessToken,
        album->title(), album->description(),
        album->privacy(), album->commentPrivacy());
Gilles Caulier's avatar
Gilles Caulier committed
652
653
654
655

    connect(job, SIGNAL(result(KJob*)),
            this, SLOT(slotAlbumCreationDone(KJob*)));

656
657
658
659
660
661
    m_jobs.append(job);
    job->start();
}

void VkontakteWindow::slotAlbumCreationDone(KJob *kjob)
{
662
    SLOT_JOB_DONE_INIT(Vkontakte::CreateAlbumJob)
663

664
665
    // Select the newly created album in the combobox later (in "slotAlbumsUpdateDone()")
    m_albumToSelect = job->album()->aid();
666
667
668

    startAlbumsUpdate();
    m_albumsCombo->setEnabled(false);
Alexander Potashev's avatar
Alexander Potashev committed
669
    m_editAlbumButton->setEnabled(false);
670
    m_deleteAlbumButton->setEnabled(false);
671
672
673
    updateControls(false);
}

Gilles Caulier's avatar
Gilles Caulier committed
674
//---------------------------------------------------------------------------
Alexander Potashev's avatar
Alexander Potashev committed
675

Alexander Potashev's avatar
Alexander Potashev committed
676
void VkontakteWindow::startAlbumEditing(Vkontakte::AlbumInfoPtr album)
Alexander Potashev's avatar
Alexander Potashev committed
677
{
678
679
680
    // Select the same album again in the combobox later (in "slotAlbumsUpdateDone()")
    m_albumToSelect = album->aid();

Alexander Potashev's avatar
Alexander Potashev committed
681
    Vkontakte::EditAlbumJob *job = new Vkontakte::EditAlbumJob(
Alexander Potashev's avatar
Alexander Potashev committed
682
683
684
        m_accessToken,
        album->aid(), album->title(), album->description(),
        album->privacy(), album->commentPrivacy());
Gilles Caulier's avatar
Gilles Caulier committed
685
686
687
688

    connect(job, SIGNAL(result(KJob*)),
            this, SLOT(slotAlbumEditingDone(KJob*)));

Alexander Potashev's avatar
Alexander Potashev committed
689
690
691
692
693
694
    m_jobs.append(job);
    job->start();
}

void VkontakteWindow::slotAlbumEditingDone(KJob *kjob)
{
695
    SLOT_JOB_DONE_INIT(Vkontakte::EditAlbumJob)
Alexander Potashev's avatar
Alexander Potashev committed
696
697
698
699

    startAlbumsUpdate();
    m_albumsCombo->setEnabled(false);
    m_editAlbumButton->setEnabled(false);
700
701
702
703
    m_deleteAlbumButton->setEnabled(false);
    updateControls(false);
}

Gilles Caulier's avatar
Gilles Caulier committed
704
//---------------------------------------------------------------------------
705
706
707

void VkontakteWindow::startAlbumDeletion(Vkontakte::AlbumInfoPtr album)
{
Gilles Caulier's avatar
Gilles Caulier committed
708
709
710
711
712
    Vkontakte::DeleteAlbumJob* job = new Vkontakte::DeleteAlbumJob(m_accessToken, album->aid());

    connect(job, SIGNAL(result(KJob*)),
            this, SLOT(slotAlbumDeletionDone(KJob*)));

713
714
715
716
717
718
719
720
721
722
723
724
    m_jobs.append(job);
    job->start();
}

void VkontakteWindow::slotAlbumDeletionDone(KJob *kjob)
{
    SLOT_JOB_DONE_INIT(Vkontakte::DeleteAlbumJob)

    startAlbumsUpdate();
    m_albumsCombo->setEnabled(false);
    m_editAlbumButton->setEnabled(false);
    m_deleteAlbumButton->setEnabled(false);
Alexander Potashev's avatar
Alexander Potashev committed
725
726
727
    updateControls(false);
}

Gilles Caulier's avatar
Gilles Caulier committed
728
//---------------------------------------------------------------------------
Alexander Potashev's avatar
Alexander Potashev committed
729

730
731
void VkontakteWindow::slotNewAlbumRequest()
{
Alexander Potashev's avatar
Alexander Potashev committed
732
    Vkontakte::AlbumInfoPtr album(new Vkontakte::AlbumInfo());
Alexander Potashev's avatar
Alexander Potashev committed
733
734
    QPointer<VkontakteAlbumDialog> dlg = new VkontakteAlbumDialog(this, album);
    if (dlg->exec() == QDialog::Accepted)
735
736
737
738
    {
        updateControls(false);
        startAlbumCreation(album);
    }
Alexander Potashev's avatar
Alexander Potashev committed
739
740

    delete dlg;
741
742
}

Alexander Potashev's avatar
Alexander Potashev committed
743
744
void VkontakteWindow::slotEditAlbumRequest()
{
745
746
747
748
    Vkontakte::AlbumInfoPtr album = currentAlbum();
    if (album.isNull())
        return;

Alexander Potashev's avatar
Alexander Potashev committed
749
750
    QPointer<VkontakteAlbumDialog> dlg = new VkontakteAlbumDialog(this, album, true);
    if (dlg->exec() == QDialog::Accepted)
Alexander Potashev's avatar
Alexander Potashev committed
751
752
753
754
    {
        updateControls(false);
        startAlbumEditing(album);
    }
Alexander Potashev's avatar
Alexander Potashev committed
755
756

    delete dlg;
Alexander Potashev's avatar
Alexander Potashev committed
757
758
}

759
760
void VkontakteWindow::slotDeleteAlbumRequest()
{
761
762
763
    Vkontakte::AlbumInfoPtr album = currentAlbum();
    if (album.isNull())
        return;
764
765
766

    if (KMessageBox::warningContinueCancel(
        this,
767
        i18n("<qt>Are you sure you want to remove the album <b>%1</b> including all photos in it?</qt>", album->title()),
768
769
770
771
772
773
774
775
776
777
778
        i18nc("@title:window", "Confirm Album Deletion"),
        KStandardGuiItem::del(),
        KStandardGuiItem::cancel(),
        QString("kipi_vkontakte_delete_album_with_photos")) != KMessageBox::Continue)
    {
        return;
    }

    startAlbumDeletion(album);
}

779
780
781
void VkontakteWindow::slotReloadAlbumsRequest()
{
    updateControls(false);
782

783
784
785
786
    Vkontakte::AlbumInfoPtr album = currentAlbum();
    if (!album.isNull())
        m_albumToSelect = album->aid();

787
788
789
790
791
    startAlbumsUpdate();
}

void VkontakteWindow::slotStartTransfer()
{
792
793
    Vkontakte::AlbumInfoPtr album = currentAlbum();
    if (album.isNull() || m_albumsCombo->count() == 0)
794
    {
795
        // TODO: offer the user to create an album if there are no albums yet
796
        KMessageBox::information(this, i18n("Please select album first."));
797
798
799
800
801
802
803
804
805
        return;
    }

    // TODO: import support
    if (!m_import)
    {
        updateControls(false);

        QStringList files;
Gilles Caulier's avatar
Gilles Caulier committed
806
        foreach(const KUrl& url, m_imgList->imageUrls(true))
807
808
            files.append(url.toLocalFile());

Gilles Caulier's avatar
Gilles Caulier committed
809
        Vkontakte::UploadPhotosJob* job = new Vkontakte::UploadPhotosJob(
810
            m_accessToken, files, false /*m_checkKeepOriginal->isChecked()*/, album->aid());
Gilles Caulier's avatar
Gilles Caulier committed
811
812
813
814
815
816
817

        connect(job, SIGNAL(result(KJob*)),
                this, SLOT(slotPhotoUploadDone(KJob*)));

        connect(job, SIGNAL(progress(int)),
                m_progressBar, SLOT(setValue(int)));

818
819
820
        m_jobs.append(job);
        job->start();
    }
Alexander Potashev's avatar
Alexander Potashev committed
821
822

    m_progressBar->show();
smit mehta's avatar
Polish    
smit mehta committed
823
824
    m_progressBar->progressScheduled(i18n("Vkontakte Export"), false, true);
    m_progressBar->progressThumbnailChanged(KIcon("kipi").pixmap(22, 22));
825
826
827
828
}

void VkontakteWindow::slotPhotoUploadDone(KJob *kjob)
{
829
    SLOT_JOB_DONE_INIT(Vkontakte::UploadPhotosJob)
830

Alexander Potashev's avatar
Alexander Potashev committed
831
    m_progressBar->hide();
smit mehta's avatar
Polish    
smit mehta committed
832
    m_progressBar->progressCompleted();
833
834
835
836
837
838
839
840
841
    updateControls(true);
}

bool VkontakteWindow::isAuthenticated()
{
    return m_authenticated;
}

} // namespace KIPIVkontaktePlugin