/* This file is part of the KDE libraries Copyright (C) 2004, 2010 Joseph Wenninger 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 #include #include #include #include #include #include #include #include #include #define DUMMY_VALUE "!KTE:TEMPLATEHANDLER_DUMMY_VALUE!" using namespace KTextEditor; bool TemplateInterface::expandMacros( QMap &map, QWidget *parentWindow) { QDateTime datetime = QDateTime::currentDateTime(); QDate date = datetime.date(); QTime time = datetime.time(); QMap::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 (placeholder == "fullname" || placeholder == "email") { KEMailSettings mailsettings; const QString fullname = mailsettings.getSetting(KEMailSettings::RealName); const QString email = mailsettings.getSetting(KEMailSettings::EmailAddress); if (fullname.isEmpty() || email.isEmpty()) { KMessageBox::sorry(parentWindow,i18n("The template needs information about you but it is not available.\n The information can be set set from system settings.")); return false; } map[ "fullname" ] = fullname; map[ "email" ] = email; } 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" ) { map[ placeholder ] = QHostInfo::localHostName(); } 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 *initialValues) { QMap 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=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=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::iterator it=enhancedInitValues.begin();it!=enhancedInitValues.end();++it) { kDebug()<<"key:"<(this) ) ) return false; *initialValues=enhancedInitValues; return true; } bool TemplateInterface::insertTemplateText ( const Cursor& insertPosition, const QString &templateString, const QMap &initialValues) { QMap 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;