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

KTextEditor

  • sources
  • kde-4.12
  • kdelibs
  • interfaces
  • ktexteditor
templateinterface.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2004, 2010 Joseph Wenninger <jowenn@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "templateinterface.h"
20 #include "document.h"
21 #include "view.h"
22 #include <QtCore/QString>
23 #include <klocale.h>
24 #include <kglobal.h>
25 #include <QtCore/QDate>
26 #include <QtCore/QRegExp>
27 #include <kmessagebox.h>
28 #include <kcalendarsystem.h>
29 #include <unistd.h>
30 #include <klibrary.h>
31 
32 #include <kdebug.h>
33 
34 #define DUMMY_VALUE "!KTE:TEMPLATEHANDLER_DUMMY_VALUE!"
35 
36 using namespace KTextEditor;
37 
38 bool TemplateInterface::expandMacros( QMap<QString, QString> &map, QWidget *parentWindow)
39 {
40  QDateTime datetime = QDateTime::currentDateTime();
41  QDate date = datetime.date();
42  QTime time = datetime.time();
43  typedef QString (*kabcbridgecalltype)(const QString&,QWidget *,bool *ok);
44  kabcbridgecalltype kabcbridgecall=0;
45 
46  QStringList kabcitems;
47  kabcitems<<"firstname"<<"lastname"<<"fullname"<<"email";
48 
49  QMap<QString,QString>::Iterator it;
50  for ( it = map.begin(); it != map.end(); ++it )
51  {
52  QString placeholder = it.key();
53  if ( map[ placeholder ].isEmpty() )
54  {
55  if ( placeholder == "index" ) map[ placeholder ] = "i";
56  else if ( placeholder == "loginname" )
57  {}
58  else if (kabcitems.contains(placeholder))
59  {
60  if (kabcbridgecall==0)
61  {
62  KLibrary lib(QLatin1String("ktexteditorkabcbridge"));
63  kabcbridgecall=(kabcbridgecalltype)lib.resolveFunction("ktexteditorkabcbridge");
64  if (kabcbridgecall == 0)
65  {
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."));
67  return false;
68  }
69  }
70  bool ok;
71  map[ placeholder ] = kabcbridgecall(placeholder,parentWindow,&ok);
72  if (!ok)
73  {
74  return false;
75  }
76  }
77  else if ( placeholder == "date" )
78  {
79  map[ placeholder ] = KGlobal::locale() ->formatDate( date, KLocale::ShortDate );
80  }
81  else if ( placeholder == "time" )
82  {
83  map[ placeholder ] = KGlobal::locale() ->formatTime( time, true, false );
84  }
85  else if ( placeholder == "year" )
86  {
87  map[ placeholder ] = KGlobal::locale() ->calendar() ->formatDate(date, KLocale::Year, KLocale::LongNumber);
88  }
89  else if ( placeholder == "month" )
90  {
91  map[ placeholder ] = QString::number( KGlobal::locale() ->calendar() ->month( date ) );
92  }
93  else if ( placeholder == "day" )
94  {
95  map[ placeholder ] = QString::number( KGlobal::locale() ->calendar() ->day( date ) );
96  }
97  else if ( placeholder == "hostname" )
98  {
99  char hostname[ 256 ];
100  hostname[ 0 ] = 0;
101  gethostname( hostname, 255 );
102  hostname[ 255 ] = 0;
103  map[ placeholder ] = QString::fromLocal8Bit( hostname );
104  }
105  else if ( placeholder == "cursor" )
106  {
107  map[ placeholder ] = '|';
108  }
109  else if (placeholder== "selection" ) {
110  //DO NOTHING, THE IMPLEMENTATION WILL HANDLE THIS
111  }
112  else map[ placeholder ] = placeholder;
113  }
114  }
115  return true;
116 }
117 
118 bool TemplateInterface::KTE_INTERNAL_setupIntialValues(const QString& templateString,QMap<QString,QString> *initialValues)
119 {
120  QMap<QString, QString> enhancedInitValues( *initialValues );
121 
122  QRegExp rx( "[$%]\\{([^}\\r\\n]+)\\}" );
123  rx.setMinimal( true );
124  int pos = 0;
125  int offset;
126  QString initValue;
127  while ( pos >= 0 )
128  {
129  bool initValue_specified=false;
130  pos = rx.indexIn( templateString, pos );
131 
132  if ( pos > -1 )
133  {
134  offset = 0;
135  while ( pos - offset > 0 && templateString[ pos - offset - 1 ] == '\\' ) {
136  ++offset;
137  }
138  if ( offset % 2 == 1 ) {
139  // match is escaped
140  ++pos;
141  continue;
142  }
143  QString placeholder = rx.cap( 1 );
144 
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)) {
152  //do nothing
153  } else if ( (pos_colon==-1) && (pos_slash!=-1)) {
154  check_slash=true;
155  } else if ( (pos_colon!=-1) && (pos_slash==-1)) {
156  check_colon=true;
157  } else {
158  if (pos_colon<pos_slash)
159  check_colon=true;
160  else
161  check_slash=true;
162  }
163 
164  if ( (!check_slash) && (!check_colon) && (pos_backtick>=0) )
165  check_backtick=true;
166 
167  if (check_slash) {
168  //in most cases it should not matter, but better safe then sorry.
169  const int end=placeholder.length();
170  int slashcount=0;
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;
176  backslashcount=0;
177  } else if (placeholder[i]=='\\')
178  backslashcount++;
179  else
180  backslashcount=0; //any character terminates a backslash sequence
181  }
182  if (slashcount!=3) {
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++;
187  backslashcount=0;
188  } else if (placeholder[i]=='\\')
189  backslashcount++;
190  else
191  backslashcount=0; //any character terminates a backslash sequence
192  }
193  }
194  //this is needed
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--) {
201  backslashcount++;
202  }
203  initValue=initValue.left(initValue.length()-((backslashcount+1)/2));
204  if ((backslashcount % 2) ==1) {
205  initValue+="}";
206  const int tmpStrLength=templateString.length();
207  backslashcount=0;
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;
212  backslashcount=0;
213  } else if (placeholder[i]=='\\')
214  backslashcount++;
215  else
216  backslashcount=0; //any character terminates a backslash sequence
217  initValue+=placeholder[i];
218  }
219  }
220  placeholder=placeholder.left(placeholder.indexOf(":"));
221  } else if (check_backtick) {
222  placeholder=placeholder.left(pos_backtick);
223  }
224 
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;
229  } else {
230  enhancedInitValues[ placeholder ] = DUMMY_VALUE;
231  }
232  }
233  pos += rx.matchedLength();
234  }
235  }
236 
237  kDebug()<<"-----------------------------------";
238  for (QMap<QString,QString>::iterator it=enhancedInitValues.begin();it!=enhancedInitValues.end();++it) {
239  kDebug()<<"key:"<<it.key()<<" init value:"<<it.value();
240  if (it.value()==DUMMY_VALUE) it.value()="";
241  }
242  kDebug()<<"-----------------------------------";
243  if (!expandMacros( enhancedInitValues, dynamic_cast<QWidget*>(this) ) ) return false;
244  *initialValues=enhancedInitValues;
245  return true;
246 }
247 
248 bool TemplateInterface::insertTemplateText ( const Cursor& insertPosition, const QString &templateString, const QMap<QString, QString> &initialValues) {
249  QMap<QString,QString> enhancedInitValues(initialValues);
250  if (!KTE_INTERNAL_setupIntialValues(templateString,&enhancedInitValues)) return false;
251  return insertTemplateTextImplementation( insertPosition, templateString, enhancedInitValues);
252 }
253 
254 // kate: space-indent on; indent-width 2; replace-tabs on;
i18n
QString i18n(const char *text)
DUMMY_VALUE
#define DUMMY_VALUE
Definition: templateinterface.cpp:34
KTextEditor::TemplateInterface::expandMacros
static bool expandMacros(QMap< QString, QString > &initialValues, QWidget *parentWindow)
Parses templateString for macros in the form [$%]{NAME} and finds the value corresponding to NAME if ...
Definition: templateinterface.cpp:38
kdebug.h
QWidget
KLocale::ShortDate
kcalendarsystem.h
KLocale::Year
KLocale::formatTime
QString formatTime(const QTime &pTime, bool includeSecs=false, bool isDuration=false) const
QString
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KTextEditor::Cursor
An object which represents a position in a Document.
Definition: cursor.h:61
klocale.h
templateinterface.h
KTextEditor::TemplateInterface::insertTemplateTextImplementation
virtual bool insertTemplateTextImplementation(const Cursor &insertPosition, const QString &templateString, const QMap< QString, QString > &initialValues)=0
You must implement this, it is called by insertTemplateText, after all default values are inserted...
kglobal.h
KLocale::calendar
const KCalendarSystem * calendar() const
KLocale::LongNumber
view.h
KCalendarSystem::formatDate
virtual QString formatDate(const QDate &fromDate, KLocale::DateFormat toFormat=KLocale::LongDate) const
QStringList
KMessageBox::sorry
static void sorry(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
KLibrary
document.h
ok
KGuiItem ok()
KGlobal::locale
KLocale * locale()
QDateTime
KLibrary::resolveFunction
void_function_ptr resolveFunction(const char *name)
KTextEditor::TemplateInterface::insertTemplateText
bool insertTemplateText(const Cursor &insertPosition, const QString &templateString, const QMap< QString, QString > &initialValues)
Inserts an interactive ediable template text at line "line", column "col".
Definition: templateinterface.cpp:248
KLocale::formatDate
QString formatDate(const QDate &date, DateFormat format=LongDate) const
kmessagebox.h
klibrary.h
end
const KShortcut & end()
QMap< QString, QString >
KTextEditor::TemplateInterface::KTE_INTERNAL_setupIntialValues
bool KTE_INTERNAL_setupIntialValues(const QString &templateString, QMap< QString, QString > *initialValues)
DO NOT USE !!!! THIS IS USED INTERNALLY by the interface only !!!!!! Behaviour might change !!!!!!! ...
Definition: templateinterface.cpp:118
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:52:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KTextEditor

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal