• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KTextEditor

templateinterface.cpp

Go to the documentation of this file.
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 "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=KLibLoader::self()->library(QLatin1String("ktexteditorkabcbridge"));
00061           if (lib)
00062               kabcbridgecall=(kabcbridgecalltype)lib->resolveFunction("ktexteditorkabcbridge");
00063           if (kabcbridgecall == 0)
00064           {
00065             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."));
00066             return false;
00067           }
00068         }
00069         bool ok;
00070         map[ placeholder ] = kabcbridgecall(placeholder,parentWindow,&ok);
00071         if (!ok)
00072         {
00073           return false;
00074         }
00075       }
00076       else if ( placeholder == "date" )
00077       {
00078         map[ placeholder ] = KGlobal::locale() ->formatDate( date, KLocale::ShortDate );
00079       }
00080       else if ( placeholder == "time" )
00081       {
00082         map[ placeholder ] = KGlobal::locale() ->formatTime( time, true, false );
00083       }
00084       else if ( placeholder == "year" )
00085       {
00086         map[ placeholder ] = KGlobal::locale() ->calendar() ->yearString( date, KCalendarSystem::LongFormat );
00087       }
00088       else if ( placeholder == "month" )
00089       {
00090         map[ placeholder ] = QString::number( KGlobal::locale() ->calendar() ->month( date ) );
00091       }
00092       else if ( placeholder == "day" )
00093       {
00094         map[ placeholder ] = QString::number( KGlobal::locale() ->calendar() ->day( date ) );
00095       }
00096       else if ( placeholder == "hostname" )
00097       {
00098         char hostname[ 256 ];
00099         hostname[ 0 ] = 0;
00100         gethostname( hostname, 255 );
00101         hostname[ 255 ] = 0;
00102         map[ placeholder ] = QString::fromLocal8Bit( hostname );
00103       }
00104       else if ( placeholder == "cursor" )
00105       {
00106         map[ placeholder ] = "|";
00107       }
00108       else map[ placeholder ] = placeholder;
00109     }
00110   }
00111   return true;
00112 }
00113 
00114 bool TemplateInterface::insertTemplateText ( const Cursor& insertPosition, const QString &templateString, const QMap<QString, QString> &initialValues)
00115 {
00116   QMap<QString, QString> enhancedInitValues( initialValues );
00117 
00118   QRegExp rx( "[$%]\\{([^}\\s]+)\\}" );
00119   rx.setMinimal( true );
00120   int pos = 0;
00121   int opos = 0;
00122 
00123   while ( pos >= 0 )
00124   {
00125     pos = rx.indexIn( templateString, pos );
00126 
00127     if ( pos > -1 )
00128     {
00129       if ( ( pos - opos ) > 0 )
00130       {
00131         if ( templateString[ pos - 1 ] == '\\' )
00132         {
00133           pos = opos = pos + 1;
00134           continue;
00135         }
00136       }
00137       QString placeholder = rx.cap( 1 );
00138       if ( ! enhancedInitValues.contains( placeholder ) )
00139         enhancedInitValues[ placeholder ] = "";
00140 
00141       pos += rx.matchedLength();
00142       opos = pos;
00143     }
00144   }
00145 
00146   return expandMacros( enhancedInitValues, dynamic_cast<QWidget*>(this) )
00147          && insertTemplateTextImplementation( insertPosition, templateString, enhancedInitValues);
00148 }
00149 
00150 // kate: space-indent on; indent-width 2; replace-tabs on;

KTextEditor

Skip menu "KTextEditor"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal