19 #include "templateinterface.h"
21 #include <stdaddressbook.h>
22 #include <addressee.h>
23 #include <addresseedialog.h>
25 #include <tdelocale.h>
26 #include <tdeglobal.h>
27 #include <tqdatetime.h>
29 #include <tdemessagebox.h>
30 #include <kcalendarsystem.h>
37 unsigned int TemplateInterface::globalTemplateInterfaceNumber = 0;
39 TemplateInterface::TemplateInterface()
41 myTemplateInterfaceNumber = globalTemplateInterfaceNumber++;
44 TemplateInterface::~TemplateInterface()
47 uint TemplateInterface::templateInterfaceNumber ()
const
49 return myTemplateInterfaceNumber;
52 void TemplateInterface::setTemplateInterfaceDCOPSuffix (
const TQCString &suffix )
55 #define INITKABC do { \
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 ); \
75 TDEABC::StdAddressBook *addrBook = 0;
76 TDEABC::Addressee userAddress;
77 TQDateTime datetime = TQDateTime::currentDateTime();
78 TQDate date = datetime.date();
79 TQTime time = datetime.time();
81 TQMap<TQString,TQString>::Iterator it;
82 for ( it = map.begin(); it != map.end(); ++it )
84 TQString placeholder = it.key();
85 if ( map[ placeholder ].isEmpty() )
87 if ( placeholder ==
"index" ) map[ placeholder ] =
"i";
88 else if ( placeholder ==
"loginname" )
90 else if ( placeholder ==
"firstname" )
93 if ( !userAddress.isEmpty() )
94 map[ placeholder ] = userAddress.givenName();
96 else if ( placeholder ==
"lastname" )
99 if ( !userAddress.isEmpty() )
100 map[ placeholder ] = userAddress.familyName();
102 else if ( placeholder ==
"fullname" )
105 if ( !userAddress.isEmpty() )
106 map[ placeholder ] = userAddress.assembledName();
108 else if ( placeholder ==
"email" )
111 if ( !userAddress.isEmpty() )
112 map[ placeholder ] = userAddress.preferredEmail();
114 else if ( placeholder ==
"date" )
116 map[ placeholder ] = TDEGlobal::locale() ->formatDate( date,
true );
118 else if ( placeholder ==
"time" )
120 map[ placeholder ] = TDEGlobal::locale() ->formatTime( time,
true,
false );
122 else if ( placeholder ==
"year" )
124 map[ placeholder ] = TDEGlobal::locale() ->calendar() ->yearString( date,
false );
126 else if ( placeholder ==
"month" )
128 map[ placeholder ] = TQString::number( TDEGlobal::locale() ->calendar() ->month( date ) );
130 else if ( placeholder ==
"day" )
132 map[ placeholder ] = TQString::number( TDEGlobal::locale() ->calendar() ->day( date ) );
134 else if ( placeholder ==
"hostname" )
136 char hostname[ 256 ];
138 gethostname( hostname, 255 );
140 map[ placeholder ] = TQString::fromLocal8Bit( hostname );
142 else if ( placeholder ==
"cursor" )
144 map[ placeholder ] =
"|";
146 else map[ placeholder ] = placeholder;
154 TQMap<TQString, TQString> enhancedInitValues( initialValues );
156 TQRegExp rx(
"[$%]\\{([^}\\s]+)\\}" );
157 rx.setMinimal(
true );
163 pos = rx.search( templateString, pos );
167 if ( ( pos - opos ) > 0 )
169 if ( templateString[ pos - 1 ] ==
'\\' )
171 pos = opos = pos + 1;
175 TQString placeholder = rx.cap( 1 );
176 if ( ! enhancedInitValues.contains( placeholder ) )
177 enhancedInitValues[ placeholder ] =
"";
179 pos += rx.matchedLength();
The main class representing a text document.
This is an interface for inserting template strings with user editable fields into a document.
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.
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 ...
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".
KTextEditor is KDE's standard text editing KPart interface.