1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
/* This file is part of the KDE libraries
Copyright (C) 2004, 2010 Joseph Wenninger <jowenn@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "templateinterface.h"
#include "document.h"
#include "view.h"
#include <QtCore/QString>
#include <klocale.h>
#include <kglobal.h>
#include <QtCore/QDate>
#include <QtCore/QRegExp>
#include <kmessagebox.h>
#include <kcalendarsystem.h>
#include <unistd.h>
#include <klibrary.h>
#include <kdebug.h>
#define DUMMY_VALUE "!KTE:TEMPLATEHANDLER_DUMMY_VALUE!"
using namespace KTextEditor;
bool TemplateInterface::expandMacros( QMap<QString, QString> &map, QWidget *parentWindow)
{
QDateTime datetime = QDateTime::currentDateTime();
QDate date = datetime.date();
QTime time = datetime.time();
typedef QString (*kabcbridgecalltype)(const QString&,QWidget *,bool *ok);
kabcbridgecalltype kabcbridgecall=0;
QStringList kabcitems;
kabcitems<<"firstname"<<"lastname"<<"fullname"<<"email";
QMap<QString,QString>::Iterator it;
for ( it = map.begin(); it != map.end(); ++it )
{
QString placeholder = it.key();
if ( map[ placeholder ].isEmpty() )
{
if ( placeholder == "index" ) map[ placeholder ] = "i";
else if ( placeholder == "loginname" )
{}
else if (kabcitems.contains(placeholder))
{
if (kabcbridgecall==0)
{
KLibrary lib(QLatin1String("ktexteditorkabcbridge"));
kabcbridgecall=(kabcbridgecalltype)lib.resolveFunction("ktexteditorkabcbridge");
if (kabcbridgecall == 0)
{
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."));
return false;
}
}
bool ok;
map[ placeholder ] = kabcbridgecall(placeholder,parentWindow,&ok);
if (!ok)
{
return false;
}
}
else if ( placeholder == "date" )
{
map[ placeholder ] = KGlobal::locale() ->formatDate( date, KLocale::ShortDate );
}
else if ( placeholder == "time" )
{
map[ placeholder ] = KGlobal::locale() ->formatTime( time, true, false );
}
else if ( placeholder == "year" )
{
map[ placeholder ] = KGlobal::locale() ->calendar() ->formatDate(date, KLocale::Year, KLocale::LongNumber);
}
else if ( placeholder == "month" )
{
map[ placeholder ] = QString::number( KGlobal::locale() ->calendar() ->month( date ) );
}
else if ( placeholder == "day" )
{
map[ placeholder ] = QString::number( KGlobal::locale() ->calendar() ->day( date ) );
}
else if ( placeholder == "hostname" )
{
char hostname[ 256 ];
hostname[ 0 ] = 0;
gethostname( hostname, 255 );
hostname[ 255 ] = 0;
map[ placeholder ] = QString::fromLocal8Bit( hostname );
}
else if ( placeholder == "cursor" )
{
map[ placeholder ] = '|';
}
else if (placeholder== "selection" ) {
//DO NOTHING, THE IMPLEMENTATION WILL HANDLE THIS
}
else map[ placeholder ] = placeholder;
}
}
return true;
}
bool TemplateInterface::KTE_INTERNAL_setupIntialValues(const QString& templateString,QMap<QString,QString> *initialValues)
{
QMap<QString, QString> enhancedInitValues( *initialValues );
QRegExp rx( "[$%]\\{([^}\\r\\n]+)\\}" );
rx.setMinimal( true );
int pos = 0;
int offset;
QString initValue;
while ( pos >= 0 )
{
bool initValue_specified=false;
pos = rx.indexIn( templateString, pos );
if ( pos > -1 )
{
offset = 0;
while ( pos - offset > 0 && templateString[ pos - offset - 1 ] == '\\' ) {
++offset;
}
if ( offset % 2 == 1 ) {
// match is escaped
++pos;
continue;
}
QString placeholder = rx.cap( 1 );
int pos_colon=placeholder.indexOf(":");
int pos_slash=placeholder.indexOf("/");
int pos_backtick=placeholder.indexOf("`");
bool check_slash=false;
bool check_colon=false;
bool check_backtick=false;
if ((pos_colon==-1) && ( pos_slash==-1)) {
//do nothing
} else if ( (pos_colon==-1) && (pos_slash!=-1)) {
check_slash=true;
} else if ( (pos_colon!=-1) && (pos_slash==-1)) {
check_colon=true;
} else {
if (pos_colon<pos_slash)
check_colon=true;
else
check_slash=true;
}
if ( (!check_slash) && (!check_colon) && (pos_backtick>=0) )
check_backtick=true;
if (check_slash) {
//in most cases it should not matter, but better safe then sorry.
const int end=placeholder.length();
int slashcount=0;
int backslashcount=0;
for (int i=0;i<end;i++) {
if (placeholder[i]=='/') {
if ((backslashcount%2)==0) slashcount++;
if (slashcount==3) break;
backslashcount=0;
} else if (placeholder[i]=='\\')
backslashcount++;
else
backslashcount=0; //any character terminates a backslash sequence
}
if (slashcount!=3) {
const int tmpStrLength=templateString.length();
for (int i=pos+rx.matchedLength();(slashcount<3) && (i<tmpStrLength);i++,pos++) {
if (templateString[i]=='/') {
if ((backslashcount%2)==0) slashcount++;
backslashcount=0;
} else if (placeholder[i]=='\\')
backslashcount++;
else
backslashcount=0; //any character terminates a backslash sequence
}
}
//this is needed
placeholder=placeholder.left(placeholder.indexOf("/"));
} else if (check_colon) {
initValue=placeholder.mid(pos_colon+1);
initValue_specified=true;
int backslashcount=0;
for (int i=initValue.length()-1;(i>=0) && (initValue[i]=='\\'); i--) {
backslashcount++;
}
initValue=initValue.left(initValue.length()-((backslashcount+1)/2));
if ((backslashcount % 2) ==1) {
initValue+="}";
const int tmpStrLength=templateString.length();
backslashcount=0;
for (int i=pos+rx.matchedLength();(i<tmpStrLength);i++,pos++) {
if (templateString[i]=='}') {
initValue=initValue.left(initValue.length()-((backslashcount+1)/2));
if ((backslashcount%2)==0) break;
backslashcount=0;
} else if (placeholder[i]=='\\')
backslashcount++;
else
backslashcount=0; //any character terminates a backslash sequence
initValue+=placeholder[i];
}
}
placeholder=placeholder.left(placeholder.indexOf(":"));
} else if (check_backtick) {
placeholder=placeholder.left(pos_backtick);
}
if (placeholder.contains("@")) placeholder=placeholder.left(placeholder.indexOf("@"));
if ( (! enhancedInitValues.contains( placeholder )) || (enhancedInitValues[placeholder]==DUMMY_VALUE) ) {
if (initValue_specified) {
enhancedInitValues[placeholder]=initValue;
} else {
enhancedInitValues[ placeholder ] = DUMMY_VALUE;
}
}
pos += rx.matchedLength();
}
}
kDebug()<<"-----------------------------------";
for (QMap<QString,QString>::iterator it=enhancedInitValues.begin();it!=enhancedInitValues.end();++it) {
kDebug()<<"key:"<<it.key()<<" init value:"<<it.value();
if (it.value()==DUMMY_VALUE) it.value()="";
}
kDebug()<<"-----------------------------------";
if (!expandMacros( enhancedInitValues, dynamic_cast<QWidget*>(this) ) ) return false;
*initialValues=enhancedInitValues;
return true;
}
bool TemplateInterface::insertTemplateText ( const Cursor& insertPosition, const QString &templateString, const QMap<QString, QString> &initialValues) {
QMap<QString,QString> enhancedInitValues(initialValues);
if (!KTE_INTERNAL_setupIntialValues(templateString,&enhancedInitValues)) return false;
return insertTemplateTextImplementation( insertPosition, templateString, enhancedInitValues);
}
// kate: space-indent on; indent-width 2; replace-tabs on;
|