summaryrefslogtreecommitdiffstats
path: root/kontact/src/profilemanager.cpp
blob: 53419cc5482f7355dd66e696a7c4b0236f81686c (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
    This file is part of KDE Kontact.

    Copyright (c) 2007 Frank Osterfeld <frank.osterfeld@kdemail.net>

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

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

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

    As a special exception, permission is given to link this program
    with any edition of Qt, and distribute the resulting executable,
    without including the source code for Qt in the source distribution.
*/

#include "profilemanager.h"

#include <kio/job.h>

#include <kapplication.h>
#include <kconfig.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kstaticdeleter.h>
#include <kurl.h>

#include <qdir.h>
#include <qstringlist.h>
#include <qvaluelist.h>

Kontact::Profile::Profile( const QString& id, bool isLocal ) : m_id( id ), m_local( isLocal )
{
}

Kontact::Profile::Profile() : m_local( false )
{
}

QString Kontact::Profile::id() const
{
    return m_id;
}

QString Kontact::Profile::name() const
{
    return m_name;
}

QString Kontact::Profile::description() const
{
    return m_description;
}

bool Kontact::Profile::isNull() const
{
    return m_id.isNull();
}

void Kontact::Profile::setId( const QString& id )
{
    m_id = id;
}

void Kontact::Profile::setDescription( const QString& description )
{
    m_description = description;
}

void Kontact::Profile::setName( const QString& name )
{
    m_name = name;
}

void Kontact::Profile::setLocal( SetLocalMode mode )
{
    if ( m_local )
        return;

    if ( mode == CopyProfileFiles )
        copyConfigFiles( m_originalLocation, localSaveLocation() );

    m_local = true;
}

bool Kontact::Profile::isLocal() const
{
    return m_local;
}

void Kontact::Profile::setOriginalLocation( const QString& path )
{
    m_originalLocation = path;
}

QString Kontact::Profile::localSaveLocation() const
{

    return  m_id.isNull() ? QString() : locateLocal( "data", "kontact/profiles/" + m_id, /*create folder=*/true );
}

QString Kontact::Profile::saveLocation() const
{
    return m_local ? localSaveLocation() : m_originalLocation;
}

bool Kontact::Profile::operator==( const Kontact::Profile& other ) const
{
    return m_id == other.m_id && m_name == other.m_name && m_description == other.m_description;
}

Kontact::ProfileManager* Kontact::ProfileManager::m_self = 0;

static KStaticDeleter<Kontact::ProfileManager> profileManagerSD;

Kontact::ProfileManager* Kontact::ProfileManager::self() 
{
    if ( m_self == 0 )
    {
        profileManagerSD.setObject( m_self, new Kontact::ProfileManager );
        m_self->readConfig();
    }
    return m_self;
}

Kontact::ProfileManager::ProfileManager( QObject* parent ) : QObject( parent )
{
}

Kontact::ProfileManager::~ProfileManager()
{
    writeConfig();
}

void Kontact::ProfileManager::writeConfig() const
{
    const QValueList<Kontact::Profile> profiles = m_profiles.values();
    for ( QValueList<Kontact::Profile>::ConstIterator it = profiles.begin(), end = profiles.end(); it != end; ++it )
    {
        writeProfileConfig( *it );
    }
}

Kontact::Profile Kontact::ProfileManager::readFromConfiguration( const QString& configFile, bool isLocal )
{
    KConfig profileCfg( configFile, true /*read-only*/, false /*no KDE global*/ );
    const QString configDir = configFile.left( configFile.findRev( QDir::separator(), -1 ) );
    profileCfg.setGroup( "Kontact Profile" );
    const QString id = profileCfg.readEntry( "Identifier" );
    Kontact::Profile profile( id );
    profile.setName( profileCfg.readEntry( "Name" ) );
    profile.setDescription( profileCfg.readEntry( "Description" ) );
    profile.setOriginalLocation( configDir );
    if ( isLocal )
        profile.setLocal( Kontact::Profile::DoNotCopyProfileFiles );
    return profile;
}

void Kontact::ProfileManager::writeProfileConfig( const Kontact::Profile& profile ) const
{
    const QString profileDir = profile.saveLocation();
    const QString cfgPath = profileDir + "/profile.cfg";
    KConfig profileCfg( cfgPath, false /*read-only*/, false /*no KDE global*/ );
    profileCfg.setGroup( "Kontact Profile" );
    profileCfg.writeEntry( "Identifier", profile.id() );
    profileCfg.writeEntry( "Name", profile.name() );
    profileCfg.writeEntry( "Description", profile.description() );
}

void Kontact::ProfileManager::readConfig()
{
    
    const QStringList profilePaths = KGlobal::dirs()->findAllResources( "data", QString::fromLatin1( "kontact/profiles/*/profile.cfg" ) );

    typedef QMap<QString, Kontact::Profile> ProfileMap;
    ProfileMap profiles;
    ProfileMap globalProfiles;

    const QString localPrefix = locateLocal( "data", "kontact/profiles/", /*createDir=*/false );
    for ( QStringList::ConstIterator it = profilePaths.begin(), end = profilePaths.end(); it != end; ++it )
    {
        const bool isLocal = (*it).startsWith( localPrefix );
        const Kontact::Profile profile = readFromConfiguration( *it, isLocal );
        if ( profile.isNull() )
            continue;
        if ( isLocal )
            profiles[profile.id()] = profile;
        else 
            globalProfiles[profile.id()] = profile;
    }
    
    for ( ProfileMap::ConstIterator it = globalProfiles.begin(), end = globalProfiles.end(); it != end; ++it )
    {
        if ( !profiles.contains( it.key() ) )
            profiles[it.key()] = it.data();
    }

    for ( ProfileMap::ConstIterator it = profiles.begin(), end = profiles.end(); it != end; ++it )
    {
        addProfile( *it, false /*dont sync config */ );
    }
}

QValueList<Kontact::Profile> Kontact::ProfileManager::profiles() const
{
    return m_profiles.values();
}

Kontact::Profile Kontact::ProfileManager::profileById( const QString& id ) const
{
    return m_profiles[id];
}

void Kontact::ProfileManager::updateProfile( const Kontact::Profile& profile_ )
{
    const QString id = profile_.id();
    if ( id.isNull() || m_profiles[id] == profile_ )
        return;
    Kontact::Profile profile( profile_ );
    m_profiles[id] = profile;
    profile.setLocal( Kontact::Profile::CopyProfileFiles );
    writeProfileConfig( profile );
    emit profileUpdated( id );
}

void Kontact::Profile::copyConfigFiles( const QString& source_, const QString& dest_ )
{
    const KURL source = KURL::fromPathOrURL( source_+"/*rc" );
    const KURL dest = KURL::fromPathOrURL( dest_ );
    KIO::CopyJob* job = KIO::copy( source, dest, /*showProgressInfo=*/false );
    // TODO better check for the copy result
}

void Kontact::ProfileManager::saveToProfile( const QString& id )
{
    Kontact::Profile profile = profileById( id );
    if ( profile.isNull() )
        return;
    profile.setLocal( Kontact::Profile::CopyProfileFiles );
    writeProfileConfig( profile );
    emit saveToProfileRequested( id );
}

bool Kontact::ProfileManager::addProfile( const Kontact::Profile& profile, bool syncConfig )
{
    const QString id = profile.id();
    if ( m_profiles.contains( id ) )
        return false;
    m_profiles[id] = profile;
    emit profileAdded( id );
    emit saveToProfileRequested( id );
    if ( syncConfig ) {
        writeProfileConfig( profile );
    }

    return true;
}

void Kontact::ProfileManager::loadProfile( const QString& id )
{
    if ( !m_profiles.contains( id ) )
        return;
    emit profileLoaded( id );
}

void Kontact::ProfileManager::removeProfile( const Kontact::Profile& profile )
{
    removeProfile( profile.id() );
}

void Kontact::ProfileManager::removeProfile( const QString& id )
{
    if ( !m_profiles.contains( id ) )
        return;
    Kontact::Profile profile = profileById( id );
    if ( profile.isLocal() ) {
        KURL location = KURL::fromPathOrURL( profile.saveLocation() );
        KIO::DeleteJob* job = KIO::del( location, /*shred*/ false, /*showProgressInfo=*/false );
        // TODO check result
    }
    m_profiles.remove( id );
    emit profileRemoved( id );
 }

Kontact::ProfileManager::ExportError Kontact::ProfileManager::exportProfileToDirectory( const QString& id, const QString& path )
{
    if ( !m_profiles.contains( id ) )
        return SuccessfulExport;

    if ( !QDir( path ).exists() )
        return DirectoryDoesNotExist;

    const Kontact::Profile profile = profileById( id );
    const KURL source = KURL::fromPathOrURL( profile.saveLocation() );
    const KURL target = KURL::fromPathOrURL( path + QDir::separator() + profile.name() );

    KIO::CopyJob* job = KIO::copy( source, target, /*showProgressInfo=*/false );
    // TODO check result

    return SuccessfulExport;
}

Kontact::ProfileManager::ImportError Kontact::ProfileManager::importProfileFromDirectory( const QString& path )
{
    Kontact::Profile profile = readFromConfiguration( path + "/profile.cfg", /*isLocal=*/ true );
    if ( profile.isNull() )
        return NoValidProfile;

    profile.setId( generateNewId() );

    const KURL source = KURL::fromPathOrURL( path );
    const KURL target = KURL::fromPathOrURL( profile.saveLocation() );

    KIO::CopyJob* job = KIO::copy( source, target, /*showProgressInfo=*/false );
    // TODO better check for the copy result

    addProfile( profile );

    return SuccessfulImport;
}

QString Kontact::ProfileManager::generateNewId() const
{
    while ( true )
    {
        const QString newId = KApplication::randomString( 10 );
        if ( !m_profiles.contains( newId ) )
            return newId;
    }
}

#include "profilemanager.moc"