• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kmail

  • sources
  • kde-4.12
  • kdepim
  • kmail
util.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 **
3 ** Filename : util
4 ** Created on : 03 April, 2005
5 ** Copyright : (c) 2005 Till Adam
6 ** Email : <adam@kde.org>
7 **
8 *******************************************************************************/
9 
10 /*******************************************************************************
11 **
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License as published by
14 ** the Free Software Foundation; either version 2 of the License, or
15 ** (at your option) any later version.
16 **
17 ** It is distributed in the hope that it will be useful, but
18 ** WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** General Public License for more details.
21 **
22 ** You should have received a copy of the GNU General Public License
23 ** along with this program; if not, write to the Free Software
24 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 **
26 ** In addition, as a special exception, the copyright holders give
27 ** permission to link the code of this program with any edition of
28 ** the Qt library by Trolltech AS, Norway (or with modified versions
29 ** of Qt that use the same license as Qt), and distribute linked
30 ** combinations including the two. You must obey the GNU General
31 ** Public License in all respects for all of the code used other than
32 ** Qt. If you modify this file, you may extend this exception to
33 ** your version of the file, but you are not obligated to do so. If
34 ** you do not wish to do so, delete this exception statement from
35 ** your version.
36 **
37 *******************************************************************************/
38 #include "util.h"
39 #include "kmkernel.h"
40 
41 #include "messagecore/utils/stringutil.h"
42 #include "messagecomposer/helper/messagehelper.h"
43 
44 #include "templateparser/templateparser.h"
45 
46 #include <kmime/kmime_message.h>
47 #include <kmessagebox.h>
48 #include <KLocale>
49 #include <KProcess>
50 
51 #include <KStandardDirs>
52 
53 #include <QProcess>
54 #include <QFileInfo>
55 #include <QModelIndex>
56 
57 #include "foldercollection.h"
58 
59 using namespace MailCommon;
60 
61 
62 KMime::Types::Mailbox::List KMail::Util::mailingListsFromMessage( const Akonadi::Item& item )
63 {
64  KMime::Types::Mailbox::List addresses;
65  // determine the mailing list posting address
66  Akonadi::Collection parentCollection = item.parentCollection();
67  if ( parentCollection.isValid() ) {
68  const QSharedPointer<FolderCollection> fd = FolderCollection::forCollection( parentCollection, false );
69  if ( fd->isMailingListEnabled() && !fd->mailingListPostAddress().isEmpty() ) {
70  addresses << MessageCore::StringUtil::mailboxFromUnicodeString( fd->mailingListPostAddress() );
71  }
72  }
73 
74  return addresses;
75 }
76 
77 Akonadi::Item::Id KMail::Util::putRepliesInSameFolder( const Akonadi::Item& item )
78 {
79  Akonadi::Collection parentCollection = item.parentCollection();
80  if ( parentCollection.isValid() ) {
81  const QSharedPointer<FolderCollection> fd = FolderCollection::forCollection( parentCollection, false );
82  if( fd->putRepliesInSameFolder() ) {
83  return parentCollection.id();
84  }
85  }
86  return -1;
87 }
88 
89 void KMail::Util::launchAccountWizard( QWidget *w )
90 {
91  QStringList lst;
92  lst.append( QLatin1String("--type") );
93  lst.append( QLatin1String("message/rfc822") );
94 
95  const QString path = KStandardDirs::findExe( QLatin1String("accountwizard" ) );
96  if( !QProcess::startDetached( path, lst ) )
97  KMessageBox::error( w, i18n( "Could not start the account wizard. "
98  "Please check your installation." ),
99  i18n( "Unable to start account wizard" ) );
100 
101 }
102 
103 bool KMail::Util::handleClickedURL( const KUrl &url, const QSharedPointer<MailCommon::FolderCollection> &folder )
104 {
105  if ( url.protocol() == QLatin1String( "mailto" ) ) {
106  KMime::Message::Ptr msg ( new KMime::Message );
107  uint identity = !folder.isNull() ? folder->identity() : 0;
108  MessageHelper::initHeader( msg, KMKernel::self()->identityManager(), identity );
109  msg->contentType()->setCharset("utf-8");
110 
111  QMap<QString, QString> fields = MessageCore::StringUtil::parseMailtoUrl( url );
112 
113  msg->to()->fromUnicodeString( fields.value( QLatin1String("to") ),"utf-8" );
114  if ( !fields.value( QLatin1String("subject") ).isEmpty() )
115  msg->subject()->fromUnicodeString( fields.value( QLatin1String("subject") ),"utf-8" );
116  if ( !fields.value( QLatin1String("body") ).isEmpty() )
117  msg->setBody( fields.value( QLatin1String("body") ).toUtf8() );
118  if ( !fields.value( QLatin1String("cc" )).isEmpty() )
119  msg->cc()->fromUnicodeString( fields.value( QLatin1String("cc") ),"utf-8" );
120 
121  if ( !folder.isNull() ) {
122  TemplateParser::TemplateParser parser( msg, TemplateParser::TemplateParser::NewMessage );
123  parser.setIdentityManager( KMKernel::self()->identityManager() );
124  parser.process( msg, folder->collection() );
125  }
126 
127  KMail::Composer * win = KMail::makeComposer( msg, false, false, KMail::Composer::New, identity );
128  win->setFocusToSubject();
129  if ( !folder.isNull() ) {
130  win->setCollectionForNewMessage( folder->collection() );
131  }
132  win->show();
133  return true;
134  } else {
135  kWarning() << "Can't handle URL:" << url;
136  return false;
137  }
138 }
139 
140 bool KMail::Util::mailingListsHandleURL( const KUrl::List& lst,const QSharedPointer<MailCommon::FolderCollection> &folder )
141 {
142  const QString handler = ( folder->mailingList().handler() == MailingList::KMail )
143  ? QLatin1String( "mailto" ) : QLatin1String( "https" );
144 
145  KUrl urlToHandle;
146  KUrl::List::ConstIterator end( lst.constEnd() );
147  for ( KUrl::List::ConstIterator itr = lst.constBegin(); itr != end; ++itr ) {
148  if ( handler == (*itr).protocol() ) {
149  urlToHandle = *itr;
150  break;
151  }
152  }
153  if ( urlToHandle.isEmpty() && !lst.empty() ) {
154  urlToHandle = lst.first();
155  }
156 
157  if ( !urlToHandle.isEmpty() ) {
158  return KMail::Util::handleClickedURL( urlToHandle, folder );
159  } else {
160  kWarning()<< "Can't handle url";
161  return false;
162  }
163 }
164 
165 bool KMail::Util::mailingListPost( const QSharedPointer<MailCommon::FolderCollection> &fd )
166 {
167  if ( fd )
168  return KMail::Util::mailingListsHandleURL( fd->mailingList().postUrls(),fd );
169  return false;
170 }
171 
172 bool KMail::Util::mailingListSubscribe( const QSharedPointer<MailCommon::FolderCollection> &fd )
173 {
174  if ( fd )
175  return KMail::Util::mailingListsHandleURL( fd->mailingList().subscribeUrls(),fd );
176  return false;
177 }
178 
179 bool KMail::Util::mailingListUnsubscribe( const QSharedPointer<MailCommon::FolderCollection> &fd )
180 {
181  if ( fd )
182  return KMail::Util::mailingListsHandleURL( fd->mailingList().unsubscribeUrls(),fd );
183  return false;
184 }
185 
186 bool KMail::Util::mailingListArchives( const QSharedPointer<MailCommon::FolderCollection> &fd )
187 {
188  if ( fd )
189  return KMail::Util::mailingListsHandleURL( fd->mailingList().archiveUrls(),fd );
190  return false;
191 }
192 
193 bool KMail::Util::mailingListHelp( const QSharedPointer<MailCommon::FolderCollection> &fd )
194 {
195  if ( fd )
196  return KMail::Util::mailingListsHandleURL( fd->mailingList().helpUrls(),fd );
197  return false;
198 }
199 
200 void KMail::Util::lastEncryptAndSignState(bool &lastEncrypt, bool &lastSign, const KMime::Message::Ptr& msg)
201 {
202  lastSign = KMime::isSigned(msg.get());
203  lastEncrypt = KMime::isEncrypted(msg.get());
204 }
205 
206 QColor KMail::Util::misspelledColor()
207 {
208  return QColor(Qt::red);
209 }
210 
211 QColor KMail::Util::quoteL1Color()
212 {
213  return QColor( 0x00, 0x80, 0x00 );
214 }
215 
216 QColor KMail::Util::quoteL2Color()
217 {
218  return QColor( 0x00, 0x70, 0x00 );
219 }
220 
221 QColor KMail::Util::quoteL3Color()
222 {
223  return QColor( 0x00, 0x60, 0x00 );
224 }
225 
226 
227 void KMail::Util::reduceQuery(QString &query)
228 {
229  QRegExp rx(QLatin1String("<[\\w]+://[\\w\\d-_.]+(/[\\d\\w/-._]+/)*([\\w\\d-._]+)#([\\w\\d]+)>"));
230  query.replace(rx,QLatin1String("\\2:\\3"));
231  query.replace( QLatin1String("rdf-schema:"), QLatin1String("rdfs:") );
232  query.replace( QLatin1String("22-rdf-syntax-ns:"), QLatin1String("rdf:") );
233  query.replace( QLatin1String("XMLSchema:"), QLatin1String("xsd:") );
234  query = query.simplified();
235 }
236 
237 void KMail::Util::migrateFromKMail1()
238 {
239  // Akonadi migration
240  // check if there is something to migrate at all
241  bool needMigration = true;
242  KConfig oldKMailConfig( QLatin1String("kmailrc"), KConfig::NoGlobals );
243  if ( oldKMailConfig.hasGroup("General") ||
244  ( oldKMailConfig.groupList().count() == 1 &&
245  oldKMailConfig.groupList().first() == QLatin1String("$Version") ) ) {
246  const QFileInfo oldDataDirFileInfo( KStandardDirs::locateLocal( "data", QLatin1String("kmail") ) );
247  if ( !oldDataDirFileInfo.exists() || !oldDataDirFileInfo.isDir() ) {
248  // neither config or data, the migrator cannot do anything useful anyways
249  needMigration = false;
250  }
251  } else {
252  needMigration = false;
253  }
254 
255  KConfig config( QLatin1String("kmail-migratorrc") );
256  KConfigGroup migrationCfg( &config, "Migration" );
257  if ( needMigration ) {
258  const bool enabled = migrationCfg.readEntry( "Enabled", false );
259  const int currentVersion = migrationCfg.readEntry( "Version", 0 );
260  const int targetVersion = migrationCfg.readEntry( "TargetVersion", 1 );
261  if ( enabled && currentVersion < targetVersion ) {
262  const int choice = KMessageBox::questionYesNoCancel( 0, i18n(
263  "<b>Thanks for using KMail2!</b>"
264  "<p>KMail2 uses a new storage technology that requires migration of your current KMail data and configuration.</p>\n"
265  "<p>The conversion process can take a lot of time (depending on the amount of email you have) and it <em>must not be interrupted</em>.</p>\n"
266  "<p>You can:</p><ul>"
267  "<li>Migrate now (be prepared to wait)</li>"
268  "<li>Skip the migration and start with fresh data and configuration</li>"
269  "<li>Cancel and exit KMail2.</li>"
270  "</ul>"
271  "<p><a href=\"http://userbase.kde.org/Akonadi\">More Information...</a></p>"
272  ), i18n( "KMail Migration" ), KGuiItem(i18n( "Migrate Now" )), KGuiItem(i18n( "Skip Migration" )), KStandardGuiItem::cancel(),
273  QString(), KMessageBox::Notify | KMessageBox::Dangerous | KMessageBox::AllowLink );
274  if ( choice == KMessageBox::Cancel )
275  exit( 1 );
276 
277  if ( choice != KMessageBox::Yes ) { // user skipped migration
278  // we only will make one attempt at this
279  migrationCfg.writeEntry( "Version", targetVersion );
280  migrationCfg.sync();
281 
282  return;
283  }
284 
285  kDebug() << "Performing Akonadi migration. Good luck!";
286  KProcess proc;
287  QStringList args = QStringList() << QLatin1String("--interactive-on-change");
288  const QString path = KStandardDirs::findExe( QLatin1String("kmail-migrator" ) );
289  proc.setProgram( path, args );
290  proc.start();
291  bool result = proc.waitForStarted();
292  if ( result ) {
293  result = proc.waitForFinished( -1 );
294  }
295  if ( result && proc.exitCode() == 0 ) {
296  kDebug() << "Akonadi migration has been successful";
297  } else {
298  // exit code 1 means it is already running, so we are probably called by a migrator instance
299  kError() << "Akonadi migration failed!";
300  kError() << "command was: " << proc.program();
301  kError() << "exit code: " << proc.exitCode();
302  kError() << "stdout: " << proc.readAllStandardOutput();
303  kError() << "stderr: " << proc.readAllStandardError();
304 
305  KMessageBox::error( 0, i18n("Migration to KMail 2 failed. In case you want to try again, run 'kmail-migrator --interactive' manually."),
306  i18n( "Migration Failed" ) );
307  return;
308  }
309  }
310  } else {
311  if (migrationCfg.hasKey("Enabled") && (migrationCfg.readEntry("Enabled", false) == false)) {
312  return;
313  }
314  migrationCfg.writeEntry( "Enabled", false );
315  migrationCfg.sync();
316  }
317 }
KMail::Composer::setFocusToSubject
virtual void setFocusToSubject()=0
Sets the focus to the subject line edit.
KMail::Util::mailingListSubscribe
bool mailingListSubscribe(const QSharedPointer< MailCommon::FolderCollection > &fd)
Definition: util.cpp:172
KMail::Util::migrateFromKMail1
void migrateFromKMail1()
Definition: util.cpp:237
QSharedPointer
Definition: collectionmailinglistpage.h:34
KMail::Util::mailingListPost
bool mailingListPost(const QSharedPointer< MailCommon::FolderCollection > &fd)
Definition: util.cpp:165
KMail::Util::mailingListUnsubscribe
bool mailingListUnsubscribe(const QSharedPointer< MailCommon::FolderCollection > &fd)
Definition: util.cpp:179
KMail::Util::putRepliesInSameFolder
Akonadi::Item::Id putRepliesInSameFolder(const Akonadi::Item &item)
Whether or not the mail item has the keep-reply-in-folder attribute set.
Definition: util.cpp:77
KMail::Util::launchAccountWizard
void launchAccountWizard(QWidget *)
Definition: util.cpp:89
QWidget
KMail::Util::handleClickedURL
bool handleClickedURL(const KUrl &url, const QSharedPointer< MailCommon::FolderCollection > &folder=QSharedPointer< MailCommon::FolderCollection >())
Handles a clicked URL, but only in case the viewer didn't handle it.
Definition: util.cpp:103
KMail::Util::mailingListsHandleURL
bool mailingListsHandleURL(const KUrl::List &lst, const QSharedPointer< MailCommon::FolderCollection > &folder)
Definition: util.cpp:140
KMail::Composer
Definition: composer.h:37
KMKernel::self
static KMKernel * self()
normal control stuff
Definition: kmkernel.cpp:1451
KMail::Util::mailingListsFromMessage
KMime::Types::Mailbox::List mailingListsFromMessage(const Akonadi::Item &item)
Returns any mailing list post addresses set on the parent collection (the mail folder) of the item...
Definition: util.cpp:62
KMail::Util::reduceQuery
void reduceQuery(QString &query)
Definition: util.cpp:227
KMail::Util::quoteL3Color
QColor quoteL3Color()
Definition: util.cpp:221
KMail::Util::quoteL1Color
QColor quoteL1Color()
Definition: util.cpp:211
util.h
KMail::Util::mailingListArchives
bool mailingListArchives(const QSharedPointer< MailCommon::FolderCollection > &fd)
Definition: util.cpp:186
KMail::Util::misspelledColor
QColor misspelledColor()
Definition: util.cpp:206
KMail::Util::mailingListHelp
bool mailingListHelp(const QSharedPointer< MailCommon::FolderCollection > &fd)
Definition: util.cpp:193
kmkernel.h
KMail::Util::lastEncryptAndSignState
void lastEncryptAndSignState(bool &lastEncrypt, bool &lastSign, const KMime::Message::Ptr &msg)
Definition: util.cpp:200
KMail::Util::quoteL2Color
QColor quoteL2Color()
Definition: util.cpp:216
KMail::Composer::New
Definition: composer.h:44
KMail::Composer::setCollectionForNewMessage
virtual void setCollectionForNewMessage(const Akonadi::Collection &folder)=0
QMap
Definition: kmmainwidget.h:58
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:52 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kmail

Skip menu "kmail"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal