File: KoRdfFoaF.cpp

package info (click to toggle)
koffice 1%3A2.2.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 157,656 kB
  • ctags: 110,762
  • sloc: cpp: 889,358; xml: 22,758; ansic: 6,533; python: 5,224; perl: 2,771; sh: 1,805; yacc: 1,304; ruby: 1,219; sql: 720; lex: 455; makefile: 76
file content (286 lines) | stat: -rw-r--r-- 8,969 bytes parent folder | download
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
/* This file is part of the KDE project
   Copyright (C) 2010 KO GmbH <ben.martin@kogmbh.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KoRdfFoaF.h"
#include "KoDocumentRdf.h"
#include "KoDocumentRdf_p.h"
#include "KoRdfSemanticItem_p.h"
#include "KoTextRdfCore.h"

#include <QUuid>
#include <QTemporaryFile>
#include <kdebug.h>
#include <kfiledialog.h>

// contacts
#ifdef KDEPIMLIBS_FOUND
#include <kabc/addressee.h>
#include <kabc/stdaddressbook.h>
#include <kabc/addressbook.h>
#include <kabc/phonenumber.h>
#include <kabc/vcardconverter.h>
#endif

#include "ui_KoRdfFoaFEditWidget.h"

using namespace Soprano;

class KoRdfFoaFPrivate : public KoRdfSemanticItemPrivate
{
    // foaf Rdf template,
    // s == m_uri
    // s -> <uri:gollum>; p -> <http://xmlns.com/foaf/0.1/name>; o -> "Gollum"
    // s-> <uri:gollum>; p -> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>; o -> <http://xmlns.com/foaf/0.1/Person>
public:
    QString m_uri;   // This is the subject in Rdf
    QString m_name;
    QString m_nick;
    QString m_homePage;
    QString m_imageUrl;
    QString m_phone;
    Ui::KoRdfFoaFEditWidget editWidget;
#ifdef KDEPIMLIBS_FOUND
    KABC::Addressee toKABC() const;
    void fromKABC(KABC::Addressee addr);
#endif

    KoRdfFoaFPrivate(KoDocumentRdf *rdf)
        : KoRdfSemanticItemPrivate(rdf)
        {}

    KoRdfFoaFPrivate(KoDocumentRdf *rdf,Soprano::QueryResultIterator &it)
        : KoRdfSemanticItemPrivate(rdf,it)
        {}
};

KoRdfFoaF::KoRdfFoaF(QObject *parent, KoDocumentRdf *m_rdf)
    :
    KoRdfSemanticItem(*new KoRdfFoaFPrivate (m_rdf),parent)
{
}

KoRdfFoaF::KoRdfFoaF(QObject *parent, KoDocumentRdf *m_rdf, Soprano::QueryResultIterator &it)
    :
    KoRdfSemanticItem(*new KoRdfFoaFPrivate (m_rdf,it),parent)
{
    Q_D (KoRdfFoaF);
    d->m_uri      = it.binding("person").toString();
    d->m_name     = it.binding("name").toString();
    d->m_nick     = KoTextRdfCore::optionalBindingAsString(it, "nick");
    d->m_homePage = KoTextRdfCore::optionalBindingAsString(it, "homepage");
    d->m_imageUrl = KoTextRdfCore::optionalBindingAsString(it, "img");
    d->m_phone    = KoTextRdfCore::optionalBindingAsString(it, "phone");
    kDebug(30015) << "+++xmlid:" << it.binding("xmlid").toString();
}

KoRdfFoaF::~KoRdfFoaF()
{
    kDebug(30015) << "~KoRdfFoaF() this:" << this << " name:" << name();
}


QString KoRdfFoaF::name() const
{
    Q_D (const KoRdfFoaF);
    return d->m_name;
}

QWidget* KoRdfFoaF::createEditor(QWidget * parent)
{
    Q_D (KoRdfFoaF);
    QWidget *ret = new QWidget(parent);
    d->editWidget.setupUi(ret);
    d->editWidget.name->setText(d->m_name);
    d->editWidget.nick->setText(d->m_nick);
    d->editWidget.url->setText(d->m_homePage);
    d->editWidget.phone->setText(d->m_phone);
    return ret;
}

void KoRdfFoaF::updateFromEditorData()
{
    Q_D (KoRdfFoaF);
    if (d->m_uri.size() <= 0) {
        QUuid u = QUuid::createUuid();
        d->m_uri = u.toString();
        kDebug(30015) << "no uuid, created one:" << u.toString();
    }
    QString predBase = "http://xmlns.com/foaf/0.1/";
    kDebug(30015) << "name:" << d->m_name << " newV:" << d->editWidget.name->text();
    setRdfType(predBase + "Person");
    updateTriple(d->m_name, d->editWidget.name->text(), predBase + "name");
    updateTriple(d->m_nick, d->editWidget.nick->text(), predBase + "nick");
    updateTriple(d->m_homePage, d->editWidget.url->text(), predBase + "homepage");
    updateTriple(d->m_phone, d->editWidget.phone->text(), predBase + "phone");
    if (documentRdf()) {
        documentRdf()->emitSemanticObjectUpdated(this);
    }
}

KoRdfSemanticTreeWidgetItem *KoRdfFoaF::createQTreeWidgetItem(QTreeWidgetItem *parent)
{
    kDebug(30015) << "format(), default stylesheet:" << defaultStylesheet()->name();
    KoRdfFoaFTreeWidgetItem *item = new KoRdfFoaFTreeWidgetItem(parent, this);
    return item;
}

Soprano::Node KoRdfFoaF::linkingSubject() const
{
    Q_D (const KoRdfFoaF);
    return Node::createResourceNode(d->m_uri);
}

void KoRdfFoaF::setupStylesheetReplacementMapping(QMap<QString, QString> &m)
{
    Q_D (KoRdfFoaF);
    m["%URI%"] = d->m_uri;
    m["%NICK%"] = d->m_nick;
    m["%HOMEPAGE%"] = d->m_homePage;
    m["%PHONE%"] = d->m_phone;
}

void KoRdfFoaF::exportToMime(QMimeData *md) const
{
    Q_D (const KoRdfFoaF);
    QTemporaryFile file;
    if (file.open()) {
        //QString mimeType = "text/directory"; // text/x-vcard";
        exportToFile(file.fileName());
        QByteArray ba = KoTextRdfCore::fileToByteArray(file.fileName());
        md->setData("text/directory", ba);
        md->setData("text/x-vcard", ba);
        kDebug(30015) << "ba.sz:" << ba.size();
        kDebug(30015) << "ba:" << ba;
    }
    QString data;
    QTextStream oss(&data);
    oss << name() << ", " << d->m_phone << flush;
    md->setText(data);
}

QList<KoSemanticStylesheet*> KoRdfFoaF::stylesheets() const
{
    QList<KoSemanticStylesheet*> stylesheets;
    stylesheets.append(
        new KoSemanticStylesheet("143c1ba3-d7bb-440b-8528-7f07d2eff5f2", "name", "%NAME%"));
    stylesheets.append(
        new KoSemanticStylesheet("2fad34d1-42a0-4b10-b17e-a87db5208f6d", "nick", "%NICK%"));
    stylesheets.append(
        new KoSemanticStylesheet("0dd5878d-95c5-47e5-a777-63ec36da3b9a", "name, phone", "%NAME%, %PHONE%"));
    stylesheets.append(
        new KoSemanticStylesheet("9cbeb4a6-34c5-49b2-b3ef-b94277db0c59", "nick, phone", "%NICK%, %PHONE%"));
    stylesheets.append(
        new KoSemanticStylesheet("47025a4a-5da5-4a32-8d89-14c03658631d", "name, (homepage), phone", "%NAME%, (%HOMEPAGE%), %PHONE%"));
    return stylesheets;
}

QString KoRdfFoaF::className() const
{
    return "Contact";
}

#ifdef KDEPIMLIBS_FOUND
KABC::Addressee KoRdfFoaFPrivate::toKABC() const
{
    KABC::Addressee addr;
    addr.setNameFromString(m_name);
    addr.setNickName(m_nick);
    KABC::PhoneNumber ph(m_phone, KABC::PhoneNumber::Work);
    addr.insertPhoneNumber(ph);
    return addr;
}

void KoRdfFoaFPrivate::fromKABC(KABC::Addressee addr)
{
    m_name = addr.realName();
    m_nick = addr.nickName();
    KABC::PhoneNumber ph = addr.phoneNumber(KABC::PhoneNumber::Work);
    m_phone = ph.number();
    m_homePage = addr.url().url();
}
#endif

void KoRdfFoaF::saveToKABC()
{
    Q_D (KoRdfFoaF);
    kDebug(30015) << "saving name:" << d->m_name;
#ifdef KDEPIMLIBS_FOUND
    KABC::StdAddressBook *ab = static_cast<KABC::StdAddressBook*>
                               (KABC::StdAddressBook::self());
    if (!ab) {
        return;
    }
    KABC::Ticket *ticket = ab->requestSaveTicket();
    KABC::Addressee addr = d->toKABC();
    ab->insertAddressee(addr);
    KABC::AddressBook* pab = ab;
    pab->save(ticket);
#endif
}

void KoRdfFoaF::exportToFile(const QString &fileNameConst) const
{
    Q_D (const KoRdfFoaF);
    QString fileName = fileNameConst;

#ifdef KDEPIMLIBS_FOUND
    if (!fileName.size()) {
        fileName = KFileDialog::getSaveFileName(
                       KUrl("kfiledialog:///ExportDialog"),
                       "*.vcf|vCard files",
                       0,
                       "Export to selected vCard file");

        if (!fileName.size()) {
            kDebug(30015) << "no filename given, cancel export..";
            return;
        }
    }
    KABC::Addressee addr = d->toKABC();
    KABC::VCardConverter converter;
    QByteArray ba = converter.createVCard(addr);
    QFile file(fileName);
    file.open(QIODevice::WriteOnly);
    file.write(ba);
    file.close();
    kDebug(30015) << "wrote " << ba.size() << " bytes to export file:" << fileName;
#else
    kDebug(30015) << "KDEPIM support not built!";
#endif
}

void KoRdfFoaF::importFromData(const QByteArray &ba, KoDocumentRdf *_rdf, KoCanvasBase *host)
{
    Q_D (KoRdfFoaF);
#ifdef KDEPIMLIBS_FOUND
    kDebug(30015) << "data.sz:" << ba.size();
    kDebug(30015) << "_rdf:" << _rdf;
    if (_rdf) {
        d->m_rdf = _rdf;
    }
    KABC::VCardConverter converter;
    KABC::Addressee addr = converter.parseVCard(ba);
    d->fromKABC(addr);
    kDebug(30015) << "adding name:" << d->m_name;
    kDebug(30015) << "uri:" << d->m_uri;
    importFromDataComplete(ba, documentRdf(), host);
#else
    kDebug(30015) << "KDEPIM support not built!";
#endif
}