Commit 96ca32fc authored by Alexander Potashev's avatar Alexander Potashev
Browse files

vkontakte: Factorize album selection widget into class AlbumChooserWidget

parent bb432b94
......@@ -7,6 +7,7 @@ set(kipiplugin_vkontakte_PART_SRCS
plugin_vkontakte.cpp
vkwindow.cpp
vkalbumdialog.cpp
albumchooserwidget.cpp
vkapi.cpp
)
......
/*
* A KIPI plugin to export images to VKontakte web service
* Copyright (C) 2011, 2012
* Alexander Potashev <aspotashev at gmail dot com>
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "albumchooserwidget.h"
#include <QWidget>
#include <QVBoxLayout>
#include <klocalizedstring.h>
#include <kguiitem.h>
#include <KComboBox>
#include <KPushButton>
#include <QToolButton>
#include <KMessageBox>
#include <libkvkontakte/albumlistjob.h>
#include <libkvkontakte/createalbumjob.h>
#include <libkvkontakte/editalbumjob.h>
#include <libkvkontakte/deletealbumjob.h>
#include "vkalbumdialog.h"
#include "vkapi.h"
namespace KIPIVkontaktePlugin
{
AlbumChooserWidget::AlbumChooserWidget(QWidget *parent, VkAPI *vkapi)
: QGroupBox(i18nc("@title:group Header above controls for managing albums", "Album"), parent)
{
m_vkapi = vkapi;
setWhatsThis(i18n("This is the VKontakte album that will be used for the transfer."));
QVBoxLayout *albumsBoxLayout = new QVBoxLayout(this);
m_albumsCombo = new KComboBox(this);
m_albumsCombo->setEditable(false);
m_newAlbumButton = new KPushButton(
KGuiItem(i18n("New Album"), "list-add",
i18n("Create new VKontakte album")), this);
m_reloadAlbumsButton = new KPushButton(
KGuiItem(i18nc("reload albums list", "Reload"), "view-refresh",
i18n("Reload albums list")), this);
m_editAlbumButton = new QToolButton(this);
m_editAlbumButton->setToolTip(i18n("Edit selected album"));
m_editAlbumButton->setEnabled(false);
m_editAlbumButton->setIcon(KIcon("document-edit"));
m_deleteAlbumButton = new QToolButton(this);
m_deleteAlbumButton->setToolTip(i18n("Delete selected album"));
m_deleteAlbumButton->setEnabled(false);
m_deleteAlbumButton->setIcon(KIcon("edit-delete"));
QWidget *currentAlbumWidget = new QWidget(this);
QHBoxLayout *currentAlbumWidgetLayout = new QHBoxLayout(currentAlbumWidget);
currentAlbumWidgetLayout->addWidget(m_albumsCombo);
currentAlbumWidgetLayout->addWidget(m_editAlbumButton);
currentAlbumWidgetLayout->addWidget(m_deleteAlbumButton);
QWidget *albumButtons = new QWidget(this);
QHBoxLayout *albumButtonsLayout = new QHBoxLayout(albumButtons);
albumButtonsLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
albumButtonsLayout->addWidget(m_newAlbumButton);
albumButtonsLayout->addWidget(m_reloadAlbumsButton);
albumsBoxLayout->addWidget(currentAlbumWidget);
albumsBoxLayout->addWidget(albumButtons);
connect(m_newAlbumButton, SIGNAL(clicked()), this, SLOT(slotNewAlbumRequest()));
connect(m_editAlbumButton, SIGNAL(clicked()), this, SLOT(slotEditAlbumRequest()));
connect(m_deleteAlbumButton, SIGNAL(clicked()), this, SLOT(slotDeleteAlbumRequest()));
connect(m_reloadAlbumsButton, SIGNAL(clicked()), this, SLOT(slotReloadAlbumsRequest()));
connect(m_vkapi, SIGNAL(authenticated()), this, SLOT(slotReloadAlbumsRequest()));
}
AlbumChooserWidget::~AlbumChooserWidget()
{
}
/**
* @brief Clear the list of albums
**/
void AlbumChooserWidget::clearList()
{
m_albumsCombo->clear();
}
Vkontakte::AlbumInfoPtr AlbumChooserWidget::currentAlbum()
{
int index = m_albumsCombo->currentIndex();
if (index >= 0)
return m_albums.at(index);
else
return Vkontakte::AlbumInfoPtr();
}
void AlbumChooserWidget::selectAlbum(int aid)
{
/*
* If the album list is not ready yet, select this album later
*/
m_albumToSelect = aid;
for (int i = 0; i < m_albums.size(); i ++)
if (m_albums.at(i)->aid() == aid)
{
m_albumsCombo->setCurrentIndex(i);
break;
}
}
//------------------------------
void AlbumChooserWidget::slotNewAlbumRequest()
{
Vkontakte::AlbumInfoPtr album(new Vkontakte::AlbumInfo());
QPointer<VkontakteAlbumDialog> dlg = new VkontakteAlbumDialog(this, album);
if (dlg->exec() == QDialog::Accepted)
{
updateBusyStatus(true);
startAlbumCreation(album);
}
delete dlg;
}
void AlbumChooserWidget::startAlbumCreation(Vkontakte::AlbumInfoPtr album)
{
Vkontakte::CreateAlbumJob *job = new Vkontakte::CreateAlbumJob(
m_vkapi->accessToken(),
album->title(), album->description(),
album->privacy(), album->commentPrivacy());
connect(job, SIGNAL(result(KJob*)), this, SLOT(slotAlbumCreationDone(KJob*)));
job->start();
}
void AlbumChooserWidget::slotAlbumCreationDone(KJob *kjob)
{
SLOT_JOB_DONE_INIT(Vkontakte::CreateAlbumJob)
// Select the newly created album in the combobox later (in "slotAlbumsReloadDone()")
m_albumToSelect = job->album()->aid();
startAlbumsReload();
updateBusyStatus(true);
}
//------------------------------
void AlbumChooserWidget::slotEditAlbumRequest()
{
Vkontakte::AlbumInfoPtr album = currentAlbum();
if (album.isNull())
return;
QPointer<VkontakteAlbumDialog> dlg = new VkontakteAlbumDialog(this, album, true);
if (dlg->exec() == QDialog::Accepted)
{
updateBusyStatus(true);
startAlbumEditing(album);
}
delete dlg;
}
void AlbumChooserWidget::startAlbumEditing(Vkontakte::AlbumInfoPtr album)
{
// Select the same album again in the combobox later (in "slotAlbumsReloadDone()")
m_albumToSelect = album->aid();
Vkontakte::EditAlbumJob *job = new Vkontakte::EditAlbumJob(
m_vkapi->accessToken(),
album->aid(), album->title(), album->description(),
album->privacy(), album->commentPrivacy());
connect(job, SIGNAL(result(KJob*)), this, SLOT(slotAlbumEditingDone(KJob*)));
job->start();
}
void AlbumChooserWidget::slotAlbumEditingDone(KJob *kjob)
{
SLOT_JOB_DONE_INIT(Vkontakte::EditAlbumJob)
startAlbumsReload();
updateBusyStatus(true);
}
//------------------------------
void AlbumChooserWidget::slotDeleteAlbumRequest()
{
Vkontakte::AlbumInfoPtr album = currentAlbum();
if (album.isNull())
return;
if (KMessageBox::warningContinueCancel(
this,
i18n("<qt>Are you sure you want to remove the album <b>%1</b> including all photos in it?</qt>", album->title()),
i18nc("@title:window", "Confirm Album Deletion"),
KStandardGuiItem::del(),
KStandardGuiItem::cancel(),
QString("kipi_vkontakte_delete_album_with_photos")) != KMessageBox::Continue)
{
return;
}
startAlbumDeletion(album);
}
void AlbumChooserWidget::startAlbumDeletion(Vkontakte::AlbumInfoPtr album)
{
Vkontakte::DeleteAlbumJob *job = new Vkontakte::DeleteAlbumJob(
m_vkapi->accessToken(), album->aid());
connect(job, SIGNAL(result(KJob*)), this, SLOT(slotAlbumDeletionDone(KJob*)));
job->start();
}
void AlbumChooserWidget::slotAlbumDeletionDone(KJob *kjob)
{
SLOT_JOB_DONE_INIT(Vkontakte::DeleteAlbumJob)
startAlbumsReload();
updateBusyStatus(true);
}
//------------------------------
void AlbumChooserWidget::slotReloadAlbumsRequest()
{
updateBusyStatus(true);
Vkontakte::AlbumInfoPtr album = currentAlbum();
if (!album.isNull())
m_albumToSelect = album->aid();
startAlbumsReload();
}
void AlbumChooserWidget::startAlbumsReload()
{
updateBusyStatus(true);
Vkontakte::AlbumListJob *job = new Vkontakte::AlbumListJob(m_vkapi->accessToken());
connect(job, SIGNAL(result(KJob*)), this, SLOT(slotAlbumsReloadDone(KJob*)));
job->start();
}
void AlbumChooserWidget::slotAlbumsReloadDone(KJob *kjob)
{
SLOT_JOB_DONE_INIT(Vkontakte::AlbumListJob)
m_albumsCombo->clear();
m_albums = job->list();
foreach (const Vkontakte::AlbumInfoPtr &album, m_albums)
m_albumsCombo->addItem(KIcon("folder-image"), album->title());
if (m_albumToSelect != -1)
{
selectAlbum(m_albumToSelect);
m_albumToSelect = -1;
}
m_albumsCombo->setEnabled(true);
if (!m_albums.empty())
{
m_editAlbumButton->setEnabled(true);
m_deleteAlbumButton->setEnabled(true);
}
updateBusyStatus(false);
}
//------------------------------
void AlbumChooserWidget::updateBusyStatus(bool busy)
{
setEnabled(!busy);
}
// TODO: share this code with `vkwindow.cpp`
void AlbumChooserWidget::handleVkError(KJob *kjob)
{
KMessageBox::error(this, kjob->errorText(), i18nc("@title:window", "Request to VKontakte failed"));
}
} // namespace KIPIVkontaktePlugin
/*
* A KIPI plugin to export images to VKontakte web service
* Copyright (C) 2011, 2012
* Alexander Potashev <aspotashev at gmail dot com>
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef ALBUMCHOOSERWIDGET_H
#define ALBUMCHOOSERWIDGET_H
#include <QGroupBox>
#include <libkvkontakte/albuminfo.h>
// TODO: share this code with `vkwindow.cpp`
#define SLOT_JOB_DONE_INIT(JobClass) \
JobClass *job = dynamic_cast<JobClass *>(kjob); \
Q_ASSERT(job); \
if (job->error()) \
{ \
handleVkError(job); \
return; \
}
class KJob;
class QToolButton;
class KPushButton;
class KComboBox;
namespace KIPIVkontaktePlugin
{
class VkAPI;
class AlbumChooserWidget : public QGroupBox
{
Q_OBJECT
public:
AlbumChooserWidget(QWidget *parent, VkAPI *vkapi);
~AlbumChooserWidget();
void clearList();
Vkontakte::AlbumInfoPtr currentAlbum();
void selectAlbum(int aid);
protected Q_SLOTS:
void slotNewAlbumRequest();
void slotEditAlbumRequest();
void slotDeleteAlbumRequest();
void slotReloadAlbumsRequest();
void startAlbumCreation(Vkontakte::AlbumInfoPtr album);
void startAlbumEditing(Vkontakte::AlbumInfoPtr album);
void startAlbumDeletion(Vkontakte::AlbumInfoPtr album);
void startAlbumsReload();
void slotAlbumCreationDone(KJob *kjob);
void slotAlbumEditingDone(KJob *kjob);
void slotAlbumDeletionDone(KJob *kjob);
void slotAlbumsReloadDone(KJob *kjob);
protected:
void updateBusyStatus(bool busy);
void handleVkError(KJob *kjob);
private:
KComboBox *m_albumsCombo;
KPushButton *m_newAlbumButton;
KPushButton *m_reloadAlbumsButton;
QToolButton *m_editAlbumButton;
QToolButton *m_deleteAlbumButton;
QList<Vkontakte::AlbumInfoPtr> m_albums;
/** album with this "aid" will be selected in slotAlbumsReloadDone() */
int m_albumToSelect;
VkAPI *m_vkapi;
};
} // namespace KIPIVkontaktePlugin
#endif // ALBUMCHOOSERWIDGET_H
......@@ -6,7 +6,7 @@
* 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>
* Copyright (C) 2011, 2012 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>
......@@ -65,12 +65,6 @@
// libkvkontakte includes
#include <libkvkontakte/getapplicationpermissionsjob.h>
#include <libkvkontakte/authenticationdialog.h>
#include <libkvkontakte/albumlistjob.h>
#include <libkvkontakte/createalbumjob.h>
#include <libkvkontakte/editalbumjob.h>
#include <libkvkontakte/deletealbumjob.h>
#include <libkvkontakte/uploadphotosjob.h>
#include <libkvkontakte/getvariablejob.h>
......@@ -90,10 +84,12 @@
#include "kpaboutdata.h"
#include "kpversion.h"
#include "kpimageslist.h"
#include "vkalbumdialog.h"
#include "kpprogresswidget.h"
#include "vkapi.h"
#include "vkalbumdialog.h"
#include "albumchooserwidget.h"
#undef SLOT_JOB_DONE_INIT
#define SLOT_JOB_DONE_INIT(JobClass) \
JobClass *job = dynamic_cast<JobClass *>(kjob); \
Q_ASSERT(job); \
......@@ -110,6 +106,8 @@ namespace KIPIVkontaktePlugin
VkontakteWindow::VkontakteWindow(bool import, QWidget* const parent)
: KPToolDialog(parent)
{
m_albumsBox = NULL;
m_vkapi = new VkAPI(this);
// read settings from file
......@@ -167,57 +165,8 @@ VkontakteWindow::VkontakteWindow(bool import, QWidget* const parent)
/*
* Album box
*/
m_albumsBox = new QGroupBox(i18nc("@title:group Header above controls for managing albums", "Album"), settingsBox);
m_albumsBox->setWhatsThis(
i18n("This is the VKontakte album that will be used for the transfer."));
QVBoxLayout *albumsBoxLayout = new QVBoxLayout(m_albumsBox);
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);
m_editAlbumButton = new QToolButton(m_albumsBox);
m_editAlbumButton->setToolTip(i18n("Edit selected album"));
m_editAlbumButton->setEnabled(false);
m_editAlbumButton->setIcon(KIcon("document-edit"));
m_deleteAlbumButton = new QToolButton(m_albumsBox);
m_deleteAlbumButton->setToolTip(i18n("Delete selected album"));
m_deleteAlbumButton->setEnabled(false);
m_deleteAlbumButton->setIcon(KIcon("edit-delete"));
QWidget *currentAlbumWidget = new QWidget(m_albumsBox);
QHBoxLayout *currentAlbumWidgetLayout = new QHBoxLayout(currentAlbumWidget);
currentAlbumWidgetLayout->addWidget(m_albumsCombo);
currentAlbumWidgetLayout->addWidget(m_editAlbumButton);
currentAlbumWidgetLayout->addWidget(m_deleteAlbumButton);
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);
albumsBoxLayout->addWidget(currentAlbumWidget);
albumsBoxLayout->addWidget(albumButtons);
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()));
m_albumsBox = new AlbumChooserWidget(this, m_vkapi);
m_albumsBox->selectAlbum(m_albumToSelect);
// ------------------------------------------------------------------------
......@@ -281,7 +230,6 @@ VkontakteWindow::VkontakteWindow(bool import, QWidget* const parent)
{
// TODO: import support
m_imgList->hide();
m_newAlbumButton->hide();
// optionsBox->hide();
}
......@@ -391,7 +339,8 @@ void VkontakteWindow::updateAuthInfo()
else
{
loginText = i18n("Unauthorized");
m_albumsCombo->clear();
if (m_albumsBox)
m_albumsBox->clearList();
}
if (m_vkapi->isAuthenticated() && m_userId != -1)
......@@ -405,25 +354,6 @@ void VkontakteWindow::updateAuthInfo()
.arg(urlText).arg(i18n("VKontakte")));
}
Vkontakte::AlbumInfoPtr VkontakteWindow::currentAlbum()
{
int index = m_albumsCombo->currentIndex();
if (index >= 0)
return m_albums.at(index);
else
return Vkontakte::AlbumInfoPtr();
}
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;
}
}
//---------------------------------------------------------------------------
void VkontakteWindow::readSettings()
......@@ -446,7 +376,7 @@ void VkontakteWindow::writeSettings()
if (!m_vkapi->accessToken().isEmpty())
grp.writeEntry("AccessToken", m_vkapi->accessToken());
Vkontakte::AlbumInfoPtr album = currentAlbum();
Vkontakte::AlbumInfoPtr album = m_albumsBox->currentAlbum();
if (album.isNull())
grp.deleteEntry("SelectedAlbumId");
......@@ -492,8 +422,8 @@ void VkontakteWindow::startAuthentication(bool forceLogout)
{
m_userFullName.clear();
m_userId = -1;
m_editAlbumButton->setEnabled(false);
m_deleteAlbumButton->setEnabled(false);
if (m_albumsBox)
m_albumsBox->setEnabled(false);
m_vkapi->startAuthentication(forceLogout);
}
......@@ -507,46 +437,6 @@ void VkontakteWindow::handleVkError(KJob *kjob)
//---------------------------------------------------------------------------
void VkontakteWindow::startAlbumsUpdate()
{
Vkontakte::AlbumListJob* job = new Vkontakte::AlbumListJob(m_vkapi->accessToken());
connect(job, SIGNAL(result(KJob*)),
this, SLOT(slotAlbumsUpdateDone(KJob*)));
m_jobs.append(job);
job->start();
}
void VkontakteWindow::slotAlbumsUpdateDone(KJob *kjob)
{
SLOT_JOB_DONE_INIT(Vkontakte::AlbumListJob)
m_albumsCombo->clear();
m_albums = job->list();
foreach(const Vkontakte::AlbumInfoPtr &album, m_albums)
m_albumsCombo->addItem(KIcon("folder-image"), album->title());
if (m_albumToSelect != -1)
{
selectAlbum(m_albumToSelect);
m_albumToSelect = -1;
}
m_albumsCombo->setEnabled(true);
if (!m_albums.empty())
{
m_editAlbumButton->setEnabled(true);
m_deleteAlbumButton->setEnabled(true);
}
emit signalUpdateBusyStatus(false);
}