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

kmail

  • sources
  • kde-4.14
  • 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 <KLocalizedString>
49 #include <KProcess>
50 
51 #include <KStandardDirs>
52 
53 #include <QProcess>
54 #include <QFileInfo>
55 
56 #include "foldercollection.h"
57 
58 using namespace MailCommon;
59 
60 
61 KMime::Types::Mailbox::List KMail::Util::mailingListsFromMessage( const Akonadi::Item& item )
62 {
63  KMime::Types::Mailbox::List addresses;
64  // determine the mailing list posting address
65  Akonadi::Collection parentCollection = item.parentCollection();
66  if ( parentCollection.isValid() ) {
67  const QSharedPointer<FolderCollection> fd = FolderCollection::forCollection( parentCollection, false );
68  if ( fd->isMailingListEnabled() && !fd->mailingListPostAddress().isEmpty() ) {
69  addresses << MessageCore::StringUtil::mailboxFromUnicodeString( fd->mailingListPostAddress() );
70  }
71  }
72 
73  return addresses;
74 }
75 
76 Akonadi::Item::Id KMail::Util::putRepliesInSameFolder( const Akonadi::Item& item )
77 {
78  Akonadi::Collection parentCollection = item.parentCollection();
79  if ( parentCollection.isValid() ) {
80  const QSharedPointer<FolderCollection> fd = FolderCollection::forCollection( parentCollection, false );
81  if( fd->putRepliesInSameFolder() ) {
82  return parentCollection.id();
83  }
84  }
85  return -1;
86 }
87 
88 bool KMail::Util::handleClickedURL( const KUrl &url, const QSharedPointer<MailCommon::FolderCollection> &folder )
89 {
90  if ( url.protocol() == QLatin1String( "mailto" ) ) {
91  KMime::Message::Ptr msg ( new KMime::Message );
92  uint identity = !folder.isNull() ? folder->identity() : 0;
93  MessageHelper::initHeader( msg, KMKernel::self()->identityManager(), identity );
94  msg->contentType()->setCharset("utf-8");
95 
96  QMap<QString, QString> fields = MessageCore::StringUtil::parseMailtoUrl( url );
97 
98  msg->to()->fromUnicodeString( fields.value( QLatin1String("to") ),"utf-8" );
99  if ( !fields.value( QLatin1String("subject") ).isEmpty() )
100  msg->subject()->fromUnicodeString( fields.value( QLatin1String("subject") ),"utf-8" );
101  if ( !fields.value( QLatin1String("body") ).isEmpty() )
102  msg->setBody( fields.value( QLatin1String("body") ).toUtf8() );
103  if ( !fields.value( QLatin1String("cc" )).isEmpty() )
104  msg->cc()->fromUnicodeString( fields.value( QLatin1String("cc") ),"utf-8" );
105 
106  if ( !folder.isNull() ) {
107  TemplateParser::TemplateParser parser( msg, TemplateParser::TemplateParser::NewMessage );
108  parser.setIdentityManager( KMKernel::self()->identityManager() );
109  parser.process( msg, folder->collection() );
110  }
111 
112  KMail::Composer * win = KMail::makeComposer( msg, false, false, KMail::Composer::New, identity );
113  win->setFocusToSubject();
114  if ( !folder.isNull() ) {
115  win->setCollectionForNewMessage( folder->collection() );
116  }
117  win->show();
118  return true;
119  } else {
120  kWarning() << "Can't handle URL:" << url;
121  return false;
122  }
123 }
124 
125 bool KMail::Util::mailingListsHandleURL( const KUrl::List& lst,const QSharedPointer<MailCommon::FolderCollection> &folder )
126 {
127  const QString handler = ( folder->mailingList().handler() == MailingList::KMail )
128  ? QLatin1String( "mailto" ) : QLatin1String( "https" );
129 
130  KUrl urlToHandle;
131  KUrl::List::ConstIterator end( lst.constEnd() );
132  for ( KUrl::List::ConstIterator itr = lst.constBegin(); itr != end; ++itr ) {
133  if ( handler == (*itr).protocol() ) {
134  urlToHandle = *itr;
135  break;
136  }
137  }
138  if ( urlToHandle.isEmpty() && !lst.empty() ) {
139  urlToHandle = lst.first();
140  }
141 
142  if ( !urlToHandle.isEmpty() ) {
143  return KMail::Util::handleClickedURL( urlToHandle, folder );
144  } else {
145  kWarning()<< "Can't handle url";
146  return false;
147  }
148 }
149 
150 bool KMail::Util::mailingListPost( const QSharedPointer<MailCommon::FolderCollection> &fd )
151 {
152  if ( fd )
153  return KMail::Util::mailingListsHandleURL( fd->mailingList().postUrls(),fd );
154  return false;
155 }
156 
157 bool KMail::Util::mailingListSubscribe( const QSharedPointer<MailCommon::FolderCollection> &fd )
158 {
159  if ( fd )
160  return KMail::Util::mailingListsHandleURL( fd->mailingList().subscribeUrls(),fd );
161  return false;
162 }
163 
164 bool KMail::Util::mailingListUnsubscribe( const QSharedPointer<MailCommon::FolderCollection> &fd )
165 {
166  if ( fd )
167  return KMail::Util::mailingListsHandleURL( fd->mailingList().unsubscribeUrls(),fd );
168  return false;
169 }
170 
171 bool KMail::Util::mailingListArchives( const QSharedPointer<MailCommon::FolderCollection> &fd )
172 {
173  if ( fd )
174  return KMail::Util::mailingListsHandleURL( fd->mailingList().archiveUrls(),fd );
175  return false;
176 }
177 
178 bool KMail::Util::mailingListHelp( const QSharedPointer<MailCommon::FolderCollection> &fd )
179 {
180  if ( fd )
181  return KMail::Util::mailingListsHandleURL( fd->mailingList().helpUrls(),fd );
182  return false;
183 }
184 
185 void KMail::Util::lastEncryptAndSignState(bool &lastEncrypt, bool &lastSign, const KMime::Message::Ptr& msg)
186 {
187  lastSign = KMime::isSigned(msg.get());
188  lastEncrypt = KMime::isEncrypted(msg.get());
189 }
190 
191 QColor KMail::Util::misspelledColor()
192 {
193  return QColor(Qt::red);
194 }
195 
196 QColor KMail::Util::quoteL1Color()
197 {
198  return QColor( 0x00, 0x80, 0x00 );
199 }
200 
201 QColor KMail::Util::quoteL2Color()
202 {
203  return QColor( 0x00, 0x70, 0x00 );
204 }
205 
206 QColor KMail::Util::quoteL3Color()
207 {
208  return QColor( 0x00, 0x60, 0x00 );
209 }
210 
211 
212 void KMail::Util::migrateFromKMail1()
213 {
214  // Akonadi migration
215  // check if there is something to migrate at all
216  bool needMigration = true;
217  KConfig oldKMailConfig( QLatin1String("kmailrc"), KConfig::NoGlobals );
218  if ( oldKMailConfig.hasGroup("General") ||
219  ( oldKMailConfig.groupList().count() == 1 &&
220  oldKMailConfig.groupList().first() == QLatin1String("$Version") ) ) {
221  const QFileInfo oldDataDirFileInfo( KStandardDirs::locateLocal( "data", QLatin1String("kmail") ) );
222  if ( !oldDataDirFileInfo.exists() || !oldDataDirFileInfo.isDir() ) {
223  // neither config or data, the migrator cannot do anything useful anyways
224  needMigration = false;
225  }
226  } else {
227  needMigration = false;
228  }
229 
230  KConfig config( QLatin1String("kmail-migratorrc") );
231  KConfigGroup migrationCfg( &config, "Migration" );
232  if ( needMigration ) {
233  const bool enabled = migrationCfg.readEntry( "Enabled", false );
234  const int currentVersion = migrationCfg.readEntry( "Version", 0 );
235  const int targetVersion = migrationCfg.readEntry( "TargetVersion", 1 );
236  if ( enabled && currentVersion < targetVersion ) {
237  const int choice = KMessageBox::questionYesNoCancel( 0, i18n(
238  "<b>Thanks for using KMail2!</b>"
239  "<p>KMail2 uses a new storage technology that requires migration of your current KMail data and configuration.</p>\n"
240  "<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"
241  "<p>You can:</p><ul>"
242  "<li>Migrate now (be prepared to wait)</li>"
243  "<li>Skip the migration and start with fresh data and configuration</li>"
244  "<li>Cancel and exit KMail2.</li>"
245  "</ul>"
246  "<p><a href=\"http://userbase.kde.org/Akonadi\">More Information...</a></p>"
247  ), i18n( "KMail Migration" ), KGuiItem(i18n( "Migrate Now" )), KGuiItem(i18n( "Skip Migration" )), KStandardGuiItem::cancel(),
248  QString(), KMessageBox::Notify | KMessageBox::Dangerous | KMessageBox::AllowLink );
249  if ( choice == KMessageBox::Cancel )
250  exit( 1 );
251 
252  if ( choice != KMessageBox::Yes ) { // user skipped migration
253  // we only will make one attempt at this
254  migrationCfg.writeEntry( "Version", targetVersion );
255  migrationCfg.sync();
256 
257  return;
258  }
259 
260  kDebug() << "Performing Akonadi migration. Good luck!";
261  KProcess proc;
262  QStringList args = QStringList() << QLatin1String("--interactive-on-change");
263  const QString path = KStandardDirs::findExe( QLatin1String("kmail-migrator" ) );
264  proc.setProgram( path, args );
265  proc.start();
266  bool result = proc.waitForStarted();
267  if ( result ) {
268  result = proc.waitForFinished( -1 );
269  }
270  if ( result && proc.exitCode() == 0 ) {
271  kDebug() << "Akonadi migration has been successful";
272  } else {
273  // exit code 1 means it is already running, so we are probably called by a migrator instance
274  kError() << "Akonadi migration failed!";
275  kError() << "command was: " << proc.program();
276  kError() << "exit code: " << proc.exitCode();
277  kError() << "stdout: " << proc.readAllStandardOutput();
278  kError() << "stderr: " << proc.readAllStandardError();
279 
280  KMessageBox::error( 0, i18n("Migration to KMail 2 failed. In case you want to try again, run 'kmail-migrator --interactive' manually."),
281  i18n( "Migration Failed" ) );
282  return;
283  }
284  }
285  } else {
286  if (migrationCfg.hasKey("Enabled") && (migrationCfg.readEntry("Enabled", false) == false)) {
287  return;
288  }
289  migrationCfg.writeEntry( "Enabled", false );
290  migrationCfg.sync();
291  }
292 }
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:157
KMail::Util::migrateFromKMail1
void migrateFromKMail1()
Definition: util.cpp:212
KMail::Util::mailingListPost
bool mailingListPost(const QSharedPointer< MailCommon::FolderCollection > &fd)
Definition: util.cpp:150
KMail::Util::mailingListUnsubscribe
bool mailingListUnsubscribe(const QSharedPointer< MailCommon::FolderCollection > &fd)
Definition: util.cpp:164
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:76
QMap
Definition: kmmainwidget.h:66
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:88
KMail::Util::mailingListsHandleURL
bool mailingListsHandleURL(const KUrl::List &lst, const QSharedPointer< MailCommon::FolderCollection > &folder)
Definition: util.cpp:125
KMail::Composer
Definition: composer.h:39
KMKernel::self
static KMKernel * self()
normal control stuff
Definition: kmkernel.cpp:1471
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:61
QSharedPointer
Definition: collectionmailinglistpage.h:34
QFileInfo::isDir
bool isDir() const
KMail::Util::quoteL3Color
QColor quoteL3Color()
Definition: util.cpp:206
QString::isEmpty
bool isEmpty() const
KMail::Util::quoteL1Color
QColor quoteL1Color()
Definition: util.cpp:196
QString
QColor
util.h
KMail::Util::mailingListArchives
bool mailingListArchives(const QSharedPointer< MailCommon::FolderCollection > &fd)
Definition: util.cpp:171
QStringList
KMail::Util::misspelledColor
QColor misspelledColor()
Definition: util.cpp:191
QFileInfo
QFileInfo::exists
bool exists() const
KMail::Util::mailingListHelp
bool mailingListHelp(const QSharedPointer< MailCommon::FolderCollection > &fd)
Definition: util.cpp:178
kmkernel.h
QLatin1String
KMail::Util::lastEncryptAndSignState
void lastEncryptAndSignState(bool &lastEncrypt, bool &lastSign, const KMime::Message::Ptr &msg)
Definition: util.cpp:185
KMail::Util::quoteL2Color
QColor quoteL2Color()
Definition: util.cpp:201
KMail::Composer::New
Definition: composer.h:46
QSharedPointer::isNull
bool isNull() const
KMail::Composer::setCollectionForNewMessage
virtual void setCollectionForNewMessage(const Akonadi::Collection &folder)=0
QMap::value
const T value(const Key &key) const
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:33 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
  • pimprint

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