KTextEditor
templateinterface.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "templateinterface.h"
00020 #include "document.h"
00021 #include "view.h"
00022 #include <QtCore/QString>
00023 #include <klocale.h>
00024 #include <kglobal.h>
00025 #include <QtCore/QDate>
00026 #include <QtCore/QRegExp>
00027 #include <kmessagebox.h>
00028 #include <kcalendarsystem.h>
00029 #include <unistd.h>
00030 #include <klibloader.h>
00031
00032 #include <kdebug.h>
00033
00034 using namespace KTextEditor;
00035
00036 bool TemplateInterface::expandMacros( QMap<QString, QString> &map, QWidget *parentWindow)
00037 {
00038 QDateTime datetime = QDateTime::currentDateTime();
00039 QDate date = datetime.date();
00040 QTime time = datetime.time();
00041 typedef QString (*kabcbridgecalltype)(const QString&,QWidget *,bool *ok);
00042 kabcbridgecalltype kabcbridgecall=0;
00043
00044 QStringList kabcitems;
00045 kabcitems<<"firstname"<<"lastname"<<"fullname"<<"email";
00046
00047 QMap<QString,QString>::Iterator it;
00048 for ( it = map.begin(); it != map.end(); ++it )
00049 {
00050 QString placeholder = it.key();
00051 if ( map[ placeholder ].isEmpty() )
00052 {
00053 if ( placeholder == "index" ) map[ placeholder ] = "i";
00054 else if ( placeholder == "loginname" )
00055 {}
00056 else if (kabcitems.contains(placeholder))
00057 {
00058 if (kabcbridgecall==0)
00059 {
00060 KLibrary lib(QLatin1String("ktexteditorkabcbridge"));
00061 kabcbridgecall=(kabcbridgecalltype)lib.resolveFunction("ktexteditorkabcbridge");
00062 if (kabcbridgecall == 0)
00063 {
00064 KMessageBox::sorry(parentWindow,i18n("The template needs information about you, which is stored in your address book.\nHowever, the required plugin could not be loaded.\n\nPlease install the KDEPIM/Kontact package for your system."));
00065 return false;
00066 }
00067 }
00068 bool ok;
00069 map[ placeholder ] = kabcbridgecall(placeholder,parentWindow,&ok);
00070 if (!ok)
00071 {
00072 return false;
00073 }
00074 }
00075 else if ( placeholder == "date" )
00076 {
00077 map[ placeholder ] = KGlobal::locale() ->formatDate( date, KLocale::ShortDate );
00078 }
00079 else if ( placeholder == "time" )
00080 {
00081 map[ placeholder ] = KGlobal::locale() ->formatTime( time, true, false );
00082 }
00083 else if ( placeholder == "year" )
00084 {
00085 map[ placeholder ] = KGlobal::locale() ->calendar() ->yearString( date, KCalendarSystem::LongFormat );
00086 }
00087 else if ( placeholder == "month" )
00088 {
00089 map[ placeholder ] = QString::number( KGlobal::locale() ->calendar() ->month( date ) );
00090 }
00091 else if ( placeholder == "day" )
00092 {
00093 map[ placeholder ] = QString::number( KGlobal::locale() ->calendar() ->day( date ) );
00094 }
00095 else if ( placeholder == "hostname" )
00096 {
00097 char hostname[ 256 ];
00098 hostname[ 0 ] = 0;
00099 gethostname( hostname, 255 );
00100 hostname[ 255 ] = 0;
00101 map[ placeholder ] = QString::fromLocal8Bit( hostname );
00102 }
00103 else if ( placeholder == "cursor" )
00104 {
00105 map[ placeholder ] = "|";
00106 }
00107 else map[ placeholder ] = placeholder;
00108 }
00109 }
00110 return true;
00111 }
00112
00113 bool TemplateInterface::insertTemplateText ( const Cursor& insertPosition, const QString &templateString, const QMap<QString, QString> &initialValues)
00114 {
00115 QMap<QString, QString> enhancedInitValues( initialValues );
00116
00117 QRegExp rx( "[$%]\\{([^}\\s]+)\\}" );
00118 rx.setMinimal( true );
00119 int pos = 0;
00120 int opos = 0;
00121
00122 while ( pos >= 0 )
00123 {
00124 pos = rx.indexIn( templateString, pos );
00125
00126 if ( pos > -1 )
00127 {
00128 if ( ( pos - opos ) > 0 )
00129 {
00130 if ( templateString[ pos - 1 ] == '\\' )
00131 {
00132 pos = opos = pos + 1;
00133 continue;
00134 }
00135 }
00136 QString placeholder = rx.cap( 1 );
00137 if ( ! enhancedInitValues.contains( placeholder ) )
00138 enhancedInitValues[ placeholder ] = "";
00139
00140 pos += rx.matchedLength();
00141 opos = pos;
00142 }
00143 }
00144
00145 return expandMacros( enhancedInitValues, dynamic_cast<QWidget*>(this) )
00146 && insertTemplateTextImplementation( insertPosition, templateString, enhancedInitValues);
00147 }
00148
00149