templateinterface.cpp

00001 /* This file is part of the KDE libraries
00002   Copyright (C) 2004 Joseph Wenninger <jowenn@kde.org>
00003 
00004   This library is free software; you can redistribute it and/or
00005   modify it under the terms of the GNU Library General Public
00006   License version 2 as published by the Free Software Foundation.
00007 
00008   This library is distributed in the hope that it will be useful,
00009   but WITHOUT ANY WARRANTY; without even the implied warranty of
00010   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011   Library General Public License for more details.
00012 
00013   You should have received a copy of the GNU Library General Public License
00014   along with this library; see the file COPYING.LIB.  If not, write to
00015   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016   Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "templateinterface.h"
00020 #include "document.h"
00021 #include <stdaddressbook.h>
00022 #include <addressee.h>
00023 #include <qstring.h>
00024 #include <klocale.h>
00025 #include <kglobal.h>
00026 #include <qdatetime.h>
00027 #include <qregexp.h>
00028 #include <kmessagebox.h>
00029 #include <kcalendarsystem.h>
00030 #include <unistd.h>
00031 
00032 #include <kdebug.h>
00033 
00034 using namespace KTextEditor;
00035 
00036 unsigned int TemplateInterface::globalTemplateInterfaceNumber = 0;
00037 
00038 TemplateInterface::TemplateInterface()
00039 {
00040   myTemplateInterfaceNumber = globalTemplateInterfaceNumber++;
00041 }
00042 
00043 TemplateInterface::~TemplateInterface()
00044 {}
00045 
00046 uint TemplateInterface::templateInterfaceNumber () const
00047 {
00048   return myTemplateInterfaceNumber;
00049 }
00050 
00051 void TemplateInterface::setTemplateInterfaceDCOPSuffix ( const QCString &suffix )
00052 {}
00053 
00054 #define INITKABC do { \
00055   if (addrBook==0) { \
00056     addrBook=KABC::StdAddressBook::self(); \
00057     userAddress=addrBook->whoAmI(); \
00058     if (userAddress.isEmpty()) { \
00059       /*instead of sorry add he posibility to launch kaddressbook here*/ \
00060       KMessageBox::sorry(parentWindow,i18n("The template needs information about you, please set your identity in your addressbook"));\
00061       return false; \
00062     } \
00063   } \
00064 } while(false)
00065 
00066 bool TemplateInterface::expandMacros( QMap<QString, QString> &map, QWidget *parentWindow )
00067 {
00068   KABC::StdAddressBook *addrBook = 0;
00069   KABC::Addressee userAddress;
00070   QDateTime datetime = QDateTime::currentDateTime();
00071   QDate date = datetime.date();
00072   QTime time = datetime.time();
00073 
00074   QMap<QString,QString>::Iterator it;
00075   for ( it = map.begin(); it != map.end(); ++it )
00076   {
00077     QString placeholder = it.key();
00078     if ( map[ placeholder ].isEmpty() )
00079     {
00080       if ( placeholder == "index" ) map[ placeholder ] = "i";
00081       else if ( placeholder == "loginname" )
00082       {}
00083       else if ( placeholder == "firstname" )
00084       {
00085         INITKABC;
00086         map[ placeholder ] = userAddress.givenName();
00087       }
00088       else if ( placeholder == "lastname" )
00089       {
00090         INITKABC;
00091         map[ placeholder ] = userAddress.familyName();
00092       }
00093       else if ( placeholder == "fullname" )
00094       {
00095         INITKABC;
00096         map[ placeholder ] = userAddress.assembledName();
00097       }
00098       else if ( placeholder == "email" )
00099       {
00100         INITKABC;
00101         map[ placeholder ] = userAddress.preferredEmail();
00102       }
00103       else if ( placeholder == "date" )
00104       {
00105         map[ placeholder ] = KGlobal::locale() ->formatDate( date, true );
00106       }
00107       else if ( placeholder == "time" )
00108       {
00109         map[ placeholder ] = KGlobal::locale() ->formatTime( time, true, false );
00110       }
00111       else if ( placeholder == "year" )
00112       {
00113         map[ placeholder ] = KGlobal::locale() ->calendar() ->yearString( date, false );
00114       }
00115       else if ( placeholder == "month" )
00116       {
00117         map[ placeholder ] = QString::number( KGlobal::locale() ->calendar() ->month( date ) );
00118       }
00119       else if ( placeholder == "day" )
00120       {
00121         map[ placeholder ] = QString::number( KGlobal::locale() ->calendar() ->day( date ) );
00122       }
00123       else if ( placeholder == "hostname" )
00124       {
00125         char hostname[ 256 ];
00126         hostname[ 0 ] = 0;
00127         gethostname( hostname, 255 );
00128         hostname[ 255 ] = 0;
00129         map[ placeholder ] = QString::fromLocal8Bit( hostname );
00130       }
00131       else if ( placeholder == "cursor" )
00132       {
00133         map[ placeholder ] = "|";
00134       }
00135       else map[ placeholder ] = placeholder;
00136     }
00137   }
00138   return true;
00139 }
00140 
00141 bool TemplateInterface::insertTemplateText ( uint line, uint column, const QString &templateString, const QMap<QString, QString> &initialValues, QWidget *parentWindow )
00142 {
00143   QMap<QString, QString> enhancedInitValues( initialValues );
00144 
00145   QRegExp rx( "[$%]\\{([^}\\s]+)\\}" );
00146   rx.setMinimal( true );
00147   int pos = 0;
00148   int opos = 0;
00149 
00150   while ( pos >= 0 )
00151   {
00152     pos = rx.search( templateString, pos );
00153 
00154     if ( pos > -1 )
00155     {
00156       if ( ( pos - opos ) > 0 )
00157       {
00158         if ( templateString[ pos - 1 ] == '\\' )
00159         {
00160           pos = opos = pos + 1;
00161           continue;
00162         }
00163       }
00164       QString placeholder = rx.cap( 1 );
00165       if ( ! enhancedInitValues.contains( placeholder ) )
00166         enhancedInitValues[ placeholder ] = "";
00167 
00168       pos += rx.matchedLength();
00169       opos = pos;
00170     }
00171   }
00172 
00173   return expandMacros( enhancedInitValues, parentWindow )
00174          && insertTemplateTextImplementation( line, column, templateString, enhancedInitValues, parentWindow );
00175 }
00176 
00177 
00178 
00179 TemplateInterface *KTextEditor::templateInterface ( KTextEditor::Document *doc )
00180 {
00181   if ( !doc )
00182     return 0;
00183 
00184   return static_cast<TemplateInterface*>( doc->qt_cast( "KTextEditor::TemplateInterface" ) );
00185 }
00186 
KDE Home | KDE Accessibility Home | Description of Access Keys