~renatofilho/address-book-app/save-state

« back to all changes in this revision

Viewing changes to src/app/contentcommunicator.cpp

  • Committer: Tarmac
  • Author(s): Renato Araujo Oliveira Filho
  • Date: 2013-12-18 02:46:41 UTC
  • mfrom: (114.1.30 content-hub)
  • Revision ID: tarmac-20131218024641-2o3c5ht5hw6xtepm
Implemented content hub support to export contacts.

Approved by PS Jenkins bot, Bill Filler.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
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.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 */
 
17
 
 
18
#include <contentcommunicator.h>
 
19
 
 
20
#include <QDebug>
 
21
#include <QTemporaryFile>
 
22
#include <QDir>
 
23
 
 
24
#include <com/ubuntu/content/hub.h>
 
25
#include <com/ubuntu/content/item.h>
 
26
#include <com/ubuntu/content/transfer.h>
 
27
 
 
28
using namespace com::ubuntu::content;
 
29
 
 
30
/*!
 
31
 * \brief ContentCommunicator::ContentCommunicator
 
32
 * \param parent
 
33
 */
 
34
ContentCommunicator::ContentCommunicator(QObject *parent)
 
35
    : ImportExportHandler(parent),
 
36
      m_transfer(0)
 
37
{
 
38
    Hub *hub = Hub::Client::instance();
 
39
    if (hub) {
 
40
        hub->register_import_export_handler(this);
 
41
    } else {
 
42
        qWarning() << "Fail to get Hub client instance";
 
43
    }
 
44
}
 
45
 
 
46
/*!
 
47
 * \brief \reimp
 
48
 */
 
49
void ContentCommunicator::handle_import(content::Transfer *)
 
50
{
 
51
    qDebug() << Q_FUNC_INFO << "address book app does not import content";
 
52
}
 
53
 
 
54
/*!
 
55
 * \brief \reimp
 
56
 */
 
57
void ContentCommunicator::handle_export(content::Transfer *transfer)
 
58
{
 
59
    if (m_transfer != 0) {
 
60
        qWarning() << "address book app does only one content export at a time";
 
61
        transfer->abort();
 
62
        m_transfer = 0;
 
63
        return;
 
64
    }
 
65
 
 
66
    if (transfer) {
 
67
        m_transfer = transfer;
 
68
        connect(m_transfer, SIGNAL(selectionTypeChanged()), SIGNAL(multipleItemsChanged()));
 
69
    } else {
 
70
        qWarning() << "Transfer pointer is null in handle_export";
 
71
    }
 
72
    Q_EMIT contactRequested();
 
73
    Q_EMIT activeChanged();
 
74
    Q_EMIT multipleItemsChanged();
 
75
}
 
76
 
 
77
/*!
 
78
 * \brief ContentCommunicator::cancelTransfer aborts the current transfer
 
79
 */
 
80
void ContentCommunicator::cancelTransfer()
 
81
{
 
82
    if (!m_transfer) {
 
83
        qWarning() << "No ongoing transfer to cancel";
 
84
        return;
 
85
    }
 
86
 
 
87
    m_transfer->abort();
 
88
    m_transfer = 0;
 
89
    Q_EMIT activeChanged();
 
90
}
 
91
 
 
92
/*!
 
93
 * \brief ContentCommunicator::returnContacts returns the given contacts
 
94
 * via content hub to the requester
 
95
 * \param urls
 
96
 */
 
97
void ContentCommunicator::returnContacts(const QUrl &contactsFile)
 
98
{
 
99
    if (!m_transfer) {
 
100
        qWarning() << "No ongoing transfer to return a contact";
 
101
        return;
 
102
    }
 
103
 
 
104
    QVector<Item> items;
 
105
    items << contactsFile;
 
106
    m_transfer->charge(items);
 
107
    m_transfer = 0;
 
108
    Q_EMIT activeChanged();
 
109
}
 
110
 
 
111
bool ContentCommunicator::isActive() const
 
112
{
 
113
    return (m_transfer != 0);
 
114
}
 
115
 
 
116
bool ContentCommunicator::isMultipleItems() const
 
117
{
 
118
    return (m_transfer && m_transfer->selectionType() == Transfer::multiple);
 
119
}
 
120
 
 
121
QUrl ContentCommunicator::createTemporaryFile() const
 
122
{
 
123
    QTemporaryFile tmp(QDir::tempPath() + "/vcard_XXXXXX.vcf");
 
124
    tmp.setAutoRemove(false);
 
125
    if (!tmp.open()) {
 
126
        qWarning() << "Fail to create temporary file for vcard.";
 
127
        return QUrl();
 
128
    }
 
129
    QString tmpFileName = tmp.fileName();
 
130
    tmp.close();
 
131
    return QUrl::fromLocalFile(tmpFileName);
 
132
}