22 #include <QtCore/QString>
25 #include <QtCore/QDate>
26 #include <QtCore/QRegExp>
34 #define DUMMY_VALUE "!KTE:TEMPLATEHANDLER_DUMMY_VALUE!"
36 using namespace KTextEditor;
40 QDateTime datetime = QDateTime::currentDateTime();
41 QDate date = datetime.date();
42 QTime time = datetime.time();
44 kabcbridgecalltype kabcbridgecall=0;
47 kabcitems<<
"firstname"<<
"lastname"<<
"fullname"<<
"email";
50 for ( it = map.begin(); it != map.end(); ++it )
52 QString placeholder = it.key();
53 if ( map[ placeholder ].isEmpty() )
55 if ( placeholder ==
"index" ) map[ placeholder ] =
"i";
56 else if ( placeholder ==
"loginname" )
58 else if (kabcitems.contains(placeholder))
60 if (kabcbridgecall==0)
62 KLibrary lib(QLatin1String(
"ktexteditorkabcbridge"));
63 kabcbridgecall=(kabcbridgecalltype)lib.
resolveFunction(
"ktexteditorkabcbridge");
64 if (kabcbridgecall == 0)
66 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."));
71 map[ placeholder ] = kabcbridgecall(placeholder,parentWindow,&ok);
77 else if ( placeholder ==
"date" )
81 else if ( placeholder ==
"time" )
83 map[ placeholder ] = KGlobal::locale() ->formatTime( time,
true,
false );
85 else if ( placeholder ==
"year" )
89 else if ( placeholder ==
"month" )
91 map[ placeholder ] = QString::number( KGlobal::locale() ->calendar() ->month( date ) );
93 else if ( placeholder ==
"day" )
95 map[ placeholder ] = QString::number( KGlobal::locale() ->calendar() ->day( date ) );
97 else if ( placeholder ==
"hostname" )
101 gethostname( hostname, 255 );
103 map[ placeholder ] = QString::fromLocal8Bit( hostname );
105 else if ( placeholder ==
"cursor" )
107 map[ placeholder ] =
'|';
109 else if (placeholder==
"selection" ) {
112 else map[ placeholder ] = placeholder;
122 QRegExp rx(
"[$%]\\{([^}\\r\\n]+)\\}" );
123 rx.setMinimal(
true );
129 bool initValue_specified=
false;
130 pos = rx.indexIn( templateString, pos );
135 while ( pos - offset > 0 && templateString[ pos - offset - 1 ] ==
'\\' ) {
138 if ( offset % 2 == 1 ) {
143 QString placeholder = rx.cap( 1 );
145 int pos_colon=placeholder.indexOf(
":");
146 int pos_slash=placeholder.indexOf(
"/");
147 int pos_backtick=placeholder.indexOf(
"`");
148 bool check_slash=
false;
149 bool check_colon=
false;
150 bool check_backtick=
false;
151 if ((pos_colon==-1) && ( pos_slash==-1)) {
153 }
else if ( (pos_colon==-1) && (pos_slash!=-1)) {
155 }
else if ( (pos_colon!=-1) && (pos_slash==-1)) {
158 if (pos_colon<pos_slash)
164 if ( (!check_slash) && (!check_colon) && (pos_backtick>=0) )
169 const int end=placeholder.length();
171 int backslashcount=0;
172 for (
int i=0;i<end;i++) {
173 if (placeholder[i]==
'/') {
174 if ((backslashcount%2)==0) slashcount++;
175 if (slashcount==3)
break;
177 }
else if (placeholder[i]==
'\\')
183 const int tmpStrLength=templateString.length();
184 for (
int i=pos+rx.matchedLength();(slashcount<3) && (i<tmpStrLength);i++,pos++) {
185 if (templateString[i]==
'/') {
186 if ((backslashcount%2)==0) slashcount++;
188 }
else if (placeholder[i]==
'\\')
195 placeholder=placeholder.left(placeholder.indexOf(
"/"));
196 }
else if (check_colon) {
197 initValue=placeholder.mid(pos_colon+1);
198 initValue_specified=
true;
199 int backslashcount=0;
200 for (
int i=initValue.length()-1;(i>=0) && (initValue[i]==
'\\'); i--) {
203 initValue=initValue.left(initValue.length()-((backslashcount+1)/2));
204 if ((backslashcount % 2) ==1) {
206 const int tmpStrLength=templateString.length();
208 for (
int i=pos+rx.matchedLength();(i<tmpStrLength);i++,pos++) {
209 if (templateString[i]==
'}') {
210 initValue=initValue.left(initValue.length()-((backslashcount+1)/2));
211 if ((backslashcount%2)==0)
break;
213 }
else if (placeholder[i]==
'\\')
217 initValue+=placeholder[i];
220 placeholder=placeholder.left(placeholder.indexOf(
":"));
221 }
else if (check_backtick) {
222 placeholder=placeholder.left(pos_backtick);
225 if (placeholder.contains(
"@")) placeholder=placeholder.left(placeholder.indexOf(
"@"));
226 if ( (! enhancedInitValues.contains( placeholder )) || (enhancedInitValues[placeholder]==
DUMMY_VALUE) ) {
227 if (initValue_specified) {
228 enhancedInitValues[placeholder]=initValue;
233 pos += rx.matchedLength();
237 kDebug()<<
"-----------------------------------";
239 kDebug()<<
"key:"<<it.key()<<
" init value:"<<it.value();
242 kDebug()<<
"-----------------------------------";
243 if (!
expandMacros( enhancedInitValues, dynamic_cast<QWidget*>(
this) ) )
return false;
244 *initialValues=enhancedInitValues;