mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-23 18:32:49 +00:00
kdeui: simplify KToolInvocation::invokeMailer()
thunderbird for one accepts a single URL (which probably is split into bits and checked for query items). it does not have "t", "s", etc. placeholders in its .desktop file so that whole map thing is not really working well Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
779f3b5635
commit
ae72f73768
5 changed files with 5 additions and 212 deletions
|
@ -141,7 +141,7 @@ void KAboutApplicationPersonListDelegate::launchUrl( QAction *action ) const
|
||||||
QString url = action->data().toString();
|
QString url = action->data().toString();
|
||||||
if( !url.isEmpty() ) {
|
if( !url.isEmpty() ) {
|
||||||
if( url.startsWith( "mailto:" ) )
|
if( url.startsWith( "mailto:" ) )
|
||||||
KToolInvocation::self()->invokeMailer( KUrl( url ) );
|
KToolInvocation::self()->invokeMailer( url );
|
||||||
else
|
else
|
||||||
KToolInvocation::self()->invokeBrowser( url );
|
KToolInvocation::self()->invokeBrowser( url );
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,46 +119,6 @@ void KToolInvocation::invokeHelp(const QString &anchor,
|
||||||
invokeBrowser(url.url());
|
invokeBrowser(url.url());
|
||||||
}
|
}
|
||||||
|
|
||||||
void KToolInvocation::invokeMailer(const QString &address, const QString &subject)
|
|
||||||
{
|
|
||||||
invokeMailer(address, QString(), subject, QString(), QStringList());
|
|
||||||
}
|
|
||||||
|
|
||||||
void KToolInvocation::invokeMailer(const KUrl &mailtoURL, bool allowAttachments)
|
|
||||||
{
|
|
||||||
QString address = mailtoURL.path();
|
|
||||||
QString subject;
|
|
||||||
QString cc;
|
|
||||||
QString body;
|
|
||||||
|
|
||||||
const QStringList queries = mailtoURL.query().mid(1).split(QLatin1Char('&'));
|
|
||||||
const QChar comma = QChar::fromLatin1(',');
|
|
||||||
QStringList attachURLs;
|
|
||||||
for (QStringList::ConstIterator it = queries.begin(); it != queries.end(); ++it)
|
|
||||||
{
|
|
||||||
QString q = (*it).toLower();
|
|
||||||
if (q.startsWith(QLatin1String("subject=")))
|
|
||||||
subject = KUrl::fromPercentEncoding((*it).mid(8).toLatin1());
|
|
||||||
else
|
|
||||||
if (q.startsWith(QLatin1String("cc=")))
|
|
||||||
cc = cc.isEmpty()? KUrl::fromPercentEncoding((*it).mid(3).toLatin1()): cc + comma + KUrl::fromPercentEncoding((*it).mid(3).toLatin1());
|
|
||||||
else
|
|
||||||
if (q.startsWith(QLatin1String("body=")))
|
|
||||||
body = KUrl::fromPercentEncoding((*it).mid(5).toLatin1());
|
|
||||||
else
|
|
||||||
if (allowAttachments && q.startsWith(QLatin1String("attach=")))
|
|
||||||
attachURLs.push_back(KUrl::fromPercentEncoding((*it).mid(7).toLatin1()));
|
|
||||||
else
|
|
||||||
if (allowAttachments && q.startsWith(QLatin1String("attachment=")))
|
|
||||||
attachURLs.push_back(KUrl::fromPercentEncoding((*it).mid(11).toLatin1()));
|
|
||||||
else
|
|
||||||
if (q.startsWith(QLatin1String("to=")))
|
|
||||||
address = address.isEmpty()? KUrl::fromPercentEncoding((*it).mid(3).toLatin1()): address + comma + KUrl::fromPercentEncoding((*it).mid(3).toLatin1());
|
|
||||||
}
|
|
||||||
|
|
||||||
invokeMailer(address, cc, subject, body, attachURLs);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool KToolInvocation::startServiceInternal(const char *_function,
|
bool KToolInvocation::startServiceInternal(const char *_function,
|
||||||
const QString &name, const QStringList &URLs,
|
const QString &name, const QStringList &URLs,
|
||||||
QWidget *window, bool temp, const QString &workdir)
|
QWidget *window, bool temp, const QString &workdir)
|
||||||
|
|
|
@ -60,33 +60,8 @@ public Q_SLOTS:
|
||||||
* Convenience method; invokes the standard email application.
|
* Convenience method; invokes the standard email application.
|
||||||
*
|
*
|
||||||
* @param address The destination address
|
* @param address The destination address
|
||||||
* @param subject Subject string. Can be QString().
|
|
||||||
* @param startup_id Ffor app startup notification, "0" for none
|
|
||||||
*/
|
*/
|
||||||
void invokeMailer(const QString &address, const QString &subject);
|
void invokeMailer(const QString &address);
|
||||||
|
|
||||||
/**
|
|
||||||
* Invokes the standard email application.
|
|
||||||
*
|
|
||||||
* @param mailtoURL A mailto URL.
|
|
||||||
* @param allowAttachments Whether attachments specified in mailtoURL should be honoured. The
|
|
||||||
* default is false; do not honor requests for attachments.
|
|
||||||
*/
|
|
||||||
void invokeMailer(const KUrl &mailtoURL, bool allowAttachments = false);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convenience method; invokes the standard email application.
|
|
||||||
*
|
|
||||||
* All parameters are optional.
|
|
||||||
*
|
|
||||||
* @param to The destination address.
|
|
||||||
* @param cc The Cc field
|
|
||||||
* @param subject Subject string
|
|
||||||
* @param body A string containing the body of the mail
|
|
||||||
* @param attachURLs List of URLs to be attached to the mail.
|
|
||||||
*/
|
|
||||||
void invokeMailer(const QString &to, const QString &cc, const QString &subject,
|
|
||||||
const QString &body, const QStringList &attachURLs = QStringList());
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invokes the user's preferred browser. Note that you should only do this when you know for
|
* Invokes the user's preferred browser. Note that you should only do this when you know for
|
||||||
|
@ -145,7 +120,7 @@ public:
|
||||||
* @param name Name of the program to start
|
* @param name Name of the program to start
|
||||||
* @param args Arguments to pass to the program
|
* @param args Arguments to pass to the program
|
||||||
* @param window Window to use for error reporting and job delegation
|
* @param window Window to use for error reporting and job delegation
|
||||||
* @param temp Whether argument is temporary file or not
|
* @param temp Whether any of the arguments is temporary file or not
|
||||||
* @return an error code indicating success (== 0) or failure (> 0)
|
* @return an error code indicating success (== 0) or failure (> 0)
|
||||||
*/
|
*/
|
||||||
bool startProgram(const QString &name, const QStringList &args = QStringList(),
|
bool startProgram(const QString &name, const QStringList &args = QStringList(),
|
||||||
|
|
|
@ -39,78 +39,7 @@
|
||||||
#include "kmimetypetrader.h"
|
#include "kmimetypetrader.h"
|
||||||
#include "kurl.h"
|
#include "kurl.h"
|
||||||
|
|
||||||
static QStringList splitEmailAddressList( const QString & aStr )
|
void KToolInvocation::invokeMailer(const QString &address)
|
||||||
{
|
|
||||||
// This is a copy of KPIM::splitEmailAddrList().
|
|
||||||
// Features:
|
|
||||||
// - always ignores quoted characters
|
|
||||||
// - ignores everything (including parentheses and commas)
|
|
||||||
// inside quoted strings
|
|
||||||
// - supports nested comments
|
|
||||||
// - ignores everything (including double quotes and commas)
|
|
||||||
// inside comments
|
|
||||||
|
|
||||||
QStringList list;
|
|
||||||
|
|
||||||
if (aStr.isEmpty())
|
|
||||||
return list;
|
|
||||||
|
|
||||||
QString addr;
|
|
||||||
uint addrstart = 0;
|
|
||||||
int commentlevel = 0;
|
|
||||||
bool insidequote = false;
|
|
||||||
|
|
||||||
for (int index=0; index<aStr.length(); index++) {
|
|
||||||
// the following conversion to latin1 is o.k. because
|
|
||||||
// we can safely ignore all non-latin1 characters
|
|
||||||
switch (aStr[index].toLatin1()) {
|
|
||||||
case '"' : // start or end of quoted string
|
|
||||||
if (commentlevel == 0)
|
|
||||||
insidequote = !insidequote;
|
|
||||||
break;
|
|
||||||
case '(' : // start of comment
|
|
||||||
if (!insidequote)
|
|
||||||
commentlevel++;
|
|
||||||
break;
|
|
||||||
case ')' : // end of comment
|
|
||||||
if (!insidequote) {
|
|
||||||
if (commentlevel > 0)
|
|
||||||
commentlevel--;
|
|
||||||
else {
|
|
||||||
// kDebug() << "Error in address splitting: Unmatched ')'";
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case '\\' : // quoted character
|
|
||||||
index++; // ignore the quoted character
|
|
||||||
break;
|
|
||||||
case ',' :
|
|
||||||
if (!insidequote && (commentlevel == 0)) {
|
|
||||||
addr = aStr.mid(addrstart, index-addrstart);
|
|
||||||
if (!addr.isEmpty())
|
|
||||||
list += addr.simplified();
|
|
||||||
addrstart = index+1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// append the last address to the list
|
|
||||||
if (!insidequote && (commentlevel == 0)) {
|
|
||||||
addr = aStr.mid(addrstart, aStr.length()-addrstart);
|
|
||||||
if (!addr.isEmpty())
|
|
||||||
list += addr.simplified();
|
|
||||||
}
|
|
||||||
//else
|
|
||||||
// kDebug() << "Error in address splitting: "
|
|
||||||
// << "Unexpected end of address list";
|
|
||||||
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
void KToolInvocation::invokeMailer(const QString &to, const QString &cc,
|
|
||||||
const QString &subject, const QString &body,
|
|
||||||
const QStringList &attachURLs)
|
|
||||||
{
|
{
|
||||||
KConfig config(QString::fromLatin1("emaildefaults"));
|
KConfig config(QString::fromLatin1("emaildefaults"));
|
||||||
KConfigGroup profileGrp(&config, "General");
|
KConfigGroup profileGrp(&config, "General");
|
||||||
|
@ -130,77 +59,6 @@ void KToolInvocation::invokeMailer(const QString &to, const QString &cc,
|
||||||
|
|
||||||
QStringList cmdTokens = KShell::splitArgs(command);
|
QStringList cmdTokens = KShell::splitArgs(command);
|
||||||
QString cmd = cmdTokens.takeFirst();
|
QString cmd = cmdTokens.takeFirst();
|
||||||
|
|
||||||
KUrl url;
|
|
||||||
//QStringList qry;
|
|
||||||
if (!to.isEmpty())
|
|
||||||
{
|
|
||||||
QStringList tos = splitEmailAddressList( to );
|
|
||||||
url.setPath( tos.first() );
|
|
||||||
tos.erase( tos.begin() );
|
|
||||||
for (QStringList::ConstIterator it = tos.constBegin(); it != tos.constEnd(); ++it)
|
|
||||||
url.addQueryItem(QString::fromLatin1("to"), *it);
|
|
||||||
//qry.append( "to=" + QLatin1String(KUrl::toPercentEncoding( *it ) ));
|
|
||||||
}
|
|
||||||
const QStringList ccs = splitEmailAddressList( cc );
|
|
||||||
for (QStringList::ConstIterator it = ccs.constBegin(); it != ccs.constEnd(); ++it)
|
|
||||||
url.addQueryItem(QString::fromLatin1("cc"), *it);
|
|
||||||
//qry.append( "cc=" + QLatin1String(KUrl::toPercentEncoding( *it ) ));
|
|
||||||
for (QStringList::ConstIterator it = attachURLs.constBegin(); it != attachURLs.constEnd(); ++it)
|
|
||||||
url.addQueryItem(QString::fromLatin1("attach"), *it);
|
|
||||||
//qry.append( "attach=" + QLatin1String(KUrl::toPercentEncoding( *it ) ));
|
|
||||||
if (!subject.isEmpty())
|
|
||||||
url.addQueryItem(QString::fromLatin1("subject"), subject);
|
|
||||||
//qry.append( "subject=" + QLatin1String(KUrl::toPercentEncoding( subject ) ));
|
|
||||||
if (!body.isEmpty())
|
|
||||||
url.addQueryItem(QString::fromLatin1("body"), body);
|
|
||||||
//qry.append( "body=" + QLatin1String(KUrl::toPercentEncoding( body ) ));
|
|
||||||
//url.setQuery( qry.join( "&" ) );
|
|
||||||
|
|
||||||
if ( ! (to.isEmpty() && (!url.hasQuery())) )
|
|
||||||
url.setScheme(QString::fromLatin1("mailto"));
|
|
||||||
|
|
||||||
QHash<QChar, QString> keyMap;
|
|
||||||
keyMap.insert(QLatin1Char('t'), to);
|
|
||||||
keyMap.insert(QLatin1Char('s'), subject);
|
|
||||||
keyMap.insert(QLatin1Char('c'), cc);
|
|
||||||
keyMap.insert(QLatin1Char('B'), body);
|
|
||||||
keyMap.insert(QLatin1Char('u'), url.url());
|
|
||||||
|
|
||||||
QString attachlist = attachURLs.join(QString::fromLatin1(","));
|
|
||||||
attachlist.prepend(QLatin1Char('\''));
|
|
||||||
attachlist.append(QLatin1Char('\''));
|
|
||||||
keyMap.insert(QLatin1Char('A'), attachlist);
|
|
||||||
|
|
||||||
for (QStringList::Iterator it = cmdTokens.begin(); it != cmdTokens.end(); )
|
|
||||||
{
|
|
||||||
if (*it == QLatin1String("%A"))
|
|
||||||
{
|
|
||||||
if (it == cmdTokens.begin()) // better safe than sorry ...
|
|
||||||
continue;
|
|
||||||
QStringList::ConstIterator urlit = attachURLs.begin();
|
|
||||||
QStringList::ConstIterator urlend = attachURLs.end();
|
|
||||||
if ( urlit != urlend )
|
|
||||||
{
|
|
||||||
QStringList::Iterator previt = it;
|
|
||||||
--previt;
|
|
||||||
*it = *urlit;
|
|
||||||
++it;
|
|
||||||
while ( ++urlit != urlend )
|
|
||||||
{
|
|
||||||
cmdTokens.insert( it, *previt );
|
|
||||||
cmdTokens.insert( it, *urlit );
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
--it;
|
|
||||||
it = cmdTokens.erase( cmdTokens.erase( it ) );
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
*it = KMacroExpander::expandMacros(*it, keyMap);
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
startProgram(cmd, cmdTokens);
|
startProgram(cmd, cmdTokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ int main( int argc, char **argv )
|
||||||
if ( args->count() != 1 )
|
if ( args->count() != 1 )
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
KToolInvocation::self()->invokeMailer(KUrl(args->arg(0)), true);
|
KToolInvocation::self()->invokeMailer(args->arg(0));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue