Warning, file /network/ktp-send-file/mainwindow.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * Main Send File Transfer Window
0003  *
0004  * Copyright (C) 2011 David Edmundson <kde@davidedmundson.co.uk>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Lesser General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2.1 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Lesser General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Lesser General Public
0017  * License along with this library; if not, write to the Free Software
0018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0019  */
0020 
0021 #include "mainwindow.h"
0022 #include "ui_mainwindow.h"
0023 
0024 #include <KFileItem>
0025 #include <KPixmapSequence>
0026 #include <KPixmapSequenceOverlayPainter>
0027 #include <KLocalizedString>
0028 #include <KIO/PreviewJob>
0029 
0030 #include <QPushButton>
0031 #include <QMessageBox>
0032 #include <QLineEdit>
0033 
0034 #include <TelepathyQt/AccountManager>
0035 #include <TelepathyQt/PendingChannelRequest>
0036 #include <TelepathyQt/PendingReady>
0037 
0038 #include <KTp/actions.h>
0039 #include <KTp/contact-factory.h>
0040 #include <KTp/Models/contacts-list-model.h>
0041 #include <KTp/Models/contacts-filter-model.h>
0042 #include <KTp/Widgets/contact-grid-widget.h>
0043 
0044 MainWindow::MainWindow(const QList<QUrl> &urls, QWidget *parent) :
0045     QWidget(parent),
0046     ui(new Ui::MainWindow),
0047     m_urls(urls),
0048     m_contactsModel(0)
0049 {
0050     Tp::registerTypes();
0051 
0052     ui->setupUi(this);
0053     if (urls.size() == 1) {
0054         setWindowTitle(i18n("Send file - %1", urls.first().fileName()));
0055 
0056         ui->filesInfoLabel->hide();
0057         ui->fileNameLabel->setText(urls.first().fileName());
0058     } else {
0059         QString fileNames;
0060         Q_FOREACH(const QUrl &file, urls) {
0061             fileNames += file.fileName() + " ";
0062         }
0063         setWindowTitle(i18n("Send files - %1", fileNames.trimmed()));
0064 
0065         ui->messageLabel->setText(i18n("You are about to send these files"));
0066         ui->filesInfoLabel->setText(i18np("1 file selected", "%1 files selected", urls.count()));
0067         ui->fileNameLabel->setText(fileNames.replace(" ", "<br />"));
0068     }
0069 
0070     m_busyOverlay = new KPixmapSequenceOverlayPainter(this);
0071     m_busyOverlay->setSequence(KPixmapSequence("process-working", 22));
0072     m_busyOverlay->setWidget(ui->filePreview);
0073     m_busyOverlay->start();
0074 
0075     if (urls.size() == 1) {
0076         KFileItem file(urls.first());
0077         QStringList availablePlugins = KIO::PreviewJob::availablePlugins();
0078         KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << file, QSize(280, 280), &availablePlugins);
0079         job->setOverlayIconAlpha(0);
0080         job->setScaleType(KIO::PreviewJob::Unscaled);
0081         connect(job, SIGNAL(gotPreview(KFileItem, QPixmap)),
0082                 this, SLOT(onPreviewLoaded(KFileItem, QPixmap)));
0083         connect(job, SIGNAL(failed(KFileItem)),
0084                 this, SLOT(onPreviewFailed(KFileItem)));
0085     } else {
0086         ui->filePreview->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-information.png")).pixmap(128));
0087         m_busyOverlay->stop();
0088     }
0089 
0090     Tp::AccountFactoryPtr  accountFactory = Tp::AccountFactory::create(QDBusConnection::sessionBus(),
0091                                                                        Tp::Features() << Tp::Account::FeatureCore
0092                                                                        << Tp::Account::FeatureAvatar
0093                                                                        << Tp::Account::FeatureProtocolInfo
0094                                                                        << Tp::Account::FeatureProfile
0095                                                                        << Tp::Account::FeatureCapabilities);
0096 
0097     Tp::ConnectionFactoryPtr connectionFactory = Tp::ConnectionFactory::create(QDBusConnection::sessionBus(),
0098                                                                                Tp::Features() << Tp::Connection::FeatureCore
0099                                                                                << Tp::Connection::FeatureRosterGroups
0100                                                                                << Tp::Connection::FeatureRoster
0101                                                                                << Tp::Connection::FeatureSelfContact);
0102 
0103     Tp::ContactFactoryPtr contactFactory = KTp::ContactFactory::create(Tp::Features()  << Tp::Contact::FeatureAlias
0104                                                                       << Tp::Contact::FeatureAvatarData
0105                                                                       << Tp::Contact::FeatureSimplePresence
0106                                                                       << Tp::Contact::FeatureCapabilities);
0107 
0108     Tp::ChannelFactoryPtr channelFactory = Tp::ChannelFactory::create(QDBusConnection::sessionBus());
0109 
0110     m_accountManager = Tp::AccountManager::create(QDBusConnection::sessionBus(),
0111                                                   accountFactory,
0112                                                   connectionFactory,
0113                                                   channelFactory,
0114                                                   contactFactory);
0115 
0116     m_contactsModel = new KTp::ContactsListModel(this);
0117     connect(m_accountManager->becomeReady(), SIGNAL(finished(Tp::PendingOperation*)), SLOT(onAccountManagerReady()));
0118 
0119 
0120     m_contactGridWidget = new KTp::ContactGridWidget(m_contactsModel, this);
0121     m_contactGridWidget->contactFilterLineEdit()->setPlaceholderText(i18n("Search in Contacts..."));
0122     m_contactGridWidget->filter()->setPresenceTypeFilterFlags(KTp::ContactsFilterModel::ShowOnlyConnected);
0123     m_contactGridWidget->filter()->setCapabilityFilterFlags(KTp::ContactsFilterModel::FilterByFileTransferCapability);
0124     ui->recipientVLayout->addWidget(m_contactGridWidget);
0125 
0126     connect(m_contactGridWidget,
0127             SIGNAL(selectionChanged(Tp::AccountPtr,KTp::ContactPtr)),
0128             SLOT(onContactSelectionChanged(Tp::AccountPtr,KTp::ContactPtr)));
0129 
0130     ui->buttonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
0131     connect(ui->buttonBox, SIGNAL(accepted()), SLOT(onDialogAccepted()));
0132     connect(ui->buttonBox, SIGNAL(rejected()), SLOT(close()));
0133 }
0134 
0135 MainWindow::~MainWindow()
0136 {
0137     delete ui;
0138 }
0139 
0140 void MainWindow::onAccountManagerReady()
0141 {
0142     m_contactsModel->setAccountManager(m_accountManager);
0143 }
0144 
0145 void MainWindow::onDialogAccepted()
0146 {
0147     // don't do anytghing if no contact has been selected
0148     if (!m_contactGridWidget->hasSelection()) {
0149         // show message box?
0150         return;
0151     }
0152 
0153     // start sending file
0154     Q_FOREACH(const QUrl &url, m_urls) {
0155         Tp::PendingOperation* channelRequest = KTp::Actions::startFileTransfer(m_contactGridWidget->selectedAccount(),
0156                                                                                m_contactGridWidget->selectedContact(),
0157                                                                                url);
0158 
0159         connect(channelRequest, SIGNAL(finished(Tp::PendingOperation*)), SLOT(slotFileTransferFinished(Tp::PendingOperation*)));
0160     }
0161 
0162     //disable the buttons
0163     foreach(QAbstractButton* button, ui->buttonBox->buttons()) {
0164         button->setEnabled(false);
0165     }
0166 }
0167 
0168 void MainWindow::onContactSelectionChanged(Tp::AccountPtr account, KTp::ContactPtr contact)
0169 {
0170     Q_UNUSED(account)
0171     Q_UNUSED(contact)
0172 
0173     ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(m_contactGridWidget->hasSelection());
0174 }
0175 
0176 void MainWindow::slotFileTransferFinished(Tp::PendingOperation* op)
0177 {
0178     if (op->isError()) {
0179         //FIXME map to human readable strings.
0180         QString errorMsg(op->errorName() + ": " + op->errorMessage());
0181         qWarning() << "ERROR!: " << errorMsg;
0182         QMessageBox::warning(this, i18n("Failed to send file"), i18n("File Transfer Failed"));
0183         close();
0184     } else {
0185         // now I can close the dialog
0186         close();
0187     }
0188 }
0189 
0190 void MainWindow::onPreviewLoaded(const KFileItem& item, const QPixmap& preview)
0191 {
0192     Q_UNUSED(item);
0193     ui->filePreview->setPixmap(preview);
0194     m_busyOverlay->stop();
0195 }
0196 
0197 void MainWindow::onPreviewFailed(const KFileItem& item)
0198 {
0199     qWarning() << "Loading thumb failed" << item.name();
0200     ui->filePreview->setPixmap(QIcon::fromTheme(item.iconName()).pixmap(128));
0201     m_busyOverlay->stop();
0202 }