diff options
author | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
---|---|---|
committer | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
commit | 460c52653ab0dcca6f19a4f492ed2c5e4e963ab0 (patch) | |
tree | 67208f7c145782a7e90b123b982ca78d88cc2c87 /kabc/kabc2mutt | |
download | tdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.tar.gz tdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.zip |
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'kabc/kabc2mutt')
-rw-r--r-- | kabc/kabc2mutt/Makefile.am | 13 | ||||
-rw-r--r-- | kabc/kabc2mutt/README | 19 | ||||
-rw-r--r-- | kabc/kabc2mutt/kabc2mutt.cpp | 160 | ||||
-rw-r--r-- | kabc/kabc2mutt/kabc2mutt.h | 56 | ||||
-rw-r--r-- | kabc/kabc2mutt/main.cpp | 80 |
5 files changed, 328 insertions, 0 deletions
diff --git a/kabc/kabc2mutt/Makefile.am b/kabc/kabc2mutt/Makefile.am new file mode 100644 index 000000000..f8a201cfd --- /dev/null +++ b/kabc/kabc2mutt/Makefile.am @@ -0,0 +1,13 @@ +INCLUDES = -I$(includedir)/kabc $(all_includes) + +METASOURCES = AUTO + +bin_PROGRAMS = kabc2mutt + +kabc2mutt_LDFLAGS = $(all_libraries) $(KDE_RPATH) +kabc2mutt_LDADD = -lkabc $(LIB_KDECORE) +kabc2mutt_SOURCES = kabc2mutt.cpp main.cpp + +messages: rc.cpp + $(XGETTEXT) *.cpp -o $(podir)/kabc2mutt.pot + diff --git a/kabc/kabc2mutt/README b/kabc/kabc2mutt/README new file mode 100644 index 000000000..5d82fdfec --- /dev/null +++ b/kabc/kabc2mutt/README @@ -0,0 +1,19 @@ +KAbc2Mutt +----------- + +kabc2mutt is a small programm to make the email addresses, stored in +kaddressbook, available in mutt. +You only have to add the following line to your .muttrc + + source "kabc2mutt |" + +You can also configure mutt to query kabc2mutt when typing a substring +of an email address (see the docu for query_command). +To do this, add the following line to your .muttrc + + set query_command = "kabc2mutt --query '%s' --format query" + +Have fun with it + +Ciao, +Tobias diff --git a/kabc/kabc2mutt/kabc2mutt.cpp b/kabc/kabc2mutt/kabc2mutt.cpp new file mode 100644 index 000000000..84f4626bb --- /dev/null +++ b/kabc/kabc2mutt/kabc2mutt.cpp @@ -0,0 +1,160 @@ +/* + KAbc2Mutt + + Copyright (c) 2003 - 2004 Tobias Koenig <tokoe@kde.org> + + This program is free software; you can redistribute it and/or + modify it under the terms of version 2 of the GNU General Public + License as published by the Free Software Foundation. + + 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. +*/ + +#include <kabc/distributionlist.h> +#include <kapplication.h> +#include <klocale.h> + +#include <qregexp.h> + +#include <iostream> + +#include "kabc2mutt.h" + +static std::ostream & operator<< ( std::ostream &os, const QString &s ); + +KABC2Mutt::KABC2Mutt( QObject *parent, const char *name ) + : QObject( parent, name ), mFormat( Aliases ), + mIgnoreCase( false ), mAllAddresses( false ), + mAlternateKeyFormat( false ), + mAddressBook( 0 ) +{ +} + +void KABC2Mutt::run() +{ + mAddressBook = KABC::StdAddressBook::self( true ); + KABC::StdAddressBook::setAutomaticSave( false ); + + connect( mAddressBook, SIGNAL( addressBookChanged( AddressBook* ) ), + this, SLOT( loadingFinished() ) ); +} + +void KABC2Mutt::loadingFinished() +{ + // print addressees + KABC::AddressBook::ConstIterator iaddr; + for ( iaddr = mAddressBook->begin(); iaddr != mAddressBook->end(); ++iaddr ) { + const QString name = (*iaddr).givenName() + ' ' + (*iaddr).familyName(); + if ( !mQuery.isEmpty() ) { + bool match = (name.find(mQuery, 0, mIgnoreCase) > -1) || + ((*iaddr).preferredEmail().find( mQuery, 0, mIgnoreCase ) > -1 ); + if ( !match ) + continue; + } + + const QStringList &allAddresses = (*iaddr).emails(); + QStringList::const_iterator from, to; + bool multiple = false; + + if ( mAllAddresses ) { + // use all entries + multiple = allAddresses.size() > 1; + from = allAddresses.begin(); + to = allAddresses.end(); + } else { + // use only the first entry, the one returned by preferredEmail() + from = to = allAddresses.begin(); // start with empty list + if ( to != allAddresses.end() ) + ++to; + } + + size_t index = 0; + if ( mFormat == Aliases ) { + static const QChar space = QChar( ' ' ); + static const QChar underscore = QChar( '_' ); + + QString key; + if ( !mAlternateKeyFormat ) + key = (*iaddr).givenName().left( 3 ) + (*iaddr).familyName().left( 3 ); + else + if ( !(*iaddr).familyName().isEmpty() ) + key = (*iaddr).givenName().left( 1 ).lower() + + (*iaddr).familyName().lower().replace( space, underscore ); + else + key = (*iaddr).givenName().lower().replace( space, underscore ); + + while ( from != to ) { + std::cout << "alias " << key; + if ( index ) + std::cout << index; + std::cout << '\t' << name << " <" << (*from) << '>' << std::endl; + ++index; + ++from; + } + + if ( !(*iaddr).nickName().isEmpty() ) { + std::cout << "alias " + << (*iaddr).nickName().lower().replace( space, underscore ) + << '\t' << name << " <" + << (*iaddr).preferredEmail() << '>' << std::endl; + } + } else { + while ( from != to ) { + std::cout << (*from) << '\t' << name; + if ( multiple ) { + if ( index ) + std::cout << "\t#" << index; + else + std::cout << '\t' << i18n( "preferred" ); + ++index; + } + std::cout << std::endl; + ++from; + } + } + } + + // print all distribution lists + KABC::DistributionListManager manager( mAddressBook ); + manager.load(); + + QStringList dists = manager.listNames(); + for ( QStringList::Iterator iaddr = dists.begin(); iaddr != dists.end(); ++iaddr ) { + KABC::DistributionList *list = manager.list( *iaddr ); + if ( list ) { + if ( !mQuery.isEmpty() ) { + bool match = ((*iaddr).find(mQuery) > -1); + if ( !match ) + continue; + } + + QStringList emails = list->emails(); + if ( emails.isEmpty() ) + continue; + + if ( mFormat == Aliases ) { + std::cout << "alias " << (*iaddr).replace( QRegExp( " " ), "_" ) + << '\t' << emails.join( "," ) << std::endl; + } else { + std::cout << emails.join( "," ) << '\t' << (*iaddr) << '\t' << std::endl; + } + } + } + + kapp->quit(); +} + +static std::ostream & operator<< ( std::ostream &os, const QString &s ) +{ + os << s.local8Bit().data(); + return os; +} + +#include "kabc2mutt.moc" diff --git a/kabc/kabc2mutt/kabc2mutt.h b/kabc/kabc2mutt/kabc2mutt.h new file mode 100644 index 000000000..65f4d06f4 --- /dev/null +++ b/kabc/kabc2mutt/kabc2mutt.h @@ -0,0 +1,56 @@ +/* + KAbc2Mutt + + Copyright (c) 2003 - 2004 Tobias Koenig <tokoe@kde.org> + + This program is free software; you can redistribute it and/or + modify it under the terms of version 2 of the GNU General Public + License as published by the Free Software Foundation. + + 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. +*/ + +#ifndef KABC2MUTT_H +#define KABC2MUTT_H + +#include <kabc/stdaddressbook.h> + +class KABC2Mutt : public QObject +{ + Q_OBJECT + + public: + + enum Format { Aliases, Query }; + + KABC2Mutt( QObject *parent, const char *name = 0 ); + + void setQuery( const QString &query ) { mQuery = query; } + void setFormat( Format format ) { mFormat = format; } + void setIgnoreCase( bool ignoreCase ) { mIgnoreCase = ignoreCase; } + void setAllAddresses( bool allAddresses ) { mAllAddresses = allAddresses; } + void setAlternateKeyFormat( bool alternateKeyFormat ) { mAlternateKeyFormat = alternateKeyFormat; } + + void run(); + + private slots: + void loadingFinished(); + + private: + QString mQuery; + Format mFormat; + bool mIgnoreCase; + bool mAllAddresses; + bool mAlternateKeyFormat; + + KABC::AddressBook *mAddressBook; +}; + +#endif diff --git a/kabc/kabc2mutt/main.cpp b/kabc/kabc2mutt/main.cpp new file mode 100644 index 000000000..8da8f5491 --- /dev/null +++ b/kabc/kabc2mutt/main.cpp @@ -0,0 +1,80 @@ +/* + KAbc2Mutt + + Copyright (c) 2003 - 2004 Tobias Koenig <tokoe@kde.org> + + This program is free software; you can redistribute it and/or + modify it under the terms of version 2 of the GNU General Public + License as published by the Free Software Foundation. + + 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. +*/ + +#include <kapplication.h> +#include <kcmdlineargs.h> +#include <klocale.h> + +#include <iostream> + +#include "kabc2mutt.h" + +static const char version[] = "0.2"; +static const char appName[] = "kabc2mutt"; +static const char programName[] = I18N_NOOP( "kabc2mutt" ); +static const char description[] = I18N_NOOP( "kabc - mutt converter" ); + +static KCmdLineOptions k2moptions[] = +{ + { "query <substring>", I18N_NOOP( "Only show contacts where name or address matches <substring>" ), 0 }, + { "format <format>", I18N_NOOP( "Default format is 'alias'. 'query' returns email<tab>name<tab>, as needed by mutt's query_command" ), "alias" }, + { "alternate-key-format", I18N_NOOP( "Default key format is 'JohDoe', this option turns it into 'jdoe'" ), 0 }, + { "ignore-case", I18N_NOOP( "Make queries case insensitive" ), 0 }, + { "all-addresses", I18N_NOOP( "Return all mail addresses, not just the preferred one" ), 0}, + KCmdLineLastOption +}; + + +int main( int argc, char **argv ) +{ + KApplication::disableAutoDcopRegistration(); + KCmdLineArgs::init( argc, argv, appName, programName, description, version ); + KCmdLineArgs::addCmdLineOptions( k2moptions ); + + KApplication app( false, false ); + + KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); + + KABC2Mutt *object = new KABC2Mutt( 0 ); + + // Handle --format option + object->setFormat( (args->getOption("format") == "query") ? KABC2Mutt::Query : KABC2Mutt::Aliases ); + + // Handle --alternate-key-format option + object->setAlternateKeyFormat( args->isSet( "alternate-key-format" ) ); + + // Handle --all-addresses option + object->setAllAddresses( args->isSet( "all-addresses" ) ); + + // Handle --query option + const QString subString = QString::fromLocal8Bit( args->getOption( "query" ) ); + if ( !subString.isEmpty() ) { + // Mutt wants a first line with some status message on it + // See http://mutt.org/doc/manual/manual-4.html#ss4.5 + std::cout << i18n( "Searching KDE addressbook" ).latin1() << std::endl; + } + object->setQuery( subString ); + + // Handle --ignore-case option + object->setIgnoreCase( !args->isSet( "ignore-case" ) ); + + object->run(); + + return app.exec(); +} |