~ubuntu-branches/ubuntu/vivid/kdepim/vivid

« back to all changes in this revision

Viewing changes to pimcommon/createresource.cpp

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman, Jonathan Riddell, Rohan Garg, Scott Kitterman
  • Date: 2012-11-21 13:12:36 UTC
  • mfrom: (0.2.33)
  • Revision ID: package-import@ubuntu.com-20121121131236-32ijw9a2txrar80k
Tags: 4:4.9.80-0ubuntu1
[ Jonathan Riddell ]
* New upstream beta release

[ Rohan Garg ]
* Add nepomuk-core-dev to build-deps

[ Scott Kitterman ]
* Add new package, libpimcommon4
  - Add libpimcommon4.install
  - Add to debian/control, including kdepim-dbg and kdepim-dev depends
  - Add to kdepim-dev.install
* Remove usr/bin/backupmail and related files from kmail.install as they are
  not provided by upstream anymore
* Add usr/bin/pimsettingexporter and related files to kmail.install
* Add libnepomukwidgets-dev to build-depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 2012 Montel Laurent <montel@kde.org>
 
3
  
 
4
  This program is free software; you can redistribute it and/or modify it
 
5
  under the terms of the GNU General Public License, version 2, as
 
6
  published by the Free Software Foundation.
 
7
  
 
8
  This program is distributed in the hope that it will be useful, but
 
9
  WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
  General Public License for more details.
 
12
  
 
13
  You should have received a copy of the GNU General Public License along
 
14
  with this program; if not, write to the Free Software Foundation, Inc.,
 
15
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
16
*/
 
17
 
 
18
#include "createresource.h"
 
19
 
 
20
#include <KDebug>
 
21
#include <KLocale>
 
22
 
 
23
#include <akonadi/agenttype.h>
 
24
#include <akonadi/agentmanager.h>
 
25
#include <akonadi/agentinstancecreatejob.h>
 
26
 
 
27
#include <QDBusReply>
 
28
#include <QDBusInterface>
 
29
#include <QMetaMethod>
 
30
 
 
31
using namespace Akonadi;
 
32
 
 
33
using namespace PimCommon;
 
34
 
 
35
CreateResource::CreateResource()
 
36
{
 
37
}
 
38
 
 
39
CreateResource::~CreateResource()
 
40
{
 
41
}
 
42
 
 
43
//code from accountwizard
 
44
static QVariant::Type argumentType( const QMetaObject *mo, const QString &method )
 
45
{
 
46
  QMetaMethod m;
 
47
  const int numberOfMethod( mo->methodCount() );
 
48
  for ( int i = 0; i < numberOfMethod; ++i ) {
 
49
    const QString signature = QString::fromLatin1( mo->method( i ).signature() );
 
50
    if ( signature.contains(method + QLatin1Char('(') )) {
 
51
      m = mo->method( i );
 
52
      break;
 
53
    }
 
54
  }
 
55
 
 
56
  if ( !m.signature() ) {
 
57
    kWarning() << "Did not find D-Bus method: " << method << " available methods are:";
 
58
    const int numberOfMethod(mo->methodCount());
 
59
    for ( int i = 0; i < numberOfMethod; ++ i )
 
60
      kWarning() << mo->method( i ).signature();
 
61
    return QVariant::Invalid;
 
62
  }
 
63
 
 
64
  const QList<QByteArray> argTypes = m.parameterTypes();
 
65
  if ( argTypes.count() != 1 )
 
66
    return QVariant::Invalid;
 
67
 
 
68
  return QVariant::nameToType( argTypes.first() );
 
69
}
 
70
 
 
71
QString CreateResource::createResource( const QString& resources, const QString& name, const QMap<QString, QVariant>& settings )
 
72
{
 
73
  const AgentType type = AgentManager::self()->type( resources );
 
74
  if ( !type.isValid() ) {
 
75
    Q_EMIT createResourceError( i18n( "Resource type '%1' is not available.", resources ) );
 
76
    return QString();
 
77
  }
 
78
 
 
79
  // check if unique instance already exists
 
80
  kDebug() << type.capabilities();
 
81
  if ( type.capabilities().contains( QLatin1String( "Unique" ) ) ) {
 
82
    Q_FOREACH ( const AgentInstance &instance, AgentManager::self()->instances() ) {
 
83
      kDebug() << instance.type().identifier() << (instance.type() == type);
 
84
      if ( instance.type() == type ) {
 
85
        Q_EMIT createResourceInfo(i18n( "Resource '%1' is already set up.", type.name() ) );
 
86
        return QString();
 
87
      }
 
88
    }
 
89
  }
 
90
 
 
91
  Q_EMIT createResourceInfo( i18n( "Creating resource instance for '%1'...", type.name() ) );
 
92
  AgentInstanceCreateJob *job = new AgentInstanceCreateJob( type, this );
 
93
  if(job->exec()) {
 
94
    Akonadi::AgentInstance instance = job->instance();
 
95
 
 
96
    if ( !settings.isEmpty() ) {
 
97
      Q_EMIT createResourceInfo( i18n( "Configuring resource instance..." ) );
 
98
      QDBusInterface iface( "org.freedesktop.Akonadi.Resource." + instance.identifier(), "/Settings" );
 
99
      if ( !iface.isValid() ) {
 
100
        Q_EMIT createResourceError( i18n( "Unable to configure resource instance." ) );
 
101
        return QString();
 
102
      }
 
103
 
 
104
      // configure resource
 
105
      if ( !name.isEmpty() )
 
106
        instance.setName( name );
 
107
      QMap<QString, QVariant>::const_iterator end( settings.constEnd());
 
108
      for ( QMap<QString, QVariant>::const_iterator it = settings.constBegin(); it != end; ++it ) {
 
109
        kDebug() << "Setting up " << it.key() << " for agent " << instance.identifier();
 
110
        const QString methodName = QString::fromLatin1("set%1").arg( it.key() );
 
111
        QVariant arg = it.value();
 
112
        const QVariant::Type targetType = argumentType( iface.metaObject(), methodName );
 
113
        if ( !arg.canConvert( targetType ) ) {
 
114
          Q_EMIT createResourceError( i18n( "Could not convert value of setting '%1' to required type %2.", it.key(), QVariant::typeToName( targetType ) ) );
 
115
          return QString();
 
116
        }
 
117
        arg.convert( targetType );
 
118
        QDBusReply<void> reply = iface.call( methodName, arg );
 
119
        if ( !reply.isValid() ) {
 
120
          Q_EMIT createResourceError( i18n( "Could not set setting '%1': %2", it.key(), reply.error().message() ) );
 
121
          return QString();
 
122
        }
 
123
      }
 
124
      instance.reconfigure();
 
125
    }
 
126
 
 
127
    Q_EMIT createResourceInfo( i18n( "Resource setup completed." ) );
 
128
    return instance.identifier();
 
129
  } else {
 
130
    if ( job->error() ) {
 
131
      Q_EMIT createResourceError( i18n( "Failed to create resource instance: %1", job->errorText() ) );
 
132
    }
 
133
  }
 
134
  return QString();
 
135
}
 
136
 
 
137
#include "createresource.moc"