TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

templateinterface.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* This file is part of the KDE libraries
  2. Copyright (C) 2004 Joseph Wenninger <jowenn@kde.org>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License version 2 as published by the Free Software Foundation.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Library General Public License for more details.
  10. You should have received a copy of the GNU Library General Public License
  11. along with this library; see the file COPYING.LIB. If not, write to
  12. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  13. Boston, MA 02110-1301, USA.
  14. */
  15. #include "templateinterface.h"
  16. #include "document.h"
  17. #include <stdaddressbook.h>
  18. #include <addressee.h>
  19. #include <addresseedialog.h>
  20. #include <tqstring.h>
  21. #include <tdelocale.h>
  22. #include <tdeglobal.h>
  23. #include <tqdatetime.h>
  24. #include <tqregexp.h>
  25. #include <tdemessagebox.h>
  26. #include <kcalendarsystem.h>
  27. #include <unistd.h>
  28. #include <kdebug.h>
  29. using namespace KTextEditor;
  30. unsigned int TemplateInterface::globalTemplateInterfaceNumber = 0;
  31. TemplateInterface::TemplateInterface()
  32. {
  33. myTemplateInterfaceNumber = globalTemplateInterfaceNumber++;
  34. }
  35. TemplateInterface::~TemplateInterface()
  36. {}
  37. uint TemplateInterface::templateInterfaceNumber () const
  38. {
  39. return myTemplateInterfaceNumber;
  40. }
  41. void TemplateInterface::setTemplateInterfaceDCOPSuffix ( const TQCString &suffix )
  42. {}
  43. #define INITKABC do { \
  44. if (addrBook==0) { \
  45. addrBook=KABC::StdAddressBook::self(); \
  46. userAddress=addrBook->whoAmI(); \
  47. if (userAddress.isEmpty()) { \
  48. if ( KMessageBox::questionYesNo(parentWindow, \
  49. 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?" ), \
  50. "Personal data requested", \
  51. KStdGuiItem::yes(), KStdGuiItem::no(), "select personal data entry") == KMessageBox::Yes ) { \
  52. userAddress = KABC::AddresseeDialog::getAddressee(parentWindow); \
  53. if ( ! userAddress.isEmpty() ) \
  54. KABC::StdAddressBook::self()->setWhoAmI( userAddress ); \
  55. }\
  56. /*return false;//no, why??*/ \
  57. } \
  58. } \
  59. } while(false)
  60. bool TemplateInterface::expandMacros( TQMap<TQString, TQString> &map, TQWidget *parentWindow )
  61. {
  62. KABC::StdAddressBook *addrBook = 0;
  63. KABC::Addressee userAddress;
  64. TQDateTime datetime = TQDateTime::currentDateTime();
  65. TQDate date = TQT_TQDATE_OBJECT(datetime.date());
  66. TQTime time = TQT_TQTIME_OBJECT(datetime.time());
  67. TQMap<TQString,TQString>::Iterator it;
  68. for ( it = map.begin(); it != map.end(); ++it )
  69. {
  70. TQString placeholder = it.key();
  71. if ( map[ placeholder ].isEmpty() )
  72. {
  73. if ( placeholder == "index" ) map[ placeholder ] = "i";
  74. else if ( placeholder == "loginname" )
  75. {}
  76. else if ( placeholder == "firstname" )
  77. {
  78. INITKABC;
  79. if ( !userAddress.isEmpty() )
  80. map[ placeholder ] = userAddress.givenName();
  81. }
  82. else if ( placeholder == "lastname" )
  83. {
  84. INITKABC;
  85. if ( !userAddress.isEmpty() )
  86. map[ placeholder ] = userAddress.familyName();
  87. }
  88. else if ( placeholder == "fullname" )
  89. {
  90. INITKABC;
  91. if ( !userAddress.isEmpty() )
  92. map[ placeholder ] = userAddress.assembledName();
  93. }
  94. else if ( placeholder == "email" )
  95. {
  96. INITKABC;
  97. if ( !userAddress.isEmpty() )
  98. map[ placeholder ] = userAddress.preferredEmail();
  99. }
  100. else if ( placeholder == "date" )
  101. {
  102. map[ placeholder ] = TDEGlobal::locale() ->formatDate( date, true );
  103. }
  104. else if ( placeholder == "time" )
  105. {
  106. map[ placeholder ] = TDEGlobal::locale() ->formatTime( time, true, false );
  107. }
  108. else if ( placeholder == "year" )
  109. {
  110. map[ placeholder ] = TDEGlobal::locale() ->calendar() ->yearString( date, false );
  111. }
  112. else if ( placeholder == "month" )
  113. {
  114. map[ placeholder ] = TQString::number( TDEGlobal::locale() ->calendar() ->month( date ) );
  115. }
  116. else if ( placeholder == "day" )
  117. {
  118. map[ placeholder ] = TQString::number( TDEGlobal::locale() ->calendar() ->day( date ) );
  119. }
  120. else if ( placeholder == "hostname" )
  121. {
  122. char hostname[ 256 ];
  123. hostname[ 0 ] = 0;
  124. gethostname( hostname, 255 );
  125. hostname[ 255 ] = 0;
  126. map[ placeholder ] = TQString::fromLocal8Bit( hostname );
  127. }
  128. else if ( placeholder == "cursor" )
  129. {
  130. map[ placeholder ] = "|";
  131. }
  132. else map[ placeholder ] = placeholder;
  133. }
  134. }
  135. return true;
  136. }
  137. bool TemplateInterface::insertTemplateText ( uint line, uint column, const TQString &templateString, const TQMap<TQString, TQString> &initialValues, TQWidget *parentWindow )
  138. {
  139. TQMap<TQString, TQString> enhancedInitValues( initialValues );
  140. TQRegExp rx( "[$%]\\{([^}\\s]+)\\}" );
  141. rx.setMinimal( true );
  142. int pos = 0;
  143. int opos = 0;
  144. while ( pos >= 0 )
  145. {
  146. pos = rx.search( templateString, pos );
  147. if ( pos > -1 )
  148. {
  149. if ( ( pos - opos ) > 0 )
  150. {
  151. if ( templateString[ pos - 1 ] == '\\' )
  152. {
  153. pos = opos = pos + 1;
  154. continue;
  155. }
  156. }
  157. TQString placeholder = rx.cap( 1 );
  158. if ( ! enhancedInitValues.contains( placeholder ) )
  159. enhancedInitValues[ placeholder ] = "";
  160. pos += rx.matchedLength();
  161. opos = pos;
  162. }
  163. }
  164. return expandMacros( enhancedInitValues, parentWindow )
  165. && insertTemplateTextImplementation( line, column, templateString, enhancedInitValues, parentWindow );
  166. }
  167. TemplateInterface *KTextEditor::templateInterface ( KTextEditor::Document *doc )
  168. {
  169. if ( !doc )
  170. return 0;
  171. return dynamic_cast<KTextEditor::TemplateInterface*>( doc );
  172. }