~ubuntu-branches/ubuntu/karmic/kdepim/karmic-backports

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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*******************************************************************************
**
** Filename   : kmmessagetag.cpp
** Created on : 29 June 2007
** Copyright  : (c) 2007 Ismail Onur Filiz
** Email      : onurf@su.sabanciuniv.edu
**
*******************************************************************************/

/*******************************************************************************
**
**   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.
**
**   In addition, as a special exception, the copyright holders give
**   permission to link the code of this program with any edition of
**   the Qt library by Trolltech AS, Norway (or with modified versions
**   of Qt that use the same license as Qt), and distribute linked
**   combinations including the two.  You must obey the GNU General
**   Public License in all respects for all of the code used other than
**   Qt.  If you modify this file, you may extend this exception to
**   your version of the file, but you are not obligated to do so.  If
**   you do not wish to do so, delete this exception statement from
**   your version.
**
*******************************************************************************/

#include "kmmessagetag.h"

#include <config-kmail.h>

#include "kmkernel.h"

#include <QStringList>
#include <QColor>
#include <QFont>
#include <QString>
#include <QRegExp>

#include <kapplication.h>
#include <klocale.h>
#include <krandom.h>

#ifdef Nepomuk_FOUND
#include <nepomuk/resourcemanager.h>
#endif

//----------------------------KMMessageTagDescription------------------------------------
KMMessageTagDescription::KMMessageTagDescription( const QString &aLabel, 
                             const QString &aName, 
                            const int aPriority, const QColor &aTextColor, 
                            const QColor &aBackgroundColor, 
                            const QFont &aTextFont, const bool aInToolbar, 
                            const QString &aIconName, 
                            const KShortcut &aShortcut ) {
  if ( aLabel.isEmpty() || aName.isEmpty() ) {
    mEmpty = true;
    return;
  }
  mLabel = aLabel;
  mName = aName;
  mPriority = aPriority;
  mTextColor = aTextColor;
  mBackgroundColor = aBackgroundColor;
  mTextFont = aTextFont;
  mInToolbar = aInToolbar;
  mIconName = aIconName;
  mShortcut = aShortcut;
  mEmpty = false;
}

KMMessageTagDescription::KMMessageTagDescription( const KConfigGroup& aGroup ) 
            : mPriority( -1 ), mEmpty( false ) 
{
  readConfig(aGroup);
}

void KMMessageTagDescription::setLabel( const QString& aLabel ) { mLabel = aLabel; }
void KMMessageTagDescription::setName( const QString& aName ) { mName = aName; }
/*void KMMessageTagDescription::setBackgroundColor( const QColor& aBackgroundColor )
{
  mBackgroundColor = aBackgroundColor; 
}*/
void KMMessageTagDescription::setTextColor( const QColor& aTextColor ) 
{
  mTextColor = aTextColor; 
}
void KMMessageTagDescription::setTextFont( const QFont& aTextFont ) 
{
  mTextFont = aTextFont; 
}
void KMMessageTagDescription::setIconName( const QString& aIconName ) 
{
  mIconName = aIconName; 
}
void KMMessageTagDescription::setShortcut( const KShortcut &aShortcut )
{
  mShortcut = aShortcut;
}

void KMMessageTagDescription::readConfig( const KConfigGroup& group ) 
{
  mEmpty = true;

  mLabel = group.readEntry( "Label", QString() );
  if ( mLabel.isEmpty() )
    return;
  mName = group.readEntry( "Name", QString() );
  if ( mName.isEmpty() )
    return;

  mEmpty = false;

  //Empty is fine, uses default color
  mTextColor = group.readEntry( "text-color", QColor()); 
  mBackgroundColor = group.readEntry( "background-color", QColor() );
  //Empty uses current standard font
  mTextFont = group.readEntry( "text-font", QFont() );

  mInToolbar = group.readEntry( "toolbar", false );
  mIconName = group.readEntry( "icon", "mail-tagged" );

  QString shortcut( group.readEntry( "shortcut", QString() ) );
  if ( !shortcut.isEmpty() )
    mShortcut = KShortcut( shortcut );

  purify();
}

void KMMessageTagDescription::writeConfig( KConfigGroup &group ) const 
{
  group.writeEntry( "Label", mLabel );
  group.writeEntry( "Name", mName );
  if ( mTextColor.isValid() ) 
    group.writeEntry( "text-color", mTextColor );

  if ( mTextFont != QFont() ) 
    group.writeEntry( "text-font", mTextFont );
  //Try to use isCopyOf later for the standard font. An alternative
  //which doesn't work:
  //if (! mTextFont.isCopyOf(KGlobalSettings::generalFont()))
  if ( !mShortcut.isEmpty() )
    group.writeEntry( "shortcut", mShortcut.toString() );

  group.writeEntry( "toolbar", mInToolbar );
  group.writeEntry( "icon", mIconName );
}

void KMMessageTagDescription::purify() 
{ 
  //fill as necessary
}
//---------------------------EO KMMessageTagDescription-----------------------------------

//-----------------------------KMMessageTagMgr-------------------------------------

KMMessageTagMgr::KMMessageTagMgr() : mDirty( 0 ) 
{
  mTagDict = new QHash<QString,KMMessageTagDescription *>();
  mTagList = new QList<KMMessageTagDescription *>();
  #ifdef Nepomuk_FOUND
  Nepomuk::ResourceManager::instance()->init(); 
  #endif
}

KMMessageTagMgr::~KMMessageTagMgr() 
{
  writeConfig( false );
  clear();
  delete mTagDict;
  delete mTagList;
}

const KMMessageTagDescription *KMMessageTagMgr::find( const QString &aLabel ) const 
{
  QHash< QString, KMMessageTagDescription* >::iterator it = mTagDict->find( aLabel );
  return it != mTagDict->end() ? it.value() : 0;

}

void KMMessageTagMgr::clear() 
{
  //Technically we are safe with true always, since there
  //is supposed to be only one Manager hence dictionary
  qDeleteAll(*mTagDict);
  mTagDict->clear();
  //TODO: Should I delete the items as well?
  mTagList->clear();
  mDirty = true;
}

void KMMessageTagMgr::fillTagsFromList( const QList<KMMessageTagDescription*> *aTagList ) 
{
  clear();
  //Assumes the list is ordered
  if ( aTagList ) {
    int i = 0;
    QListIterator<KMMessageTagDescription*> it ( *aTagList );
    while ( it.hasNext() ) {
      KMMessageTagDescription *tmp_ptr = new KMMessageTagDescription( *it.next() );
      tmp_ptr->setPriority( i );
      addTag( tmp_ptr, false );
      ++i;
    }
  }
  emit msgTagListChanged();
}

void KMMessageTagMgr::createInitialTags() 
{
  //Some predefined structures for the very first initialization of tags
  static const struct { 
    QString name;
    QString color;
  } TagNames[] = { 
    { i18n("Friend"), "blue" },
    { i18n("Business"), "green" },
    { i18n("Later"), "darkCyan"}
  };
  const int n = sizeof TagNames / sizeof *TagNames;
  for ( int i = 0; i < n; ++i ) {
    KMMessageTagDescription* tmp_ptr = new KMMessageTagDescription( KRandom::randomString(10),
                                       TagNames[i].name, i, TagNames[i].color );
    addTag( tmp_ptr, false );
  }
}

void KMMessageTagMgr::addTag( KMMessageTagDescription *aDesc, bool emitChange )
{
  if (! aDesc)
    return;
  mTagDict->insert( aDesc->label(), aDesc );
  QList<KMMessageTagDescription *>::iterator it = mTagList->begin(); 
  while ( it != mTagList->end() && (*it)->priority() < aDesc->priority() )
    it++;
  mTagList->insert( it, aDesc );

  if ( emitChange )
    emit msgTagListChanged();

  mDirty = true;
}

void KMMessageTagMgr::readConfig() 
{
  KConfig *config = KMKernel::config();
  int numTags = 0;
  QString grpName;

  clear();

  KConfigGroup group( config, "General" );
  numTags = group.readEntry( "messagetags", -1 );

  if ( numTags < 0 ) {
    //Key doesn't exist, which probably means first run with the tag feature
    //Create some tags, put them into dictionary, set dirty
    createInitialTags(); 
  } else {
    //j used to handle priorities properly, in case a tag definition is missing
    int j = 0;
    for ( int i=0; i < numTags; ++i ) {
      grpName.sprintf( "MessageTag #%d", i );
      if (! config->hasGroup( grpName ) ) {
        //Something wrong, set dirty so the tag set is correctly written back
        mDirty = true;
        continue;
      }
      KConfigGroup group( config, grpName );
      KMMessageTagDescription *tag = new KMMessageTagDescription( group );
      if ( tag->isEmpty() ) {
        delete tag;
      } else {
        tag->setPriority(j);
        addTag( tag, false );
        ++j;
      }
    }
  }
}

void KMMessageTagMgr::writeConfig( bool withSync ) 
{
  KConfig *config = KMKernel::config();

  //first, delete all groups:
  QStringList tagGroups 
      = config->groupList().filter( QRegExp( "MessageTag #\\d+" ) );
  foreach ( const QString& group, tagGroups )
    config->deleteGroup( group );

  // Now, write out the new stuff:
  int i = 0;
  foreach ( KMMessageTagDescription *description, *mTagList ) {
    if ( ! description->isEmpty() ) {
      QString grpName;
      grpName.sprintf( "MessageTag #%d", i );
      KConfigGroup group( config, grpName );
      description->writeConfig( group );
      ++i;
    }
  }

  KConfigGroup group( config, "General" );
  group.writeEntry( "messagetags", i );
  if (withSync) 
    config->sync();
  //mDirty = false;
}
//---------------------------EO KMMessageTagMgr------------------------------------

//-----------------------------KMMessageTagList------------------------------------

KMMessageTagList::KMMessageTagList() : QStringList() { }
KMMessageTagList::KMMessageTagList( const QStringList &aList ): QStringList( aList ) { }

bool KMMessageTagList::compareTags( const QString &lhs, const QString &rhs)
{
  //Implement less than or equal to
  const KMMessageTagDescription *lhsptr = kmkernel->msgTagMgr()->find( lhs );
  if ( !lhsptr )
    return false;
  const KMMessageTagDescription *rhsptr = kmkernel->msgTagMgr()->find( rhs );
  if ( !rhsptr )
    return true;
  return lhsptr->priority() < rhsptr->priority();
}

void KMMessageTagList::prioritySort() 
{
  //Use bubble sort for now
  int n = size();
  if ( !n )
    return;
  QString tmp, lhs, rhs;
  for ( int i=0; i< n-1; i++ ) {
    for ( int j=0; j< n-1-i; j++ ) {
      lhs = operator[]( j+1 );
      rhs = operator[]( j );
      if ( compareTags(lhs, rhs) ) {  /* compare the two neighbors */
        tmp = rhs;         /* swap a[j] and a[j+1]      */
        operator[]( j ) = lhs;
        operator[]( j+1 ) = tmp;
      }
    }
  }
}

const KMMessageTagList KMMessageTagList::split( const QString &aSep, 
                                        const QString &aStr )
{
  return KMMessageTagList( aStr.split( aSep, QString::SkipEmptyParts ) );
}
//----------------------------EO KMMessageTagList----------------------------------

#include "kmmessagetag.moc"