• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • interfaces
 

interfaces

  • interfaces
  • tdetexteditor
templateinterface.cpp
1/* This file is part of the KDE libraries
2 Copyright (C) 2004 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 <stdaddressbook.h>
22#include <addressee.h>
23#include <addresseedialog.h>
24#include <tqstring.h>
25#include <tdelocale.h>
26#include <tdeglobal.h>
27#include <tqdatetime.h>
28#include <tqregexp.h>
29#include <tdemessagebox.h>
30#include <kcalendarsystem.h>
31#include <unistd.h>
32
33#include <kdebug.h>
34
35using namespace KTextEditor;
36
37unsigned int TemplateInterface::globalTemplateInterfaceNumber = 0;
38
39TemplateInterface::TemplateInterface()
40{
41 myTemplateInterfaceNumber = globalTemplateInterfaceNumber++;
42}
43
44TemplateInterface::~TemplateInterface()
45{}
46
47uint TemplateInterface::templateInterfaceNumber () const
48{
49 return myTemplateInterfaceNumber;
50}
51
52void TemplateInterface::setTemplateInterfaceDCOPSuffix ( const TQCString &suffix )
53{}
54
55#define INITKABC do { \
56 if (addrBook==0) { \
57 addrBook=TDEABC::StdAddressBook::self(); \
58 userAddress=addrBook->whoAmI(); \
59 if (userAddress.isEmpty()) { \
60 if ( KMessageBox::questionYesNo(parentWindow, \
61 i18n( "This template uses personal data that is stored in the TDE addressbook, but you have not selected a personal entry. You can still use the template without one, but you will have to type personal data. Would you like to select one now?" ), \
62 "Personal data requested", \
63 KStdGuiItem::yes(), KStdGuiItem::no(), "select personal data entry") == KMessageBox::Yes ) { \
64 userAddress = TDEABC::AddresseeDialog::getAddressee(parentWindow); \
65 if ( ! userAddress.isEmpty() ) \
66 TDEABC::StdAddressBook::self()->setWhoAmI( userAddress ); \
67 }\
68 /*return false;//no, why??*/ \
69 } \
70 } \
71} while(false)
72
73bool TemplateInterface::expandMacros( TQMap<TQString, TQString> &map, TQWidget *parentWindow )
74{
75 TDEABC::StdAddressBook *addrBook = 0;
76 TDEABC::Addressee userAddress;
77 TQDateTime datetime = TQDateTime::currentDateTime();
78 TQDate date = datetime.date();
79 TQTime time = datetime.time();
80
81 TQMap<TQString,TQString>::Iterator it;
82 for ( it = map.begin(); it != map.end(); ++it )
83 {
84 TQString placeholder = it.key();
85 if ( map[ placeholder ].isEmpty() )
86 {
87 if ( placeholder == "index" ) map[ placeholder ] = "i";
88 else if ( placeholder == "loginname" )
89 {}
90 else if ( placeholder == "firstname" )
91 {
92 INITKABC;
93 if ( !userAddress.isEmpty() )
94 map[ placeholder ] = userAddress.givenName();
95 }
96 else if ( placeholder == "lastname" )
97 {
98 INITKABC;
99 if ( !userAddress.isEmpty() )
100 map[ placeholder ] = userAddress.familyName();
101 }
102 else if ( placeholder == "fullname" )
103 {
104 INITKABC;
105 if ( !userAddress.isEmpty() )
106 map[ placeholder ] = userAddress.assembledName();
107 }
108 else if ( placeholder == "email" )
109 {
110 INITKABC;
111 if ( !userAddress.isEmpty() )
112 map[ placeholder ] = userAddress.preferredEmail();
113 }
114 else if ( placeholder == "date" )
115 {
116 map[ placeholder ] = TDEGlobal::locale() ->formatDate( date, true );
117 }
118 else if ( placeholder == "time" )
119 {
120 map[ placeholder ] = TDEGlobal::locale() ->formatTime( time, true, false );
121 }
122 else if ( placeholder == "year" )
123 {
124 map[ placeholder ] = TDEGlobal::locale() ->calendar() ->yearString( date, false );
125 }
126 else if ( placeholder == "month" )
127 {
128 map[ placeholder ] = TQString::number( TDEGlobal::locale() ->calendar() ->month( date ) );
129 }
130 else if ( placeholder == "day" )
131 {
132 map[ placeholder ] = TQString::number( TDEGlobal::locale() ->calendar() ->day( date ) );
133 }
134 else if ( placeholder == "hostname" )
135 {
136 char hostname[ 256 ];
137 hostname[ 0 ] = 0;
138 gethostname( hostname, 255 );
139 hostname[ 255 ] = 0;
140 map[ placeholder ] = TQString::fromLocal8Bit( hostname );
141 }
142 else if ( placeholder == "cursor" )
143 {
144 map[ placeholder ] = "|";
145 }
146 else map[ placeholder ] = placeholder;
147 }
148 }
149 return true;
150}
151
152bool TemplateInterface::insertTemplateText ( uint line, uint column, const TQString &templateString, const TQMap<TQString, TQString> &initialValues, TQWidget *parentWindow )
153{
154 TQMap<TQString, TQString> enhancedInitValues( initialValues );
155
156 TQRegExp rx( "[$%]\\{([^}\\s]+)\\}" );
157 rx.setMinimal( true );
158 int pos = 0;
159 int opos = 0;
160
161 while ( pos >= 0 )
162 {
163 pos = rx.search( templateString, pos );
164
165 if ( pos > -1 )
166 {
167 if ( ( pos - opos ) > 0 )
168 {
169 if ( templateString[ pos - 1 ] == '\\' )
170 {
171 pos = opos = pos + 1;
172 continue;
173 }
174 }
175 TQString placeholder = rx.cap( 1 );
176 if ( ! enhancedInitValues.contains( placeholder ) )
177 enhancedInitValues[ placeholder ] = "";
178
179 pos += rx.matchedLength();
180 opos = pos;
181 }
182 }
183
184 return expandMacros( enhancedInitValues, parentWindow )
185 && insertTemplateTextImplementation( line, column, templateString, enhancedInitValues, parentWindow );
186}
187
188
189
190TemplateInterface *KTextEditor::templateInterface ( KTextEditor::Document *doc )
191{
192 if ( !doc )
193 return 0;
194
195 return dynamic_cast<KTextEditor::TemplateInterface*>( doc );
196}
197
KCalendarSystem::yearString
virtual TQString yearString(const TQDate &pDate, bool bShort) const
KTextEditor::Document
The main class representing a text document.
Definition: document.h:32
KTextEditor::TemplateInterface
This is an interface for inserting template strings with user editable fields into a document.
Definition: templateinterface.h:38
KTextEditor::TemplateInterface::insertTemplateTextImplementation
virtual bool insertTemplateTextImplementation(uint line, uint column, const TQString &templateString, const TQMap< TQString, TQString > &initialValues, TQWidget *parentWindow=0)=0
You must implement this, it is called by insertTemplateText, after all default values are inserted.
KTextEditor::TemplateInterface::expandMacros
static bool expandMacros(TQMap< TQString, TQString > &initialValues, TQWidget *parentWindow)
Parses templateString for macros in the form [$%]{NAME} and finds the value corresponding to NAME if ...
Definition: templateinterface.cpp:73
KTextEditor::TemplateInterface::insertTemplateText
bool insertTemplateText(uint line, uint column, const TQString &templateString, const TQMap< TQString, TQString > &initialValues, TQWidget *parentWindow=0)
Inserts an interactive ediable template text at line "line", column "col".
Definition: templateinterface.cpp:152
TDEGlobal::locale
static TDELocale * locale()
TDELocale::calendar
const KCalendarSystem * calendar() const
TDELocale::formatDate
TQString formatDate(const TQDate &pDate, bool shortFormat=false) const
TDELocale::formatTime
TQString formatTime(const TQTime &pTime, bool includeSecs, bool isDuration) const
KTextEditor
KTextEditor is KDE's standard text editing KPart interface.
Definition: blockselectiondcopinterface.h:10
tdelocale.h

interfaces

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

interfaces

Skip menu "interfaces"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for interfaces by doxygen 1.9.4
This website is maintained by Timothy Pearson.