2014-11-13 01:04:59 +02:00
/* This file is part of the KDE libraries
Copyright ( C ) 2004 , 2010 Joseph Wenninger < jowenn @ kde . org >
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation .
This library is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
Library General Public License for more details .
You should have received a copy of the GNU Library General Public License
along with this library ; see the file COPYING . LIB . If not , write to
the Free Software Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
Boston , MA 02110 - 1301 , USA .
*/
# include "templateinterface.h"
# include "document.h"
# include "view.h"
2019-06-21 19:56:58 +00:00
2014-11-13 01:04:59 +02:00
# include <klocale.h>
# include <kglobal.h>
# include <kmessagebox.h>
2022-05-31 10:11:53 +03:00
# include <kemailsettings.h>
2023-05-23 00:46:25 +03:00
# include <kuser.h>
2014-11-13 01:04:59 +02:00
# include <kdebug.h>
2019-06-21 19:56:58 +00:00
# include <QString>
# include <QDateTime>
# include <QRegExp>
# include <QHostInfo>
2014-11-13 01:04:59 +02:00
# define DUMMY_VALUE "!KTE:TEMPLATEHANDLER_DUMMY_VALUE!"
using namespace KTextEditor ;
2023-05-23 00:46:25 +03:00
bool TemplateInterface : : insertTemplateText ( const Cursor & insertPosition , const QString & templateString , const QMap < QString , QString > & initialValues )
2014-11-13 01:04:59 +02:00
{
2023-05-23 03:53:17 +03:00
// NOTE: THE IMPLEMENTATION WILL HANDLE %{cursor}
2023-05-23 00:46:25 +03:00
2014-11-13 01:04:59 +02:00
QDateTime datetime = QDateTime : : currentDateTime ( ) ;
QDate date = datetime . date ( ) ;
QTime time = datetime . time ( ) ;
2023-05-23 00:46:25 +03:00
QWidget * parentWindow = dynamic_cast < QWidget * > ( this ) ;
QMap < QString , QString > enhancedInitValues ( initialValues ) ;
if ( templateString . contains ( QLatin1String ( " %{loginname} " ) ) ) {
enhancedInitValues [ " loginname " ] = KUser ( ) . loginName ( ) ;
}
2014-11-13 01:04:59 +02:00
2023-05-23 00:46:25 +03:00
if ( templateString . contains ( QLatin1String ( " %{fullname} " ) ) ) {
KEMailSettings mailsettings ;
const QString fullname = mailsettings . getSetting ( KEMailSettings : : RealName ) ;
if ( fullname . isEmpty ( ) ) {
KMessageBox : : sorry ( parentWindow , i18n ( " The template needs information about you but it is not available. \n The information can be set set from system settings. " ) ) ;
return false ;
2014-11-13 01:04:59 +02:00
}
2023-05-23 00:46:25 +03:00
enhancedInitValues [ " fullname " ] = fullname ;
2014-11-13 01:04:59 +02:00
}
2023-05-23 00:46:25 +03:00
if ( templateString . contains ( QLatin1String ( " %{email} " ) ) ) {
KEMailSettings mailsettings ;
const QString email = mailsettings . getSetting ( KEMailSettings : : EmailAddress ) ;
if ( email . isEmpty ( ) ) {
KMessageBox : : sorry ( parentWindow , i18n ( " The template needs information about you but it is not available. \n The information can be set set from system settings. " ) ) ;
return false ;
2014-11-13 01:04:59 +02:00
}
2023-05-23 00:46:25 +03:00
enhancedInitValues [ " email " ] = email ;
}
if ( templateString . contains ( QLatin1String ( " %{date} " ) ) ) {
2023-07-24 23:25:31 +03:00
enhancedInitValues [ " date " ] = KGlobal : : locale ( ) - > formatDate ( date , QLocale : : ShortFormat ) ;
2023-05-23 00:46:25 +03:00
}
if ( templateString . contains ( QLatin1String ( " %{time} " ) ) ) {
2023-07-24 23:25:31 +03:00
enhancedInitValues [ " time " ] = KGlobal : : locale ( ) - > formatTime ( time , QLocale : : ShortFormat ) ;
2023-05-23 00:46:25 +03:00
}
2023-07-24 23:25:31 +03:00
const QLocale locale = KGlobal : : locale ( ) - > toLocale ( ) ;
2023-05-23 00:46:25 +03:00
if ( templateString . contains ( QLatin1String ( " %{year} " ) ) ) {
2023-07-24 23:25:31 +03:00
enhancedInitValues [ " year " ] = locale . toString ( date , " yyyy " ) ;
2023-05-23 00:46:25 +03:00
}
if ( templateString . contains ( QLatin1String ( " %{month} " ) ) ) {
2023-07-24 23:25:31 +03:00
enhancedInitValues [ " month " ] = locale . toString ( date , " MM " ) ;
2023-05-23 00:46:25 +03:00
}
if ( templateString . contains ( QLatin1String ( " %{day} " ) ) ) {
2023-07-24 23:25:31 +03:00
enhancedInitValues [ " day " ] = locale . toString ( date , " dd " ) ;
2014-11-13 01:04:59 +02:00
}
2023-05-23 00:46:25 +03:00
if ( templateString . contains ( QLatin1String ( " %{hostname} " ) ) ) {
enhancedInitValues [ " hostname " ] = QHostInfo : : localHostName ( ) ;
2014-11-13 01:04:59 +02:00
}
2023-05-23 00:46:25 +03:00
return insertTemplateTextImplementation ( insertPosition , templateString , enhancedInitValues ) ;
2014-11-13 01:04:59 +02:00
}
// kate: space-indent on; indent-width 2; replace-tabs on;