templateinterface.cpp
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 <stdaddressbook.h> 00022 #include <addressee.h> 00023 #include <addresseedialog.h> 00024 #include <qstring.h> 00025 #include <klocale.h> 00026 #include <kglobal.h> 00027 #include <qdatetime.h> 00028 #include <qregexp.h> 00029 #include <kmessagebox.h> 00030 #include <kcalendarsystem.h> 00031 #include <unistd.h> 00032 00033 #include <kdebug.h> 00034 00035 using namespace KTextEditor; 00036 00037 unsigned int TemplateInterface::globalTemplateInterfaceNumber = 0; 00038 00039 TemplateInterface::TemplateInterface() 00040 { 00041 myTemplateInterfaceNumber = globalTemplateInterfaceNumber++; 00042 } 00043 00044 TemplateInterface::~TemplateInterface() 00045 {} 00046 00047 uint TemplateInterface::templateInterfaceNumber () const 00048 { 00049 return myTemplateInterfaceNumber; 00050 } 00051 00052 void TemplateInterface::setTemplateInterfaceDCOPSuffix ( const QCString &suffix ) 00053 {} 00054 00055 #define INITKABC do { \ 00056 if (addrBook==0) { \ 00057 addrBook=KABC::StdAddressBook::self(); \ 00058 userAddress=addrBook->whoAmI(); \ 00059 if (userAddress.isEmpty()) { \ 00060 if ( KMessageBox::questionYesNo(parentWindow, \ 00061 i18n( "This template uses personal data that is stored in the KDE 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?" ), \ 00062 "Personal data requested", \ 00063 KStdGuiItem::yes(), KStdGuiItem::no(), "select personal data entry") == KMessageBox::Yes ) { \ 00064 userAddress = KABC::AddresseeDialog::getAddressee(parentWindow); \ 00065 if ( ! userAddress.isEmpty() ) \ 00066 KABC::StdAddressBook::self()->setWhoAmI( userAddress ); \ 00067 }\ 00068 /*return false;//no, why??*/ \ 00069 } \ 00070 } \ 00071 } while(false) 00072 00073 bool TemplateInterface::expandMacros( QMap<QString, QString> &map, QWidget *parentWindow ) 00074 { 00075 KABC::StdAddressBook *addrBook = 0; 00076 KABC::Addressee userAddress; 00077 QDateTime datetime = QDateTime::currentDateTime(); 00078 QDate date = datetime.date(); 00079 QTime time = datetime.time(); 00080 00081 QMap<QString,QString>::Iterator it; 00082 for ( it = map.begin(); it != map.end(); ++it ) 00083 { 00084 QString placeholder = it.key(); 00085 if ( map[ placeholder ].isEmpty() ) 00086 { 00087 if ( placeholder == "index" ) map[ placeholder ] = "i"; 00088 else if ( placeholder == "loginname" ) 00089 {} 00090 else if ( placeholder == "firstname" ) 00091 { 00092 INITKABC; 00093 if ( !userAddress.isEmpty() ) 00094 map[ placeholder ] = userAddress.givenName(); 00095 } 00096 else if ( placeholder == "lastname" ) 00097 { 00098 INITKABC; 00099 if ( !userAddress.isEmpty() ) 00100 map[ placeholder ] = userAddress.familyName(); 00101 } 00102 else if ( placeholder == "fullname" ) 00103 { 00104 INITKABC; 00105 if ( !userAddress.isEmpty() ) 00106 map[ placeholder ] = userAddress.assembledName(); 00107 } 00108 else if ( placeholder == "email" ) 00109 { 00110 INITKABC; 00111 if ( !userAddress.isEmpty() ) 00112 map[ placeholder ] = userAddress.preferredEmail(); 00113 } 00114 else if ( placeholder == "date" ) 00115 { 00116 map[ placeholder ] = KGlobal::locale() ->formatDate( date, true ); 00117 } 00118 else if ( placeholder == "time" ) 00119 { 00120 map[ placeholder ] = KGlobal::locale() ->formatTime( time, true, false ); 00121 } 00122 else if ( placeholder == "year" ) 00123 { 00124 map[ placeholder ] = KGlobal::locale() ->calendar() ->yearString( date, false ); 00125 } 00126 else if ( placeholder == "month" ) 00127 { 00128 map[ placeholder ] = QString::number( KGlobal::locale() ->calendar() ->month( date ) ); 00129 } 00130 else if ( placeholder == "day" ) 00131 { 00132 map[ placeholder ] = QString::number( KGlobal::locale() ->calendar() ->day( date ) ); 00133 } 00134 else if ( placeholder == "hostname" ) 00135 { 00136 char hostname[ 256 ]; 00137 hostname[ 0 ] = 0; 00138 gethostname( hostname, 255 ); 00139 hostname[ 255 ] = 0; 00140 map[ placeholder ] = QString::fromLocal8Bit( hostname ); 00141 } 00142 else if ( placeholder == "cursor" ) 00143 { 00144 map[ placeholder ] = "|"; 00145 } 00146 else map[ placeholder ] = placeholder; 00147 } 00148 } 00149 return true; 00150 } 00151 00152 bool TemplateInterface::insertTemplateText ( uint line, uint column, const QString &templateString, const QMap<QString, QString> &initialValues, QWidget *parentWindow ) 00153 { 00154 QMap<QString, QString> enhancedInitValues( initialValues ); 00155 00156 QRegExp rx( "[$%]\\{([^}\\s]+)\\}" ); 00157 rx.setMinimal( true ); 00158 int pos = 0; 00159 int opos = 0; 00160 00161 while ( pos >= 0 ) 00162 { 00163 pos = rx.search( templateString, pos ); 00164 00165 if ( pos > -1 ) 00166 { 00167 if ( ( pos - opos ) > 0 ) 00168 { 00169 if ( templateString[ pos - 1 ] == '\\' ) 00170 { 00171 pos = opos = pos + 1; 00172 continue; 00173 } 00174 } 00175 QString placeholder = rx.cap( 1 ); 00176 if ( ! enhancedInitValues.contains( placeholder ) ) 00177 enhancedInitValues[ placeholder ] = ""; 00178 00179 pos += rx.matchedLength(); 00180 opos = pos; 00181 } 00182 } 00183 00184 return expandMacros( enhancedInitValues, parentWindow ) 00185 && insertTemplateTextImplementation( line, column, templateString, enhancedInitValues, parentWindow ); 00186 } 00187 00188 00189 00190 TemplateInterface *KTextEditor::templateInterface ( KTextEditor::Document *doc ) 00191 { 00192 if ( !doc ) 00193 return 0; 00194 00195 return static_cast<TemplateInterface*>( doc->qt_cast( "KTextEditor::TemplateInterface" ) ); 00196 } 00197