1 | /* |
2 | Copyright (c) 2009 Jonathan Armond <jon.armond@gmail.com> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "kmigratorbase.h" |
21 | |
22 | #include <akonadi/agentinstancecreatejob.h> |
23 | #include <akonadi/agentmanager.h> |
24 | #include <akonadi/agenttype.h> |
25 | |
26 | #include <KSharedConfig> |
27 | #include <KConfigGroup> |
28 | #include <KGlobal> |
29 | #include <KLocalizedString> |
30 | #include <KStandardDirs> |
31 | #include <KDebug> |
32 | |
33 | #include <QFile> |
34 | #include <QMetaEnum> |
35 | #include <QTimer> |
36 | #include <kcomponentdata.h> |
37 | |
38 | using namespace Akonadi; |
39 | |
40 | namespace { |
41 | |
42 | QString messageTypeToString( KMigratorBase::MessageType type ) |
43 | { |
44 | switch ( type ) { |
45 | case KMigratorBase::Success: return QLatin1String( "Success" ); |
46 | case KMigratorBase::Skip: return QLatin1String( "Skipped" ); |
47 | case KMigratorBase::Info: return QLatin1String( "Info " ); |
48 | case KMigratorBase::Warning: return QLatin1String( "WARNING" ); |
49 | case KMigratorBase::Error: return QLatin1String( "ERROR " ); |
50 | } |
51 | Q_ASSERT( false ); |
52 | return QString(); |
53 | } |
54 | |
55 | } |
56 | |
57 | KMigratorBase::KMigratorBase() : m_logFile( 0 ) |
58 | { |
59 | KGlobal::ref(); |
60 | |
61 | const QString logFileName = KStandardDirs::locateLocal( "data" , KGlobal::mainComponent().componentName() + QLatin1String("/migration.log" ) ); |
62 | m_logFile = new QFile( logFileName ); |
63 | if ( !m_logFile->open( QFile::Append ) ) { |
64 | delete m_logFile; |
65 | m_logFile = 0; |
66 | kWarning() << "Unable to open log file: " << logFileName; |
67 | } |
68 | logMessage( Info, QLatin1String("Starting migration..." ) ); |
69 | connect( this, SIGNAL(message(KMigratorBase::MessageType,QString)), SLOT(logMessage(KMigratorBase::MessageType,QString)) ); |
70 | |
71 | // load the vtable before we continue |
72 | QTimer::singleShot( 0, this, SLOT(migrate()) ); |
73 | } |
74 | |
75 | KMigratorBase::~KMigratorBase() |
76 | { |
77 | logMessage( Info, QLatin1String("Migration finished." ) ); |
78 | delete m_logFile; |
79 | KGlobal::deref(); |
80 | } |
81 | |
82 | KMigratorBase::MigrationState KMigratorBase::migrationState( const QString &identifier ) const |
83 | { |
84 | KConfigGroup cfg( KGlobal::config(), QLatin1String("Resource " ) + identifier ); |
85 | QMetaEnum e = metaObject()->enumerator( metaObject()->indexOfEnumerator( "MigrationState" ) ); |
86 | const QString s = cfg.readEntry( "MigrationState" , e.valueToKey( None ) ); |
87 | MigrationState state = (MigrationState)e.keyToValue( s.toLatin1() ); |
88 | |
89 | if ( state != None ) { |
90 | const QString resId = cfg.readEntry( "ResourceIdentifier" , "" ); |
91 | // previously migrated but removed again |
92 | if ( !AgentManager::self()->instance( resId ).isValid() ) |
93 | state = None; |
94 | } |
95 | |
96 | return state; |
97 | } |
98 | |
99 | void KMigratorBase::setMigrationState( const QString &identifier, MigrationState state, |
100 | const QString &resId, const QString &type ) |
101 | { |
102 | KConfigGroup cfg( KGlobal::config(), QLatin1String("Resource " ) + identifier ); |
103 | QMetaEnum e = metaObject()->enumerator( metaObject()->indexOfEnumerator( "MigrationState" ) ); |
104 | const QString stateStr = QLatin1String(e.valueToKey( state )); |
105 | cfg.writeEntry( "MigrationState" , stateStr ); |
106 | cfg.writeEntry( "ResourceIdentifier" , resId ); |
107 | cfg.sync(); |
108 | |
109 | cfg = KConfigGroup( KGlobal::config(), "Bridged" ); |
110 | QStringList bridgedResources = cfg.readEntry( type + QLatin1String("Resources" ), QStringList() ); |
111 | if ( state == Bridged ) { |
112 | if ( !bridgedResources.contains( identifier ) ) |
113 | bridgedResources << identifier; |
114 | } else { |
115 | bridgedResources.removeAll( identifier ); |
116 | } |
117 | cfg.writeEntry( type + QLatin1String("Resources" ), bridgedResources ); |
118 | cfg.sync(); |
119 | } |
120 | |
121 | KJob *KMigratorBase::createAgentInstance(const QString& typeId, QObject* receiver, const char* slot) |
122 | { |
123 | emit message( Info, i18n( "Creating instance of type %1" , typeId ) ); |
124 | AgentInstanceCreateJob *job = new AgentInstanceCreateJob( typeId, this ); |
125 | connect( job, SIGNAL(result(KJob*)), receiver, slot ); |
126 | job->start(); |
127 | return job; |
128 | } |
129 | |
130 | void KMigratorBase::logMessage(KMigratorBase::MessageType type, const QString& msg) |
131 | { |
132 | if ( m_logFile ) { |
133 | m_logFile->write( QString( QLatin1Char( '[' ) + QDateTime::currentDateTime().toString() + QLatin1String( "] " ) |
134 | + messageTypeToString( type ) + QLatin1String( ": " ) + msg + QLatin1Char( '\n' ) ).toUtf8() ); |
135 | m_logFile->flush(); |
136 | } |
137 | } |
138 | |
139 | |