diff options
Diffstat (limited to 'libtdepim')
266 files changed, 39914 insertions, 0 deletions
diff --git a/libtdepim/CMakeLists.txt b/libtdepim/CMakeLists.txt new file mode 100644 index 000000000..19ca1d524 --- /dev/null +++ b/libtdepim/CMakeLists.txt @@ -0,0 +1,108 @@ +################################################# +# +# (C) 2010-2011 Serghei Amelian +# serghei (DOT) amelian (AT) gmail.com +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +project( libtdepim ) + +include( ConfigureChecks.cmake ) + +tde_import( libkmime ) +tde_import( libkcal ) +tde_import( ktnef ) + +add_subdirectory( icons ) +add_subdirectory( pics ) +add_subdirectory( kpartsdesignerplugin ) +add_subdirectory( about ) + +include_directories( + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_BINARY_DIR} + ${CMAKE_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/libtdepim + ${TDE_INCLUDE_DIR} + ${TQT_INCLUDE_DIRS} +) + +link_directories( + ${TQT_LIBRARY_DIRS} +) + + +##### headers ################################### + +install( FILES + tdepimmacros.h kpimprefs.h + DESTINATION ${INCLUDE_INSTALL_DIR}/kde ) + + +##### other data ################################ + +install( FILES pimemoticons.kcfg DESTINATION ${KCFG_INSTALL_DIR} ) + + +##### tdepimwidgets (module) #################### + +add_custom_command( OUTPUT tdepimwidgets.cpp + COMMAND + ${KDE3_MAKEKDEWIDGETS_EXECUTABLE} + -o tdepimwidgets.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/tdepim.widgets + DEPENDS + ${CMAKE_CURRENT_SOURCE_DIR}/tdepim.widgets ) + +set_source_files_properties( tdepimwidgets.cpp PROPERTIES COMPILE_FLAGS "-DQT_PLUGIN" ) + +tde_add_kpart( tdepimwidgets + SOURCES tdepimwidgets.cpp + LINK tdepim-shared + DESTINATION ${PLUGIN_INSTALL_DIR}/plugins/designer +) + + +##### tdepim (shared) ########################### + +set( KDE3_DCOPIDL_EXECUTABLE ${KDE3_DCOPIDLNG_EXECUTABLE} ) + +tde_add_library( tdepim SHARED AUTOMOC + SOURCES + ktimeedit.cpp alarmclient.cpp kprefsdialog.cpp kpimprefs.cpp + categoryselectdialog_base.ui categoryselectdialog.cpp + categoryeditdialog_base.ui categoryeditdialog.cpp kdateedit.cpp + kimportdialog.cpp kvcarddrag.cpp sidebarextension.cpp + infoextension.cpp addressesdialog.cpp addresspicker.ui + addresseeview.cpp maillistdrag.cpp interfaces/MailTransportServiceIface.skel + interfaces/AddressBookServiceIface.skel kconfigpropagator.cpp kconfigwizard.cpp + weaver.cpp weaverextensions.cpp weaverlogger.cpp pluginloaderbase.cpp + addresseelineedit.cpp addresseelineedit.skel kdatepickerpopup.cpp + kfileio.cpp clicklineedit.cpp configmanager.cpp collectingprocess.cpp + ldapclient.cpp overlaywidget.cpp progressmanager.cpp progressdialog.cpp + statusbarprogresswidget.cpp ssllabel.cpp completionordereditor.cpp + resourceabc.cpp diffalgo.cpp addresseediffalgo.cpp calendardiffalgo.cpp + htmldiffalgodisplay.cpp ldapsearchdialog.cpp broadcaststatus.cpp + kresourceprefs.cpp kpixmapregionselectorwidget.cpp + kpixmapregionselectordialog.cpp kabcresourcecached.cpp + kxface.cpp kaccount.cpp kaddrbook.cpp kfoldertree.cpp kregexp3.cpp + kscoring.cpp kscoringeditor.cpp ksubscription.cpp kwidgetlister.cpp + linklocator.cpp qutf7codec.cpp recentaddresses.cpp spellingfilter.cpp + addresseeselector.cpp addresseeemailselection.cpp designerfields.cpp + kcmdesignerfields.cpp embeddedurlpage.cpp kincidencechooser.cpp + groupwarejob.cpp pimemoticons.kcfgc krsqueezedtextlabel.cpp + csshelper.cpp distributionlist.cpp kpimurlrequesterdlg.cpp + sendsmsdialog.cpp kmailcompletion.cpp + VERSION 1.0.0 + LINK kcal-shared kimproxy-shared kparts-shared qui + DESTINATION ${LIB_INSTALL_DIR} +) + + +##### install import cmake modules ############### + +tde_install_export( ) diff --git a/libtdepim/ConfigureChecks.cmake b/libtdepim/ConfigureChecks.cmake new file mode 100644 index 000000000..96e1e621a --- /dev/null +++ b/libtdepim/ConfigureChecks.cmake @@ -0,0 +1,10 @@ +################################################# +# +# (C) 2010-2011 Serghei Amelian +# serghei (DOT) amelian (AT) gmail.com +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# diff --git a/libtdepim/Makefile.am b/libtdepim/Makefile.am new file mode 100644 index 000000000..b5db77f6f --- /dev/null +++ b/libtdepim/Makefile.am @@ -0,0 +1,71 @@ +SUBDIRS = cfgc interfaces tests icons pics kpartsdesignerplugin about + +INCLUDES = -I$(top_srcdir) $(all_includes) + +lib_LTLIBRARIES = libtdepim.la +libtdepim_la_SOURCES = \ + ktimeedit.cpp \ + alarmclient.cpp \ + kprefsdialog.cpp kpimprefs.cpp \ + categoryselectdialog_base.ui categoryselectdialog.cpp \ + categoryeditdialog_base.ui categoryeditdialog.cpp \ + kdateedit.cpp kimportdialog.cpp kvcarddrag.cpp \ + sidebarextension.cpp infoextension.cpp \ + addressesdialog.cpp addresspicker.ui addresseeview.cpp \ + maillistdrag.cpp MailTransportServiceIface.skel \ + AddressBookServiceIface.skel \ + kconfigpropagator.cpp kconfigwizard.cpp \ + weaver.cpp weaverextensions.cpp weaverlogger.cpp \ + pluginloaderbase.cpp addresseelineedit.cpp addresseelineedit.skel \ + kdatepickerpopup.cpp kfileio.cpp \ + clicklineedit.cpp configmanager.cpp \ + collectingprocess.cpp ldapclient.cpp \ + overlaywidget.cpp progressmanager.cpp progressdialog.cpp \ + statusbarprogresswidget.cpp ssllabel.cpp completionordereditor.cpp \ + resourceabc.cpp diffalgo.cpp addresseediffalgo.cpp calendardiffalgo.cpp \ + htmldiffalgodisplay.cpp ldapsearchdialog.cpp broadcaststatus.cpp kresourceprefs.cpp \ + kpixmapregionselectorwidget.cpp kpixmapregionselectordialog.cpp \ + kabcresourcecached.cpp kxface.cpp \ + kaccount.cpp kaddrbook.cpp kfoldertree.cpp kregexp3.cpp \ + kscoring.cpp kscoringeditor.cpp ksubscription.cpp kwidgetlister.cpp \ + linklocator.cpp qutf7codec.cpp \ + recentaddresses.cpp spellingfilter.cpp \ + addresseeselector.cpp addresseeemailselection.cpp \ + designerfields.cpp kcmdesignerfields.cpp \ + embeddedurlpage.cpp kincidencechooser.cpp \ + groupwarejob.cpp pimemoticons.kcfgc \ + krsqueezedtextlabel.cpp csshelper.cpp distributionlist.cpp \ + kpimurlrequesterdlg.cpp sendsmsdialog.cpp kmailcompletion.cpp + +MailTransportServiceIface_DCOPIDLNG = true +MailTransportServiceIface_DIR = $(srcdir)/interfaces + +AddressBookServiceIface_DIR = $(srcdir)/interfaces + +libtdepim_la_LDFLAGS = $(all_libraries) -no-undefined -version-info 1:0:0 $(LIB_QT) -ltdecore +libtdepim_la_LIBADD = $(top_builddir)/libkcal/libkcal.la \ + $(top_builddir)/libemailfunctions/libemailfunctions.la \ + $(LIB_KIO) $(LIB_KABC) $(LIB_KPARTS) $(LIB_KIMPROXY) $(LIB_POLL) -lqui +# $top_builddir)/libtdepim/resources/libkpimresources.la + +include_HEADERS = tdepimmacros.h kpimprefs.h + +AM_CXXFLAGS = -DQT_PLUGIN +kde_widget_LTLIBRARIES = tdepimwidgets.la +tdepimwidgets_la_LDFLAGS = $(KDE_PLUGIN) -module $(all_libraries) $(LIB_QT) -ltdecore +tdepimwidgets_la_LIBADD = $(LIB_KIO) libtdepim.la +tdepimwidgets_la_SOURCES = tdepimwidgets.cpp + +tdepimwidgets.cpp: $(srcdir)/tdepim.widgets + $(MAKEKDEWIDGETS) -o tdepimwidgets.cpp $(srcdir)/tdepim.widgets + +CLEANFILES = tdepimwidgets.cpp + +kde_kcfg_DATA = pimemoticons.kcfg + +METASOURCES = AUTO + +messages: rc.cpp + $(XGETTEXT) ../libkpimidentities/*.cpp ../libemailfunctions/*.cpp *.cpp -o $(podir)/libtdepim.pot + +include $(top_srcdir)/admin/Doxyfile.am diff --git a/libtdepim/README b/libtdepim/README new file mode 100644 index 000000000..dfaefe79f --- /dev/null +++ b/libtdepim/README @@ -0,0 +1,36 @@ +This directory contains the library for the kdenetwork package. + +Contents: + ksieve_* The beginning of a Sieve parser and interpreter + library. + Maintainer: Marc Mutz <mutz@kde.org> + License: GPL v2 + + kscoring/ scoring engine & rule editor + kscoringeditor Maintainer: Mathias Waack <mathias@atoll-net.de> + License: GPL + + qutf7codec A QTextCodec for UTF-7. + Will hopefully be assimilated by the trolls. + Maintainer: Marc Mutz <mutz@kde.org> + License: GPL v2 + + kwidgetlister a small and nice widget which enables one to dynamically + add or delete widgets. Used by the filter and scoring + dialogs, which needs to display for instance a variable + number of conditions. + Maintainer: Marc Mutz <mutz@kde.org> + License: GPL v2 + + kregexp3 A KRegExp replacement based on QRegExp from Qt3.x + Maintainer: Marc Mutz <mutz@kde.org> + License: GPL + + kfoldertree/ Base classes for KNode's and KMail's folder lists, + ksubscription/ subscription dialogs and accounts. + kaccount Maintainer: Carsten Burghardt <burghardt@kde.org> + License: LGPL v2 + + cryptplugwrapper* C++ wrapper around the CryptPlug interface. + Maintainer: Karl-Heinz Zimmer <khz@kde.org> + License: GPL v2 diff --git a/libtdepim/TODO b/libtdepim/TODO new file mode 100644 index 000000000..f6c5214c3 --- /dev/null +++ b/libtdepim/TODO @@ -0,0 +1,10 @@ +KConfigPropagator: + +- Title for all changes. +- Write DTD for extended kcfg file. +- Check validity of rules against the installed kcfg files. +- Turn public members of Change, Rule, Condition into proper accessors/mutators + +KConfigWizard: + +- More pretty changes view. diff --git a/libtdepim/about/CMakeLists.txt b/libtdepim/about/CMakeLists.txt new file mode 100644 index 000000000..97a53d764 --- /dev/null +++ b/libtdepim/about/CMakeLists.txt @@ -0,0 +1,19 @@ +################################################# +# +# (C) 2010-2011 Serghei Amelian +# serghei (DOT) amelian (AT) gmail.com +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +install( FILES + bar-bottom-left.png bar-top-right.png bar-bottom-middle.png bottom-left.png + box-middle-left.png top-left.png bar-bottom-right.png bottom-middle.png + box-middle-right.png top-middle.png bar-middle-left.png bottom-right.png + box-top-left.png bar-middle-right.png box-bottom-left.png box-top-middle.png + bar-top-left.png box-bottom-middle.png box-top-right.png bar-top-middle.png + box-bottom-right.png kde_infopage.css kde_infopage_rtl.css + DESTINATION ${DATA_INSTALL_DIR}/libtdepim/about ) diff --git a/libtdepim/about/Makefile.am b/libtdepim/about/Makefile.am new file mode 100644 index 000000000..6481c5bcb --- /dev/null +++ b/libtdepim/about/Makefile.am @@ -0,0 +1,9 @@ + +tdepim_infopage_datadir = $(kde_datadir)/libtdepim/about +tdepim_infopage_data_DATA = \ + bar-bottom-left.png bar-top-right.png bar-bottom-middle.png bottom-left.png \ + box-middle-left.png top-left.png bar-bottom-right.png bottom-middle.png \ + box-middle-right.png top-middle.png bar-middle-left.png bottom-right.png \ + box-top-left.png bar-middle-right.png box-bottom-left.png box-top-middle.png \ + bar-top-left.png box-bottom-middle.png box-top-right.png bar-top-middle.png \ + box-bottom-right.png kde_infopage.css kde_infopage_rtl.css diff --git a/libtdepim/about/bar-bottom-left.png b/libtdepim/about/bar-bottom-left.png Binary files differnew file mode 100644 index 000000000..30a323592 --- /dev/null +++ b/libtdepim/about/bar-bottom-left.png diff --git a/libtdepim/about/bar-bottom-middle.png b/libtdepim/about/bar-bottom-middle.png Binary files differnew file mode 100644 index 000000000..b2ebbbed5 --- /dev/null +++ b/libtdepim/about/bar-bottom-middle.png diff --git a/libtdepim/about/bar-bottom-right.png b/libtdepim/about/bar-bottom-right.png Binary files differnew file mode 100644 index 000000000..e664e1511 --- /dev/null +++ b/libtdepim/about/bar-bottom-right.png diff --git a/libtdepim/about/bar-middle-left.png b/libtdepim/about/bar-middle-left.png Binary files differnew file mode 100644 index 000000000..72fda9ec2 --- /dev/null +++ b/libtdepim/about/bar-middle-left.png diff --git a/libtdepim/about/bar-middle-right.png b/libtdepim/about/bar-middle-right.png Binary files differnew file mode 100644 index 000000000..d482ab178 --- /dev/null +++ b/libtdepim/about/bar-middle-right.png diff --git a/libtdepim/about/bar-top-left.png b/libtdepim/about/bar-top-left.png Binary files differnew file mode 100644 index 000000000..b05fd216c --- /dev/null +++ b/libtdepim/about/bar-top-left.png diff --git a/libtdepim/about/bar-top-middle.png b/libtdepim/about/bar-top-middle.png Binary files differnew file mode 100644 index 000000000..387f81017 --- /dev/null +++ b/libtdepim/about/bar-top-middle.png diff --git a/libtdepim/about/bar-top-right.png b/libtdepim/about/bar-top-right.png Binary files differnew file mode 100644 index 000000000..a552ff91d --- /dev/null +++ b/libtdepim/about/bar-top-right.png diff --git a/libtdepim/about/bottom-left.png b/libtdepim/about/bottom-left.png Binary files differnew file mode 100644 index 000000000..78c9fece3 --- /dev/null +++ b/libtdepim/about/bottom-left.png diff --git a/libtdepim/about/bottom-middle.png b/libtdepim/about/bottom-middle.png Binary files differnew file mode 100644 index 000000000..41e52dd8a --- /dev/null +++ b/libtdepim/about/bottom-middle.png diff --git a/libtdepim/about/bottom-right.png b/libtdepim/about/bottom-right.png Binary files differnew file mode 100644 index 000000000..ef81b04a8 --- /dev/null +++ b/libtdepim/about/bottom-right.png diff --git a/libtdepim/about/box-bottom-left.png b/libtdepim/about/box-bottom-left.png Binary files differnew file mode 100644 index 000000000..ef68ffa6a --- /dev/null +++ b/libtdepim/about/box-bottom-left.png diff --git a/libtdepim/about/box-bottom-middle.png b/libtdepim/about/box-bottom-middle.png Binary files differnew file mode 100644 index 000000000..2bcd3ea03 --- /dev/null +++ b/libtdepim/about/box-bottom-middle.png diff --git a/libtdepim/about/box-bottom-right.png b/libtdepim/about/box-bottom-right.png Binary files differnew file mode 100644 index 000000000..993e1a6b0 --- /dev/null +++ b/libtdepim/about/box-bottom-right.png diff --git a/libtdepim/about/box-middle-left.png b/libtdepim/about/box-middle-left.png Binary files differnew file mode 100644 index 000000000..62a720178 --- /dev/null +++ b/libtdepim/about/box-middle-left.png diff --git a/libtdepim/about/box-middle-right.png b/libtdepim/about/box-middle-right.png Binary files differnew file mode 100644 index 000000000..aa94c0c90 --- /dev/null +++ b/libtdepim/about/box-middle-right.png diff --git a/libtdepim/about/box-top-left.png b/libtdepim/about/box-top-left.png Binary files differnew file mode 100644 index 000000000..3825a7de4 --- /dev/null +++ b/libtdepim/about/box-top-left.png diff --git a/libtdepim/about/box-top-middle.png b/libtdepim/about/box-top-middle.png Binary files differnew file mode 100644 index 000000000..79e714cba --- /dev/null +++ b/libtdepim/about/box-top-middle.png diff --git a/libtdepim/about/box-top-right.png b/libtdepim/about/box-top-right.png Binary files differnew file mode 100644 index 000000000..45ba201ea --- /dev/null +++ b/libtdepim/about/box-top-right.png diff --git a/libtdepim/about/kde_infopage.css b/libtdepim/about/kde_infopage.css new file mode 100644 index 000000000..69085675e --- /dev/null +++ b/libtdepim/about/kde_infopage.css @@ -0,0 +1,239 @@ + +/* + * text styles + */ + +img {margin: 0px; padding: 0px} +body {margin: 0px; padding: 0px; background-color: #418ade;} +.bar_text a {color: #008;} + +#subtext { + font-size: 10pt; + font-style: italic; +} + +#nextlink { + margin-bottom: 0px; + text-align: right; + font-size: 10pt; +} + +/* + * the header + */ + +#header { + background-image: url(top-middle.png); + width: 100%; + height: 131px; +} + +#headerL { + position: absolute; + background-image: url(top-left.png); + left: 0px; + height: 131px; + width: 147px; + z-index: 1; +} + +#headerR { + position: absolute; + right: 0px; +} + +/* title and tagline are part of the header diff */ +#title { + position: absolute; + top: 63px; + right: 100px; + margin-top: -1em; + text-align: right; + font-size: xx-large; + font-weight: bold; + text-shadow: #fff 0px 0px 5px; + color: #444; + z-index: 5; +} + +#tagline { + position: absolute; + top: 70px; + right: 100px; + text-align: right; + font-size: large; + font-weight: bold; + text-shadow: #fff 0px 0px 5px; + color: #444; + z-index: 5; +} + +/* + * the nav bar + */ + +#bar { + width: 100%; + background-color: #5babe5; + padding-top: 0.5ex; + border-bottom: 1px solid black; + padding-bottom: 0.5ex; +} + +#barCenter { + text-align: center; + color: #282828; + font-weight: bold; + font-size: small; +} + +#barCenter a.selected, #barCenter a.selected:hover { + color: #282828; + text-decoration: none; + text-shadow: #fff 0px 0px 5px; +} + + +#barCenter li a:link, #barCenter li a:visited, #barCenter li a:active { + color: #282828; + text-decoration: none; + text-shadow: none; +} + +#barCenter li a:hover { + color: #282828; + text-decoration: none; + text-shadow: #fff 0px 0px 6px; +} + +#barCenter ul { + margin: 0; + padding: 0; +} + +#barCenter li { + display: inline; +} + +#barCenter li:not(:first-child):before { /* Aren't css3 selectors great? */ + content: " · "; +} + +/* + * the main box + */ + +#box { + width: 90%; + margin-left: 5%; + margin-right: 5%; + margin-top: 10px; + margin-bottom: 10px; +} + +#boxT { + width: 100%; + height: 22px; +} + +#boxTL { + width: 25px; + height: 22px; + float: left; + background-image: url(box-top-left.png); +} + +#boxTR { + width: 25px; + height: 22px; + float: right; + background-image: url(box-top-right.png); +} + +#boxTC { + height: 22px; + margin-left: 25px; + margin-right: 25px; + background-image: url(box-top-middle.png); + background-repeat: repeat-x; +} + +#boxL { + background-image: url(box-middle-left.png); + background-repeat: repeat-y; + background-position: left; + padding-left: 20px; +} + +#boxCenter { + background-color: #dfe7f3; + background-position: center; + text-align: left; +} + +#boxR { + background-image: url(box-middle-right.png); + background-repeat: repeat-y; + background-position: right; + padding-right: 20px; +} + +#boxB { + width: 100%; + height: 22px; +} + +#boxBL { + width: 25px; + height: 22px; + float: left; + background-image: url(box-bottom-left.png); +} + +#boxBR { + width: 25px; + height: 22px; + float: right; + background-image: url(box-bottom-right.png); +} + +#boxBC { + height: 22px; + margin-left: 25px; + margin-right: 25px; + background-image: url(box-bottom-middle.png); + background-repeat: repeat-x; +} + +/* + * the footer + */ + +#footer { + position: fixed; + background-image: url(bottom-middle.png); + width: 100%; + height: 100px; + z-index: -2; + bottom:0; +} + +#footerL { + position: fixed; + background-image: url(bottom-left.png); + left: 0px; + width: 155px; + height: 100px; + z-index: -2; +} + +#footerR { + position: fixed; + background-image: url(bottom-right.png); + right: 0px; + width: 429px; + height: 100px; + z-index: -1; +} + +/* vim:set sw=2 et nocindent smartindent: */ diff --git a/libtdepim/about/kde_infopage_rtl.css b/libtdepim/about/kde_infopage_rtl.css new file mode 100644 index 000000000..003330196 --- /dev/null +++ b/libtdepim/about/kde_infopage_rtl.css @@ -0,0 +1,11 @@ +body {direction: rtl} + +#boxCenter { + text-align: right; +} + +#nextlink { + text-align: left; +} + +/* vim:set sw=2 et nocindent smartindent: */ diff --git a/libtdepim/about/top-left.png b/libtdepim/about/top-left.png Binary files differnew file mode 100644 index 000000000..d7551c0d5 --- /dev/null +++ b/libtdepim/about/top-left.png diff --git a/libtdepim/about/top-middle.png b/libtdepim/about/top-middle.png Binary files differnew file mode 100644 index 000000000..bb3fe7428 --- /dev/null +++ b/libtdepim/about/top-middle.png diff --git a/libtdepim/addresseediffalgo.cpp b/libtdepim/addresseediffalgo.cpp new file mode 100644 index 000000000..d36b7ac94 --- /dev/null +++ b/libtdepim/addresseediffalgo.cpp @@ -0,0 +1,146 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "addresseediffalgo.h" + +using namespace KPIM; + +static bool compareString( const TQString &left, const TQString &right ) +{ + if ( left.isEmpty() && right.isEmpty() ) + return true; + else + return left == right; +} + +AddresseeDiffAlgo::AddresseeDiffAlgo( const KABC::Addressee &leftAddressee, + const KABC::Addressee &rightAddressee ) + : mLeftAddressee( leftAddressee ), mRightAddressee( rightAddressee ) +{ +} + +void AddresseeDiffAlgo::run() +{ + begin(); + + if ( !compareString( mLeftAddressee.uid(), mRightAddressee.uid() ) ) + conflictField( KABC::Addressee::uidLabel(), mLeftAddressee.uid(), mRightAddressee.uid() ); + + if ( !compareString( mLeftAddressee.name(), mRightAddressee.name() ) ) + conflictField( KABC::Addressee::nameLabel(), mLeftAddressee.name(), mRightAddressee.name() ); + + if ( !compareString( mLeftAddressee.formattedName(), mRightAddressee.formattedName() ) ) + conflictField( KABC::Addressee::formattedNameLabel(), mLeftAddressee.formattedName(), mRightAddressee.formattedName() ); + + if ( !compareString( mLeftAddressee.familyName(), mRightAddressee.familyName() ) ) + conflictField( KABC::Addressee::familyNameLabel(), mLeftAddressee.familyName(), mRightAddressee.familyName() ); + + if ( !compareString( mLeftAddressee.givenName(), mRightAddressee.givenName() ) ) + conflictField( KABC::Addressee::givenNameLabel(), mLeftAddressee.givenName(), mRightAddressee.givenName() ); + + if ( !compareString( mLeftAddressee.additionalName(), mRightAddressee.additionalName() ) ) + conflictField( KABC::Addressee::additionalNameLabel(), mLeftAddressee.additionalName(), mRightAddressee.additionalName() ); + + if ( !compareString( mLeftAddressee.prefix(), mRightAddressee.prefix() ) ) + conflictField( KABC::Addressee::prefixLabel(), mLeftAddressee.prefix(), mRightAddressee.prefix() ); + + if ( !compareString( mLeftAddressee.suffix(), mRightAddressee.suffix() ) ) + conflictField( KABC::Addressee::suffixLabel(), mLeftAddressee.suffix(), mRightAddressee.suffix() ); + + if ( !compareString( mLeftAddressee.nickName(), mRightAddressee.nickName() ) ) + conflictField( KABC::Addressee::nickNameLabel(), mLeftAddressee.nickName(), mRightAddressee.nickName() ); + + if ( mLeftAddressee.birthday() != mRightAddressee.birthday() ) + conflictField( KABC::Addressee::birthdayLabel(), mLeftAddressee.birthday().toString(), + mRightAddressee.birthday().toString() ); + + if ( !compareString( mLeftAddressee.mailer(), mRightAddressee.mailer() ) ) + conflictField( KABC::Addressee::mailerLabel(), mLeftAddressee.mailer(), mRightAddressee.mailer() ); + + if ( mLeftAddressee.timeZone() != mRightAddressee.timeZone() ) + conflictField( KABC::Addressee::timeZoneLabel(), mLeftAddressee.timeZone().asString(), mRightAddressee.timeZone().asString() ); + + if ( mLeftAddressee.geo() != mRightAddressee.geo() ) + conflictField( KABC::Addressee::geoLabel(), mLeftAddressee.geo().asString(), mRightAddressee.geo().asString() ); + + if ( !compareString( mLeftAddressee.title(), mRightAddressee.title() ) ) + conflictField( KABC::Addressee::titleLabel(), mLeftAddressee.title(), mRightAddressee.title() ); + + if ( !compareString( mLeftAddressee.role(), mRightAddressee.role() ) ) + conflictField( KABC::Addressee::roleLabel(), mLeftAddressee.role(), mRightAddressee.role() ); + + if ( !compareString( mLeftAddressee.organization(), mRightAddressee.organization() ) ) + conflictField( KABC::Addressee::organizationLabel(), mLeftAddressee.organization(), mRightAddressee.organization() ); + + if ( !compareString( mLeftAddressee.note(), mRightAddressee.note() ) ) + conflictField( KABC::Addressee::noteLabel(), mLeftAddressee.note(), mRightAddressee.note() ); + + if ( !compareString( mLeftAddressee.productId(), mRightAddressee.productId() ) ) + conflictField( KABC::Addressee::productIdLabel(), mLeftAddressee.productId(), mRightAddressee.productId() ); + + if ( !compareString( mLeftAddressee.sortString(), mRightAddressee.sortString() ) ) + conflictField( KABC::Addressee::sortStringLabel(), mLeftAddressee.sortString(), mRightAddressee.sortString() ); + + if ( mLeftAddressee.secrecy() != mRightAddressee.secrecy() ) { + conflictField( KABC::Addressee::secrecyLabel(), mLeftAddressee.secrecy().asString(), mRightAddressee.secrecy().asString() ); + } + if ( mLeftAddressee.url()!= mRightAddressee.url() ) + conflictField( KABC::Addressee::urlLabel(), mLeftAddressee.url().prettyURL(), + mRightAddressee.url().prettyURL() ); + + if ( mLeftAddressee.logo() != mRightAddressee.logo() ) { + } + + if ( mLeftAddressee.photo() != mRightAddressee.photo() ) { + } + + diffList( "emails", mLeftAddressee.emails(), mRightAddressee.emails() ); + + diffList( "Phone Numbers", mLeftAddressee.phoneNumbers(), mRightAddressee.phoneNumbers() ); + diffList( "Addresses", mLeftAddressee.addresses(), mRightAddressee.addresses() ); + + end(); +} + +TQString AddresseeDiffAlgo::toString( const KABC::PhoneNumber &number ) +{ + return number.number(); +} + +TQString AddresseeDiffAlgo::toString( const KABC::Address &addr ) +{ + return addr.formattedAddress(); +} + +template <class L> +void AddresseeDiffAlgo::diffList( const TQString &id, + const TQValueList<L> &left, const TQValueList<L> &right ) +{ + for ( uint i = 0; i < left.count(); ++i ) { + if ( right.find( left[ i ] ) == right.end() ) + additionalLeftField( id, toString( left[ i ] ) ); + } + + for ( uint i = 0; i < right.count(); ++i ) { + if ( left.find( right[ i ] ) == left.end() ) + additionalRightField( id, toString( right[ i ] ) ); + } +} diff --git a/libtdepim/addresseediffalgo.h b/libtdepim/addresseediffalgo.h new file mode 100644 index 000000000..f3c6632a7 --- /dev/null +++ b/libtdepim/addresseediffalgo.h @@ -0,0 +1,50 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef KPIM_ADDRESSEEDIFFALGO_H +#define KPIM_ADDRESSEEDIFFALGO_H + +#include <kabc/addressee.h> +#include <libtdepim/diffalgo.h> + +namespace KPIM { + +class KDE_EXPORT AddresseeDiffAlgo : public DiffAlgo +{ + public: + AddresseeDiffAlgo( const KABC::Addressee &leftAddressee, const KABC::Addressee &rightAddressee ); + + void run(); + + private: + template <class L> + void diffList( const TQString &id, const TQValueList<L> &left, const TQValueList<L> &right ); + + TQString toString( const KABC::PhoneNumber &number ); + TQString toString( const KABC::Address &address ); + + KABC::Addressee mLeftAddressee; + KABC::Addressee mRightAddressee; +}; + +} + +#endif diff --git a/libtdepim/addresseeemailselection.cpp b/libtdepim/addresseeemailselection.cpp new file mode 100644 index 000000000..dde8f7dd4 --- /dev/null +++ b/libtdepim/addresseeemailselection.cpp @@ -0,0 +1,264 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <kglobal.h> +#include <kiconloader.h> +#include <klocale.h> + +#include "recentaddresses.h" + +#include "addresseeemailselection.h" + +using namespace KPIM; +using KRecentAddress::RecentAddresses; + +AddresseeEmailSelection::AddresseeEmailSelection() + : Selection() +{ +} + +uint AddresseeEmailSelection::fieldCount() const +{ + return 3; +} + +TQString AddresseeEmailSelection::fieldTitle( uint index ) const +{ + switch ( index ) { + case 0: + return i18n( "To" ); + break; + case 1: + return i18n( "Cc" ); + break; + case 2: + return i18n( "Bcc" ); + break; + default: + return TQString(); + } +} + +TQStringList AddresseeEmailSelection::to() const +{ + return mToEmailList; +} + +TQStringList AddresseeEmailSelection::cc() const +{ + return mCcEmailList; +} + +TQStringList AddresseeEmailSelection::bcc() const +{ + return mBccEmailList; +} + +KABC::Addressee::List AddresseeEmailSelection::toAddresses() const +{ + return mToAddresseeList; +} + +KABC::Addressee::List AddresseeEmailSelection::ccAddresses() const +{ + return mCcAddresseeList; +} + +KABC::Addressee::List AddresseeEmailSelection::bccAddresses() const +{ + return mBccAddresseeList; +} + +TQStringList AddresseeEmailSelection::toDistributionLists() const +{ + return mToDistributionList; +} + +TQStringList AddresseeEmailSelection::ccDistributionLists() const +{ + return mCcDistributionList; +} + +TQStringList AddresseeEmailSelection::bccDistributionLists() const +{ + return mBccDistributionList; +} + +void AddresseeEmailSelection::setSelectedTo( const TQStringList &emails ) +{ + setSelectedItem( 0, emails ); +} + +void AddresseeEmailSelection::setSelectedCC( const TQStringList &emails ) +{ + setSelectedItem( 1, emails ); +} + +void AddresseeEmailSelection::setSelectedBCC( const TQStringList &emails ) +{ + setSelectedItem( 2, emails ); +} + + +uint AddresseeEmailSelection::itemCount( const KABC::Addressee &addressee ) const +{ + return addressee.emails().count(); +} + +TQString AddresseeEmailSelection::itemText( const KABC::Addressee &addressee, uint index ) const +{ + return addressee.formattedName() + " " + email( addressee, index ); +} + +TQPixmap AddresseeEmailSelection::itemIcon( const KABC::Addressee &addressee, uint ) const +{ + if ( !addressee.photo().data().isNull() ) + return addressee.photo().data().smoothScale( 16, 16 ); + else + return KGlobal::iconLoader()->loadIcon( "personal", KIcon::Small ); +} + +bool AddresseeEmailSelection::itemEnabled( const KABC::Addressee &addressee, uint ) const +{ + return addressee.emails().count() != 0; +} + +bool AddresseeEmailSelection::itemMatches( const KABC::Addressee &addressee, uint index, const TQString &pattern ) const +{ + return addressee.formattedName().tqstartsWith( pattern, false ) || + email( addressee, index ).tqstartsWith( pattern, false ); +} + +bool AddresseeEmailSelection::itemEquals( const KABC::Addressee &addressee, uint index, const TQString &pattern ) const +{ + return (pattern == addressee.formattedName() + " " + email( addressee, index )) || + (addressee.emails().contains( pattern )); +} + +TQString AddresseeEmailSelection::distributionListText( const KABC::DistributionList *distributionList ) const +{ + return distributionList->name(); +} + +TQPixmap AddresseeEmailSelection::distributionListIcon( const KABC::DistributionList* ) const +{ + return KGlobal::iconLoader()->loadIcon( "kdmconfig", KIcon::Small ); +} + +bool AddresseeEmailSelection::distributionListEnabled( const KABC::DistributionList* ) const +{ + return true; +} + +bool AddresseeEmailSelection::distributionListMatches( const KABC::DistributionList *distributionList, + const TQString &pattern ) const +{ + // check whether the name of the distribution list matches the pattern or one of its entries. + bool ok = distributionList->name().tqstartsWith( pattern, false ); + + KABC::DistributionList::Entry::List entries = distributionList->entries(); + KABC::DistributionList::Entry::List::ConstIterator it; + for ( it = entries.begin(); it != entries.end(); ++it ) { + ok = ok || (*it).addressee.formattedName().tqstartsWith( pattern, false ) || + (*it).email.tqstartsWith( pattern, false ); + } + + return ok; +} + +uint AddresseeEmailSelection::addressBookCount() const +{ + // we provide the recent email addresses via the custom addressbooks + return 1; +} + +TQString AddresseeEmailSelection::addressBookTitle( uint index ) const +{ + if ( index == 0 ) + return i18n( "Recent Addresses" ); + else + return TQString(); +} + +KABC::Addressee::List AddresseeEmailSelection::addressBookContent( uint index ) const +{ + if ( index == 0 ) { + KConfig config( "kmailrc" ); + return RecentAddresses::self( &config )->kabcAddresses(); + } else { + return KABC::Addressee::List(); + } +} + +TQString AddresseeEmailSelection::email( const KABC::Addressee &addressee, uint index ) const +{ + return addressee.emails()[ index ]; +} + +void AddresseeEmailSelection::setSelectedItem( uint fieldIndex, const TQStringList &emails ) +{ + TQStringList::ConstIterator it; + for ( it = emails.begin(); it != emails.end(); ++it ) { + KABC::Addressee addr; + addr.insertEmail( *it, true ); + + selector()->setItemSelected( fieldIndex, addr, 0, *it ); + } +} + +void AddresseeEmailSelection::addSelectedAddressees( uint fieldIndex, const KABC::Addressee &addressee, uint itemIndex ) +{ + switch ( fieldIndex ) { + case 0: + mToAddresseeList.append( addressee ); + mToEmailList.append( email( addressee, itemIndex ) ); + break; + case 1: + mCcAddresseeList.append( addressee ); + mCcEmailList.append( email( addressee, itemIndex ) ); + break; + case 2: + mBccAddresseeList.append( addressee ); + mBccEmailList.append( email( addressee, itemIndex ) ); + break; + default: + // oops + break; + } +} + +void AddresseeEmailSelection::addSelectedDistributionList( uint fieldIndex, const KABC::DistributionList *list ) +{ + switch ( fieldIndex ) { + case 0: + mToDistributionList.append( list->name() ); + break; + case 1: + mCcDistributionList.append( list->name() ); + break; + case 2: + mBccDistributionList.append( list->name() ); + break; + default: + // oops + break; + } +} diff --git a/libtdepim/addresseeemailselection.h b/libtdepim/addresseeemailselection.h new file mode 100644 index 000000000..4ecb946e3 --- /dev/null +++ b/libtdepim/addresseeemailselection.h @@ -0,0 +1,148 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef KPIM_ADDRESSEE_EMAILSELECTION_H +#define KPIM_ADDRESSEE_EMAILSELECTION_H + +#include <addresseeselector.h> + +namespace KPIM { + +class KDE_EXPORT AddresseeEmailSelection : public Selection +{ + public: + AddresseeEmailSelection(); + + /** + Returns the number of fields the selection offers. + */ + virtual uint fieldCount() const; + + /** + Returns the title for the field specified by index. + */ + virtual TQString fieldTitle( uint index ) const; + + /** + Returns the number of items for the given addressee. + */ + virtual uint itemCount( const KABC::Addressee &addresse ) const; + + /** + Returns the text that's used for the item specified by index. + */ + virtual TQString itemText( const KABC::Addressee &addresse, uint index ) const; + + /** + Returns the icon that's used for the item specified by index. + */ + virtual TQPixmap itemIcon( const KABC::Addressee &addresse, uint index ) const; + + /** + Returns whether the item specified by index is enabled. + */ + virtual bool itemEnabled( const KABC::Addressee &addresse, uint index ) const; + + /** + Returns whether the item specified by index matches the passed pattern. + */ + virtual bool itemMatches( const KABC::Addressee &addresse, uint index, const TQString &pattern ) const; + + /** + Returns whether the item specified by index equals the passed pattern. + */ + virtual bool itemEquals( const KABC::Addressee &addresse, uint index, const TQString &pattern ) const; + + /** + Returns the text that's used for the given distribution list. + */ + virtual TQString distributionListText( const KABC::DistributionList *distributionList ) const; + + /** + Returns the icon that's used for the given distribution list. + */ + virtual TQPixmap distributionListIcon( const KABC::DistributionList *distributionList ) const; + + /** + Returns whether the given distribution list is enabled. + */ + virtual bool distributionListEnabled( const KABC::DistributionList *distributionList ) const; + + /** + Returns whether the given distribution list matches the passed pattern. + */ + virtual bool distributionListMatches( const KABC::DistributionList *distributionList, + const TQString &pattern ) const; + + /** + Returns the number of additional address books. + */ + virtual uint addressBookCount() const; + + /** + Returns the title for an additional address book. + */ + virtual TQString addressBookTitle( uint index ) const; + + /** + Returns the content for an additional address book. + */ + virtual KABC::Addressee::List addressBookContent( uint index ) const; + + TQStringList to() const; + TQStringList cc() const; + TQStringList bcc() const; + + KABC::Addressee::List toAddresses() const; + KABC::Addressee::List ccAddresses() const; + KABC::Addressee::List bccAddresses() const; + + TQStringList toDistributionLists() const; + TQStringList ccDistributionLists() const; + TQStringList bccDistributionLists() const; + + void setSelectedTo( const TQStringList &emails ); + void setSelectedCC( const TQStringList &emails ); + void setSelectedBCC( const TQStringList &emails ); + + private: + virtual void addSelectedAddressees( uint fieldIndex, const KABC::Addressee&, uint itemIndex ); + virtual void addSelectedDistributionList( uint fieldIndex, const KABC::DistributionList* ); + + TQString email( const KABC::Addressee&, uint ) const; + void setSelectedItem( uint fieldIndex, const TQStringList& ); + + KABC::Addressee::List mToAddresseeList; + KABC::Addressee::List mCcAddresseeList; + KABC::Addressee::List mBccAddresseeList; + + TQStringList mToEmailList; + TQStringList mCcEmailList; + TQStringList mBccEmailList; + + TQStringList mToDistributionList; + TQStringList mCcDistributionList; + TQStringList mBccDistributionList; +}; + +} + +#endif diff --git a/libtdepim/addresseelineedit.cpp b/libtdepim/addresseelineedit.cpp new file mode 100644 index 000000000..50d19e07b --- /dev/null +++ b/libtdepim/addresseelineedit.cpp @@ -0,0 +1,1285 @@ +/* + This file is part of libtdepim. + Copyright (c) 2002 Helge Deller <deller@gmx.de> + 2002 Lubos Lunak <llunak@suse.cz> + 2001,2003 Carsten Pfeiffer <pfeiffer@kde.org> + 2001 Waldo Bastian <bastian@kde.org> + 2004 Daniel Molkentin <danimo@klaralvdalens-datakonsult.se> + 2004 Karl-Heinz Zimmer <khz@klaralvdalens-datakonsult.se> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "addresseelineedit.h" + +#include "resourceabc.h" +#include "completionordereditor.h" +#include "ldapclient.h" + +#include <config.h> + +#ifdef KDEPIM_NEW_DISTRLISTS +#include "distributionlist.h" +#else +#include <kabc/distributionlist.h> +#endif + +#include <kabc/stdaddressbook.h> +#include <kabc/resource.h> +#include <libemailfunctions/email.h> + +#include <kcompletionbox.h> +#include <kcursor.h> +#include <kdebug.h> +#include <kstandarddirs.h> +#include <kstaticdeleter.h> +#include <kstdaccel.h> +#include <kurldrag.h> +#include <klocale.h> + +#include <tqpopupmenu.h> +#include <tqapplication.h> +#include <tqobject.h> +#include <tqptrlist.h> +#include <tqregexp.h> +#include <tqevent.h> +#include <tqdragobject.h> +#include <tqclipboard.h> + +using namespace KPIM; + +KMailCompletion * AddresseeLineEdit::s_completion = 0L; +KPIM::CompletionItemsMap* AddresseeLineEdit::s_completionItemMap = 0L; +TQStringList* AddresseeLineEdit::s_completionSources = 0L; +bool AddresseeLineEdit::s_addressesDirty = false; +TQTimer* AddresseeLineEdit::s_LDAPTimer = 0L; +KPIM::LdapSearch* AddresseeLineEdit::s_LDAPSearch = 0L; +TQString* AddresseeLineEdit::s_LDAPText = 0L; +AddresseeLineEdit* AddresseeLineEdit::s_LDAPLineEdit = 0L; + +// The weights associated with the completion sources in s_completionSources. +// Both are maintained by addCompletionSource(), don't attempt to modifiy those yourself. +TQMap<TQString,int>* s_completionSourceWeights = 0; + +// maps LDAP client indices to completion source indices +// the assumption that they are always the first n indices in s_completion +// does not hold when clients are added later on +TQMap<int, int>* AddresseeLineEdit::s_ldapClientToCompletionSourceMap = 0; + +static KStaticDeleter<KMailCompletion> completionDeleter; +static KStaticDeleter<KPIM::CompletionItemsMap> completionItemsDeleter; +static KStaticDeleter<TQTimer> ldapTimerDeleter; +static KStaticDeleter<KPIM::LdapSearch> ldapSearchDeleter; +static KStaticDeleter<TQString> ldapTextDeleter; +static KStaticDeleter<TQStringList> completionSourcesDeleter; +static KStaticDeleter<TQMap<TQString,int> > completionSourceWeightsDeleter; +static KStaticDeleter<TQMap<int, int> > ldapClientToCompletionSourceMapDeleter; + +// needs to be unique, but the actual name doesn't matter much +static TQCString newLineEditDCOPObjectName() +{ + static int s_count = 0; + TQCString name( "KPIM::AddresseeLineEdit" ); + if ( s_count++ ) { + name += '-'; + name += TQCString().setNum( s_count ); + } + return name; +} + +static const TQString s_completionItemIndentString = " "; + +static bool itemIsHeader( const TQListBoxItem* item ) +{ + return item && !item->text().startsWith( s_completionItemIndentString ); +} + + + +AddresseeLineEdit::AddresseeLineEdit( TQWidget* parent, bool useCompletion, + const char *name ) + : ClickLineEdit( parent, TQString(), name ), DCOPObject( newLineEditDCOPObjectName() ), + m_useSemiColonAsSeparator( false ), m_allowDistLists( true ) +{ + m_useCompletion = useCompletion; + m_completionInitialized = false; + m_smartPaste = false; + m_addressBookConnected = false; + m_searchExtended = false; + + init(); + + if ( m_useCompletion ) + s_addressesDirty = true; +} + +void AddresseeLineEdit::updateLDAPWeights() +{ + /* Add completion sources for all ldap server, 0 to n. Added first so + * that they map to the ldapclient::clientNumber() */ + s_LDAPSearch->updateCompletionWeights(); + TQValueList< LdapClient* > clients = s_LDAPSearch->clients(); + int clientIndex = 0; + for ( TQValueList<LdapClient*>::iterator it = clients.begin(); it != clients.end(); ++it, ++clientIndex ) { + const int sourceIndex = addCompletionSource( "LDAP server: " + (*it)->server().host(), (*it)->completionWeight() ); + s_ldapClientToCompletionSourceMap->insert( clientIndex, sourceIndex ); + } +} + +void AddresseeLineEdit::init() +{ + if ( !s_completion ) { + completionDeleter.setObject( s_completion, new KMailCompletion() ); + s_completion->setOrder( completionOrder() ); + s_completion->setIgnoreCase( true ); + + completionItemsDeleter.setObject( s_completionItemMap, new KPIM::CompletionItemsMap() ); + completionSourcesDeleter.setObject( s_completionSources, new TQStringList() ); + completionSourceWeightsDeleter.setObject( s_completionSourceWeights, new TQMap<TQString,int> ); + ldapClientToCompletionSourceMapDeleter.setObject( s_ldapClientToCompletionSourceMap, new TQMap<int,int> ); + } +// connect( s_completion, TQT_SIGNAL( match( const TQString& ) ), +// this, TQT_SLOT( slotMatched( const TQString& ) ) ); + + if ( m_useCompletion ) { + if ( !s_LDAPTimer ) { + ldapTimerDeleter.setObject( s_LDAPTimer, new TQTimer( 0, "ldapTimerDeleter" ) ); + ldapSearchDeleter.setObject( s_LDAPSearch, new KPIM::LdapSearch ); + ldapTextDeleter.setObject( s_LDAPText, new TQString ); + } + + updateLDAPWeights(); + + if ( !m_completionInitialized ) { + setCompletionObject( s_completion, false ); + connect( this, TQT_SIGNAL( completion( const TQString& ) ), + this, TQT_SLOT( slotCompletion() ) ); + connect( this, TQT_SIGNAL( returnPressed( const TQString& ) ), + this, TQT_SLOT( slotReturnPressed( const TQString& ) ) ); + + KCompletionBox *box = completionBox(); + connect( box, TQT_SIGNAL( highlighted( const TQString& ) ), + this, TQT_SLOT( slotPopupCompletion( const TQString& ) ) ); + connect( box, TQT_SIGNAL( userCancelled( const TQString& ) ), + TQT_SLOT( slotUserCancelled( const TQString& ) ) ); + + // The emitter is always called KPIM::IMAPCompletionOrder by contract + if ( !connectDCOPSignal( 0, "KPIM::IMAPCompletionOrder", "orderChanged()", + "slotIMAPCompletionOrderChanged()", false ) ) + kdError() << "AddresseeLineEdit: connection to orderChanged() failed" << endl; + + connect( s_LDAPTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotStartLDAPLookup() ) ); + connect( s_LDAPSearch, TQT_SIGNAL( searchData( const KPIM::LdapResultList& ) ), + TQT_SLOT( slotLDAPSearchData( const KPIM::LdapResultList& ) ) ); + + m_completionInitialized = true; + } + } +} + +AddresseeLineEdit::~AddresseeLineEdit() +{ + if ( s_LDAPSearch && s_LDAPLineEdit == this ) + stopLDAPLookup(); +} + +void AddresseeLineEdit::setFont( const TQFont& font ) +{ + KLineEdit::setFont( font ); + if ( m_useCompletion ) + completionBox()->setFont( font ); +} + +void AddresseeLineEdit::allowSemiColonAsSeparator( bool useSemiColonAsSeparator ) +{ + m_useSemiColonAsSeparator = useSemiColonAsSeparator; +} + +void AddresseeLineEdit::allowDistributionLists( bool allowDistLists ) +{ + m_allowDistLists = allowDistLists; +} + +void AddresseeLineEdit::keyPressEvent( TQKeyEvent *e ) +{ + bool accept = false; + + if ( KStdAccel::shortcut( KStdAccel::SubstringCompletion ).contains( KKey( e ) ) ) { + //TODO: add LDAP substring lookup, when it becomes available in KPIM::LDAPSearch + updateSearchString(); + doCompletion( true ); + accept = true; + } else if ( KStdAccel::shortcut( KStdAccel::TextCompletion ).contains( KKey( e ) ) ) { + int len = text().length(); + + if ( len == cursorPosition() ) { // at End? + updateSearchString(); + doCompletion( true ); + accept = true; + } + } + + const TQString oldContent = text(); + if ( !accept ) + KLineEdit::keyPressEvent( e ); + + // if the text didn't change (eg. because a cursor navigation key was pressed) + // we don't need to trigger a new search + if ( oldContent == text() ) + return; + + if ( e->isAccepted() ) { + updateSearchString(); + TQString searchString( m_searchString ); + //LDAP does not know about our string manipulation, remove it + if ( m_searchExtended ) + searchString = m_searchString.mid( 1 ); + + if ( m_useCompletion && s_LDAPTimer != NULL ) { + if ( *s_LDAPText != searchString || s_LDAPLineEdit != this ) + stopLDAPLookup(); + + *s_LDAPText = searchString; + s_LDAPLineEdit = this; + s_LDAPTimer->start( 500, true ); + } + } +} + +void AddresseeLineEdit::insert( const TQString &t ) +{ + if ( !m_smartPaste ) { + KLineEdit::insert( t ); + return; + } + + //kdDebug(5300) << " AddresseeLineEdit::insert( \"" << t << "\" )" << endl; + + TQString newText = t.stripWhiteSpace(); + if ( newText.isEmpty() ) + return; + + // remove newlines in the to-be-pasted string + TQStringList lines = TQStringList::split( TQRegExp("\r?\n"), newText, false ); + for ( TQStringList::iterator it = lines.begin(); + it != lines.end(); ++it ) { + // remove trailing commas and whitespace + (*it).remove( TQRegExp(",?\\s*$") ); + } + newText = lines.join( ", " ); + + if ( newText.startsWith("mailto:") ) { + KURL url( newText ); + newText = url.path(); + } + else if ( newText.find(" at ") != -1 ) { + // Anti-spam stuff + newText.replace( " at ", "@" ); + newText.replace( " dot ", "." ); + } + else if ( newText.find("(at)") != -1 ) { + newText.replace( TQRegExp("\\s*\\(at\\)\\s*"), "@" ); + } + + TQString contents = text(); + int start_sel = 0; + int pos = cursorPosition( ); + + if ( hasSelectedText() ) { + // Cut away the selection. + start_sel = selectionStart(); + pos = start_sel; + contents = contents.left( start_sel ) + contents.mid( start_sel + selectedText().length() ); + } + + int eot = contents.length(); + while ( ( eot > 0 ) && contents[ eot - 1 ].isSpace() ) { + eot--; + } + if ( eot == 0 ) { + contents = TQString(); + } else if ( pos >= eot ) { + if ( contents[ eot - 1 ] == ',' ) { + eot--; + } + contents.truncate( eot ); + contents += ", "; + pos = eot + 2; + } + + contents = contents.left( pos ) + newText + contents.mid( pos ); + setText( contents ); + setEdited( true ); + setCursorPosition( pos + newText.length() ); +} + +void AddresseeLineEdit::setText( const TQString & text ) +{ + ClickLineEdit::setText( text.stripWhiteSpace() ); +} + +void AddresseeLineEdit::paste() +{ + if ( m_useCompletion ) + m_smartPaste = true; + + KLineEdit::paste(); + m_smartPaste = false; +} + +void AddresseeLineEdit::mouseReleaseEvent( TQMouseEvent *e ) +{ + // reimplemented from TQLineEdit::mouseReleaseEvent() + if ( m_useCompletion + && TQApplication::tqclipboard()->supportsSelection() + && !isReadOnly() + && e->button() == Qt::MidButton ) { + m_smartPaste = true; + } + + KLineEdit::mouseReleaseEvent( e ); + m_smartPaste = false; +} + +void AddresseeLineEdit::dropEvent( TQDropEvent *e ) +{ + KURL::List uriList; + if ( !isReadOnly() ) { + if ( KURLDrag::canDecode(e) && KURLDrag::decode( e, uriList ) ) { + TQString contents = text(); + // remove trailing white space and comma + int eot = contents.length(); + while ( ( eot > 0 ) && contents[ eot - 1 ].isSpace() ) + eot--; + if ( eot == 0 ) + contents = TQString(); + else if ( contents[ eot - 1 ] == ',' ) { + eot--; + contents.truncate( eot ); + } + bool mailtoURL = false; + // append the mailto URLs + for ( KURL::List::Iterator it = uriList.begin(); + it != uriList.end(); ++it ) { + if ( !contents.isEmpty() ) + contents.append( ", " ); + KURL u( *it ); + if ( u.protocol() == "mailto" ) { + mailtoURL = true; + contents.append( (*it).path() ); + } + } + if ( mailtoURL ) { + setText( contents ); + setEdited( true ); + return; + } + } else { + // Let's see if this drop contains a comma separated list of emails + TQString dropData = TQString::fromUtf8( e->tqencodedData( "text/plain" ) ); + TQStringList addrs = splitEmailAddrList( dropData ); + if ( addrs.count() > 0 ) { + setText( normalizeAddressesAndDecodeIDNs( dropData ) ); + setEdited( true ); + return; + } + } + } + + if ( m_useCompletion ) + m_smartPaste = true; + TQLineEdit::dropEvent( e ); + m_smartPaste = false; +} + +void AddresseeLineEdit::cursorAtEnd() +{ + setCursorPosition( text().length() ); +} + +void AddresseeLineEdit::enableCompletion( bool enable ) +{ + m_useCompletion = enable; +} + +void AddresseeLineEdit::doCompletion( bool ctrlT ) +{ + m_lastSearchMode = ctrlT; + + KGlobalSettings::Completion mode = completionMode(); + + if ( mode == KGlobalSettings::CompletionNone ) + return; + + if ( s_addressesDirty ) { + loadContacts(); // read from local address book + s_completion->setOrder( completionOrder() ); + } + + // cursor at end of string - or Ctrl+T pressed for substring completion? + if ( ctrlT ) { + const TQStringList completions = getAdjustedCompletionItems( false ); + + if ( completions.count() > 1 ) + ; //m_previousAddresses = prevAddr; + else if ( completions.count() == 1 ) + setText( m_previousAddresses + completions.first().stripWhiteSpace() ); + + setCompletedItems( completions, true ); // this makes sure the completion popup is closed if no matching items were found + + cursorAtEnd(); + setCompletionMode( mode ); //set back to previous mode + return; + } + + + switch ( mode ) { + case KGlobalSettings::CompletionPopupAuto: + { + if ( m_searchString.isEmpty() ) + break; + } + + case KGlobalSettings::CompletionPopup: + { + const TQStringList items = getAdjustedCompletionItems( true ); + setCompletedItems( items, false ); + break; + } + + case KGlobalSettings::CompletionShell: + { + TQString match = s_completion->makeCompletion( m_searchString ); + if ( !match.isNull() && match != m_searchString ) { + setText( m_previousAddresses + match ); + setEdited( true ); + cursorAtEnd(); + } + break; + } + + case KGlobalSettings::CompletionMan: // Short-Auto in fact + case KGlobalSettings::CompletionAuto: + { + //force autoSuggest in KLineEdit::keyPressed or setCompletedText will have no effect + setCompletionMode( completionMode() ); + + if ( !m_searchString.isEmpty() ) { + + //if only our \" is left, remove it since user has not typed it either + if ( m_searchExtended && m_searchString == "\"" ){ + m_searchExtended = false; + m_searchString = TQString(); + setText( m_previousAddresses ); + break; + } + + TQString match = s_completion->makeCompletion( m_searchString ); + + if ( !match.isEmpty() ) { + if ( match != m_searchString ) { + TQString adds = m_previousAddresses + match; + setCompletedText( adds ); + } + } else { + if ( !m_searchString.startsWith( "\"" ) ) { + //try with quoted text, if user has not type one already + match = s_completion->makeCompletion( "\"" + m_searchString ); + if ( !match.isEmpty() && match != m_searchString ) { + m_searchString = "\"" + m_searchString; + m_searchExtended = true; + setText( m_previousAddresses + m_searchString ); + setCompletedText( m_previousAddresses + match ); + } + } else if ( m_searchExtended ) { + //our added \" does not work anymore, remove it + m_searchString = m_searchString.mid( 1 ); + m_searchExtended = false; + setText( m_previousAddresses + m_searchString ); + //now try again + match = s_completion->makeCompletion( m_searchString ); + if ( !match.isEmpty() && match != m_searchString ) { + TQString adds = m_previousAddresses + match; + setCompletedText( adds ); + } + } + } + } + break; + } + + case KGlobalSettings::CompletionNone: + default: // fall through + break; + } +} + +void AddresseeLineEdit::slotPopupCompletion( const TQString& completion ) +{ + setText( m_previousAddresses + completion.stripWhiteSpace() ); + cursorAtEnd(); +// slotMatched( m_previousAddresses + completion ); + updateSearchString(); +} + +void AddresseeLineEdit::slotReturnPressed( const TQString& item ) +{ + Q_UNUSED( item ); + TQListBoxItem* i = completionBox()->selectedItem(); + if ( i != 0 ) + slotPopupCompletion( i->text() ); +} + +void AddresseeLineEdit::loadContacts() +{ + s_completion->clear(); + s_completionItemMap->clear(); + s_addressesDirty = false; + //m_contactMap.clear(); + + TQApplication::setOverrideCursor( KCursor::waitCursor() ); // loading might take a while + + KConfig config( "kpimcompletionorder" ); // The weights for non-imap kabc resources is there. + config.setGroup( "CompletionWeights" ); + + KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true ); + // Can't just use the addressbook's iterator, we need to know which subresource + // is behind which contact. + TQPtrList<KABC::Resource> resources( addressBook->resources() ); + for( TQPtrListIterator<KABC::Resource> resit( resources ); *resit; ++resit ) { + KABC::Resource* resource = *resit; + KPIM::ResourceABC* resabc = dynamic_cast<ResourceABC *>( resource ); + if ( resabc ) { // IMAP KABC resource; need to associate each contact with the subresource + const TQMap<TQString, TQString> uidToResourceMap = resabc->uidToResourceMap(); + KABC::Resource::Iterator it; + for ( it = resource->begin(); it != resource->end(); ++it ) { + TQString uid = (*it).uid(); + TQMap<TQString, TQString>::const_iterator wit = uidToResourceMap.find( uid ); + const TQString subresourceLabel = resabc->subresourceLabel( *wit ); + const int weight = ( wit != uidToResourceMap.end() ) ? resabc->subresourceCompletionWeight( *wit ) : 80; + const int idx = addCompletionSource( subresourceLabel, weight ); + + //kdDebug(5300) << (*it).fullEmail() << " subres=" << *wit << " weight=" << weight << endl; + addContact( *it, weight, idx ); + } + } else { // KABC non-imap resource + int weight = config.readNumEntry( resource->identifier(), 60 ); + int sourceIndex = addCompletionSource( resource->resourceName(), weight ); + KABC::Resource::Iterator it; + for ( it = resource->begin(); it != resource->end(); ++it ) { + addContact( *it, weight, sourceIndex ); + } + } + } + +#ifndef KDEPIM_NEW_DISTRLISTS // new distr lists are normal contact, already done above + int weight = config.readNumEntry( "DistributionLists", 60 ); + KABC::DistributionListManager manager( addressBook ); + manager.load(); + const TQStringList distLists = manager.listNames(); + TQStringList::const_iterator listIt; + int idx = addCompletionSource( i18n( "Distribution Lists" ) ); + for ( listIt = distLists.begin(); listIt != distLists.end(); ++listIt ) { + + //for KGlobalSettings::CompletionAuto + addCompletionItem( (*listIt).simplifyWhiteSpace(), weight, idx ); + + //for CompletionShell, CompletionPopup + TQStringList sl( (*listIt).simplifyWhiteSpace() ); + addCompletionItem( (*listIt).simplifyWhiteSpace(), weight, idx, &sl ); + + } +#endif + + TQApplication::restoreOverrideCursor(); + + if ( !m_addressBookConnected ) { + connect( addressBook, TQT_SIGNAL( addressBookChanged( AddressBook* ) ), TQT_SLOT( loadContacts() ) ); + m_addressBookConnected = true; + } +} + +void AddresseeLineEdit::addContact( const KABC::Addressee& addr, int weight, int source ) +{ +#ifdef KDEPIM_NEW_DISTRLISTS + if ( KPIM::DistributionList::isDistributionList( addr ) ) { + //kdDebug(5300) << "AddresseeLineEdit::addContact() distribution list \"" << addr.formattedName() << "\" weight=" << weight << endl; + + if ( m_allowDistLists ) { + //for CompletionAuto + addCompletionItem( addr.formattedName(), weight, source ); + + //for CompletionShell, CompletionPopup + TQStringList sl( addr.formattedName() ); + addCompletionItem( addr.formattedName(), weight, source, &sl ); + } + + return; + } +#endif + //m_contactMap.insert( addr.realName(), addr ); + const TQStringList emails = addr.emails(); + TQStringList::ConstIterator it; + const int prefEmailWeight = 1; //increment weight by prefEmailWeight + int isPrefEmail = prefEmailWeight; //first in list is preferredEmail + for ( it = emails.begin(); it != emails.end(); ++it ) { + //TODO: highlight preferredEmail + const TQString email( (*it) ); + const TQString givenName = addr.givenName(); + const TQString familyName= addr.familyName(); + const TQString nickName = addr.nickName(); + const TQString domain = email.mid( email.find( '@' ) + 1 ); + TQString fullEmail = addr.fullEmail( email ); + //TODO: let user decide what fields to use in lookup, e.g. company, city, ... + + //for CompletionAuto + if ( givenName.isEmpty() && familyName.isEmpty() ) { + addCompletionItem( fullEmail, weight + isPrefEmail, source ); // use whatever is there + } else { + const TQString byFirstName= "\"" + givenName + " " + familyName + "\" <" + email + ">"; + const TQString byLastName = "\"" + familyName + ", " + givenName + "\" <" + email + ">"; + addCompletionItem( byFirstName, weight + isPrefEmail, source ); + addCompletionItem( byLastName, weight + isPrefEmail, source ); + } + + addCompletionItem( email, weight + isPrefEmail, source ); + + if ( !nickName.isEmpty() ){ + const TQString byNick = "\"" + nickName + "\" <" + email + ">"; + addCompletionItem( byNick, weight + isPrefEmail, source ); + } + + if ( !domain.isEmpty() ){ + const TQString byDomain = "\"" + domain + " " + familyName + " " + givenName + "\" <" + email + ">"; + addCompletionItem( byDomain, weight + isPrefEmail, source ); + } + + //for CompletionShell, CompletionPopup + TQStringList keyWords; + const TQString realName = addr.realName(); + + if ( !givenName.isEmpty() && !familyName.isEmpty() ) { + keyWords.append( givenName + " " + familyName ); + keyWords.append( familyName + " " + givenName ); + keyWords.append( familyName + ", " + givenName); + }else if ( !givenName.isEmpty() ) + keyWords.append( givenName ); + else if ( !familyName.isEmpty() ) + keyWords.append( familyName ); + + if ( !nickName.isEmpty() ) + keyWords.append( nickName ); + + if ( !realName.isEmpty() ) + keyWords.append( realName ); + + if ( !domain.isEmpty() ) + keyWords.append( domain ); + + keyWords.append( email ); + + /* KMailCompletion does not have knowledge about identities, it stores emails and + * keywords for each email. KMailCompletion::allMatches does a lookup on the + * keywords and returns an ordered list of emails. In order to get the preferred + * email before others for each identity we use this little trick. + * We remove the <blank> in getAdjustedCompletionItems. + */ + if ( isPrefEmail == prefEmailWeight ) + fullEmail.replace( " <", " <" ); + + addCompletionItem( fullEmail, weight + isPrefEmail, source, &keyWords ); + isPrefEmail = 0; + +#if 0 + int len = (*it).length(); + if ( len == 0 ) continue; + if( '\0' == (*it)[len-1] ) + --len; + const TQString tmp = (*it).left( len ); + const TQString fullEmail = addr.fullEmail( tmp ); + //kdDebug(5300) << "AddresseeLineEdit::addContact() \"" << fullEmail << "\" weight=" << weight << endl; + addCompletionItem( fullEmail.simplifyWhiteSpace(), weight, source ); + // Try to guess the last name: if found, we add an extra + // entry to the list to make sure completion works even + // if the user starts by typing in the last name. + TQString name( addr.realName().simplifyWhiteSpace() ); + if( name.endsWith("\"") ) + name.truncate( name.length()-1 ); + if( name.startsWith("\"") ) + name = name.mid( 1 ); + + // While we're here also add "email (full name)" for completion on the email + if ( !name.isEmpty() ) + addCompletionItem( addr.preferredEmail() + " (" + name + ")", weight, source ); + + bool bDone = false; + int i = -1; + while( ( i = name.findRev(' ') ) > 1 && !bDone ) { + TQString sLastName( name.mid( i+1 ) ); + if( ! sLastName.isEmpty() && + 2 <= sLastName.length() && // last names must be at least 2 chars long + ! sLastName.endsWith(".") ) { // last names must not end with a dot (like "Jr." or "Sr.") + name.truncate( i ); + if( !name.isEmpty() ){ + sLastName.prepend( "\"" ); + sLastName.append( ", " + name + "\" <" ); + } + TQString sExtraEntry( sLastName ); + sExtraEntry.append( tmp.isEmpty() ? addr.preferredEmail() : tmp ); + sExtraEntry.append( ">" ); + //kdDebug(5300) << "AddresseeLineEdit::addContact() added extra \"" << sExtraEntry.simplifyWhiteSpace() << "\" weight=" << weight << endl; + addCompletionItem( sExtraEntry.simplifyWhiteSpace(), weight, source ); + bDone = true; + } + if( !bDone ) { + name.truncate( i ); + if( name.endsWith("\"") ) + name.truncate( name.length()-1 ); + } + } +#endif + } +} + +void AddresseeLineEdit::addCompletionItem( const TQString& string, int weight, int completionItemSource, const TQStringList * keyWords ) +{ + // Check if there is an exact match for item already, and use the max weight if so. + // Since there's no way to get the information from KCompletion, we have to keep our own TQMap + CompletionItemsMap::iterator it = s_completionItemMap->find( string ); + if ( it != s_completionItemMap->end() ) { + weight = TQMAX( ( *it ).first, weight ); + ( *it ).first = weight; + } else { + s_completionItemMap->insert( string, tqMakePair( weight, completionItemSource ) ); + } + if ( keyWords == 0 ) + s_completion->addItem( string, weight ); + else + s_completion->addItemWithKeys( string, weight, keyWords ); +} + +void AddresseeLineEdit::slotStartLDAPLookup() +{ + KGlobalSettings::Completion mode = completionMode(); + + if ( mode == KGlobalSettings::CompletionNone ) + return; + + if ( !s_LDAPSearch->isAvailable() ) { + return; + } + if ( s_LDAPLineEdit != this ) + return; + + startLoadingLDAPEntries(); +} + +void AddresseeLineEdit::stopLDAPLookup() +{ + s_LDAPSearch->cancelSearch(); + s_LDAPLineEdit = NULL; +} + +void AddresseeLineEdit::startLoadingLDAPEntries() +{ + TQString s( *s_LDAPText ); + // TODO cache last? + TQString prevAddr; + int n = s.findRev( ',' ); + if ( n >= 0 ) { + prevAddr = s.left( n + 1 ) + ' '; + s = s.mid( n + 1, 255 ).stripWhiteSpace(); + } + + if ( s.isEmpty() ) + return; + + //loadContacts(); // TODO reuse these? + s_LDAPSearch->startSearch( s ); +} + +void AddresseeLineEdit::slotLDAPSearchData( const KPIM::LdapResultList& adrs ) +{ + if ( adrs.isEmpty() || s_LDAPLineEdit != this ) + return; + + for ( KPIM::LdapResultList::ConstIterator it = adrs.begin(); it != adrs.end(); ++it ) { + KABC::Addressee addr; + addr.setNameFromString( (*it).name ); + addr.setEmails( (*it).email ); + + if ( !s_ldapClientToCompletionSourceMap->contains( (*it).clientNumber ) ) + updateLDAPWeights(); // we got results from a new source, so update the completion sources + + addContact( addr, (*it).completionWeight, (*s_ldapClientToCompletionSourceMap)[ (*it ).clientNumber ] ); + } + + if ( (hasFocus() || completionBox()->hasFocus() ) + && completionMode() != KGlobalSettings::CompletionNone + && completionMode() != KGlobalSettings::CompletionShell ) { + setText( m_previousAddresses + m_searchString ); + // only complete again if the user didn't change the selection while we were waiting + // otherwise the completion box will be closed + if ( m_searchString.stripWhiteSpace() != completionBox()->currentText().stripWhiteSpace() ) + doCompletion( m_lastSearchMode ); + } +} + +void AddresseeLineEdit::setCompletedItems( const TQStringList& items, bool autoSuggest ) +{ + KCompletionBox* completionBox = this->completionBox(); + + if ( !items.isEmpty() && + !(items.count() == 1 && m_searchString == items.first()) ) + { + TQString oldCurrentText = completionBox->currentText(); + TQListBoxItem *itemUnderMouse = completionBox->itemAt( + completionBox->viewport()->mapFromGlobal(TQCursor::pos()) ); + TQString oldTextUnderMouse; + TQPoint oldPosOfItemUnderMouse; + if ( itemUnderMouse ) { + oldTextUnderMouse = itemUnderMouse->text(); + oldPosOfItemUnderMouse = completionBox->tqitemRect( itemUnderMouse ).topLeft(); + } + + completionBox->setItems( items ); + + if ( !completionBox->isVisible() ) { + if ( !m_searchString.isEmpty() ) + completionBox->setCancelledText( m_searchString ); + completionBox->popup(); + // we have to install the event filter after popup(), since that + // calls show(), and that's where KCompletionBox installs its filter. + // We want to be first, though, so do it now. + if ( s_completion->order() == KCompletion::Weighted ) + tqApp->installEventFilter( this ); + } + + // Try to re-select what was selected before, otherrwise use the first + // item, if there is one + TQListBoxItem* item = 0; + if ( oldCurrentText.isEmpty() + || ( item = completionBox->findItem( oldCurrentText ) ) == 0 ) { + item = completionBox->item( 1 ); + } + if ( item ) + { + if ( itemUnderMouse ) { + TQListBoxItem *newItemUnderMouse = completionBox->findItem( oldTextUnderMouse ); + // if the mouse was over an item, before, but now that's elsewhere, + // move the cursor, so folks don't accidently click the wrong item + if ( newItemUnderMouse ) { + TQRect r = completionBox->tqitemRect( newItemUnderMouse ); + TQPoint target = r.topLeft(); + if ( oldPosOfItemUnderMouse != target ) { + target.setX( target.x() + r.width()/2 ); + TQCursor::setPos( completionBox->viewport()->mapToGlobal(target) ); + } + } + } + completionBox->blockSignals( true ); + completionBox->setSelected( item, true ); + completionBox->setCurrentItem( item ); + completionBox->ensureCurrentVisible(); + + completionBox->blockSignals( false ); + } + + if ( autoSuggest ) + { + int index = items.first().find( m_searchString ); + TQString newText = items.first().mid( index ); + setUserSelection(false); + setCompletedText(newText,true); + } + } + else + { + if ( completionBox && completionBox->isVisible() ) { + completionBox->hide(); + completionBox->setItems( TQStringList() ); + } + } +} + +TQPopupMenu* AddresseeLineEdit::createPopupMenu() +{ + TQPopupMenu *menu = KLineEdit::createPopupMenu(); + if ( !menu ) + return 0; + + if ( m_useCompletion ){ + menu->setItemVisible( ShortAutoCompletion, false ); + menu->setItemVisible( PopupAutoCompletion, false ); + menu->insertItem( i18n( "Configure Completion Order..." ), + this, TQT_SLOT( slotEditCompletionOrder() ) ); + } + return menu; +} + +void AddresseeLineEdit::slotEditCompletionOrder() +{ + init(); // for s_LDAPSearch + CompletionOrderEditor editor( s_LDAPSearch, this ); + editor.exec(); + if ( m_useCompletion ) { + updateLDAPWeights(); + s_addressesDirty = true; + } +} + +void KPIM::AddresseeLineEdit::slotIMAPCompletionOrderChanged() +{ + if ( m_useCompletion ) + s_addressesDirty = true; +} + +void KPIM::AddresseeLineEdit::slotUserCancelled( const TQString& cancelText ) +{ + if ( s_LDAPSearch && s_LDAPLineEdit == this ) + stopLDAPLookup(); + userCancelled( m_previousAddresses + cancelText ); // in KLineEdit +} + +void AddresseeLineEdit::updateSearchString() +{ + m_searchString = text(); + + int n = -1; + bool inQuote = false; + uint searchStringLength = m_searchString.length(); + for ( uint i = 0; i < searchStringLength; ++i ) { + if ( m_searchString[ i ] == '"' ) { + inQuote = !inQuote; + } + if ( m_searchString[ i ] == '\\' && + (i + 1) < searchStringLength && m_searchString[ i + 1 ] == '"' ) { + ++i; + } + if ( inQuote ) { + continue; + } + if ( i < searchStringLength && + ( m_searchString[ i ] == ',' || + ( m_useSemiColonAsSeparator && m_searchString[ i ] == ';' ) ) ) { + n = i; + } + } + + if ( n >= 0 ) { + ++n; // Go past the "," + + int len = m_searchString.length(); + + // Increment past any whitespace... + while ( n < len && m_searchString[ n ].isSpace() ) + ++n; + + m_previousAddresses = m_searchString.left( n ); + m_searchString = m_searchString.mid( n ).stripWhiteSpace(); + } else { + m_previousAddresses = TQString(); + } +} + +void KPIM::AddresseeLineEdit::slotCompletion() +{ + // Called by KLineEdit's keyPressEvent for CompletionModes Auto,Popup -> new text, update search string + // not called for CompletionShell, this is been taken care of in AddresseeLineEdit::keyPressEvent + updateSearchString(); + if ( completionBox() ) + completionBox()->setCancelledText( m_searchString ); + doCompletion( false ); +} + +// not cached, to make sure we get an up-to-date value when it changes +KCompletion::CompOrder KPIM::AddresseeLineEdit::completionOrder() +{ + KConfig config( "kpimcompletionorder" ); + config.setGroup( "General" ); + const TQString order = config.readEntry( "CompletionOrder", "Weighted" ); + + if ( order == "Weighted" ) + return KCompletion::Weighted; + else + return KCompletion::Sorted; +} + +int KPIM::AddresseeLineEdit::addCompletionSource( const TQString &source, int weight ) +{ + TQMap<TQString,int>::iterator it = s_completionSourceWeights->find( source ); + if ( it == s_completionSourceWeights->end() ) + s_completionSourceWeights->insert( source, weight ); + else + (*s_completionSourceWeights)[source] = weight; + + int sourceIndex = s_completionSources->findIndex( source ); + if ( sourceIndex == -1 ) { + s_completionSources->append( source ); + return s_completionSources->size() - 1; + } + else + return sourceIndex; +} + +bool KPIM::AddresseeLineEdit::eventFilter(TQObject *obj, TQEvent *e) +{ + if ( TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(completionBox()) ) { + if ( e->type() == TQEvent::MouseButtonPress || + e->type() == TQEvent::MouseMove || + e->type() == TQEvent::MouseButtonRelease || + e->type() == TQEvent::MouseButtonDblClick ) { + TQMouseEvent* me = TQT_TQMOUSEEVENT( e ); + // find list box item at the event position + TQListBoxItem *item = completionBox()->itemAt( me->pos() ); + if ( !item ) { + // In the case of a mouse move outside of the box we don't want + // the parent to fuzzy select a header by mistake. + bool eat = e->type() == TQEvent::MouseMove; + return eat; + } + // avoid selection of headers on button press, or move or release while + // a button is pressed + if ( e->type() == TQEvent::MouseButtonPress + || me->state() & Qt::LeftButton || me->state() & Qt::MidButton + || me->state() & Qt::RightButton ) { + if ( itemIsHeader(item) ) { + return true; // eat the event, we don't want anything to happen + } else { + // if we are not on one of the group heading, make sure the item + // below or above is selected, not the heading, inadvertedly, due + // to fuzzy auto-selection from TQListBox + completionBox()->setCurrentItem( item ); + completionBox()->setSelected( completionBox()->index( item ), true ); + if ( e->type() == TQEvent::MouseMove ) + return true; // avoid fuzzy selection behavior + } + } + } + } + if ( ( TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(this) ) && + ( e->type() == TQEvent::AccelOverride ) ) { + TQKeyEvent *ke = TQT_TQKEYEVENT( e ); + if ( ke->key() == Key_Up || ke->key() == Key_Down || ke->key() == Key_Tab ) { + ke->accept(); + return true; + } + } + if ( ( TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(this) ) && + ( e->type() == TQEvent::KeyPress || e->type() == TQEvent::KeyRelease ) && + completionBox()->isVisible() ) { + TQKeyEvent *ke = TQT_TQKEYEVENT( e ); + int currentIndex = completionBox()->currentItem(); + if ( currentIndex < 0 ) { + return true; + } + + if ( ke->key() == Key_Up ) { + //kdDebug() << "EVENTFILTER: Key_Up currentIndex=" << currentIndex << endl; + // figure out if the item we would be moving to is one we want + // to ignore. If so, go one further + TQListBoxItem *itemAbove = completionBox()->item( currentIndex ); + if ( itemAbove && itemIsHeader(itemAbove) ) { + // there is a header above us, check if there is even further up + // and if so go one up, so it'll be selected + if ( currentIndex > 0 && completionBox()->item( currentIndex - 1 ) ) { + //kdDebug() << "EVENTFILTER: Key_Up -> skipping " << currentIndex - 1 << endl; + completionBox()->setCurrentItem( itemAbove->prev() ); + completionBox()->setSelected( currentIndex - 1, true ); + } else if ( currentIndex == 0 ) { + // nothing to skip to, let's stay where we are, but make sure the + // first header becomes visible, if we are the first real entry + completionBox()->ensureVisible( 0, 0 ); + //Kolab issue 2941: be sure to add email even if it's the only element. + if ( itemIsHeader( completionBox()->item( currentIndex ) ) ) { + currentIndex++; + } + completionBox()->setCurrentItem( itemAbove ); + completionBox()->setSelected( currentIndex, true ); + } + return true; + } + } else if ( ke->key() == Key_Down ) { + // same strategy for downwards + //kdDebug() << "EVENTFILTER: Key_Down. currentIndex=" << currentIndex << endl; + TQListBoxItem *itemBelow = completionBox()->item( currentIndex ); + if ( itemBelow && itemIsHeader( itemBelow ) ) { + if ( completionBox()->item( currentIndex + 1 ) ) { + //kdDebug() << "EVENTFILTER: Key_Down -> skipping " << currentIndex+1 << endl; + completionBox()->setCurrentItem( itemBelow->next() ); + completionBox()->setSelected( currentIndex + 1, true ); + } else { + // nothing to skip to, let's stay where we are + completionBox()->setCurrentItem( itemBelow ); + completionBox()->setSelected( currentIndex, true ); + } + return true; + } + // special case of the last and only item in the list needing selection + if ( !itemBelow && currentIndex == 1 ) { + completionBox()->setSelected( currentIndex, true ); + } + // special case of the initial selection, which is unfortunately a header. + // Setting it to selected tricks KCompletionBox into not treating is special + // and selecting making it current, instead of the one below. + TQListBoxItem *item = completionBox()->item( currentIndex ); + if ( item && itemIsHeader(item) ) { + completionBox()->setSelected( currentIndex, true ); + } + } else if ( e->type() == TQEvent::KeyRelease && + ( ke->key() == Key_Tab || ke->key() == Key_Backtab ) ) { + //kdDebug() << "EVENTFILTER: Key_Tab. currentIndex=" << currentIndex << endl; + /// first, find the header of the current section + TQListBoxItem *myHeader = 0; + const int iterationstep = ke->key() == Key_Tab ? 1 : -1; + int i = TQMIN( TQMAX( currentIndex - iterationstep, 0 ), completionBox()->count() - 1 ); + while ( i>=0 ) { + if ( itemIsHeader( completionBox()->item(i) ) ) { + myHeader = completionBox()->item( i ); + break; + } + i--; + } + Q_ASSERT( myHeader ); // we should always be able to find a header + + // find the next header (searching backwards, for Key_Backtab) + TQListBoxItem *nextHeader = 0; + // when iterating forward, start at the currentindex, when backwards, + // one up from our header, or at the end + uint j; + if ( ke->key() == Key_Tab ) { + j = currentIndex; + } else { + i = completionBox()->index( myHeader ); + if ( i == 0 ) { + j = completionBox()->count() - 1; + } else { + j = ( i - 1 ) % completionBox()->count(); + } + } + while ( ( nextHeader = completionBox()->item( j ) ) && nextHeader != myHeader ) { + if ( itemIsHeader(nextHeader) ) { + break; + } + j = (j + iterationstep) % completionBox()->count(); + } + if ( nextHeader && nextHeader != myHeader ) { + TQListBoxItem *item = completionBox()->item( j + 1 ); + if ( item && !itemIsHeader(item) ) { + completionBox()->setSelected( item, true ); + completionBox()->setCurrentItem( item ); + completionBox()->ensureCurrentVisible(); + } + } + return true; + } + } + return ClickLineEdit::eventFilter( obj, e ); +} + +class SourceWithWeight { + public: + int weight; // the weight of the source + TQString sourceName; // the name of the source, e.g. "LDAP Server" + int index; // index into s_completionSources + + bool operator< ( const SourceWithWeight &other ) { + if ( weight > other.weight ) + return true; + if ( weight < other.weight ) + return false; + return sourceName < other.sourceName; + } +}; + +const TQStringList KPIM::AddresseeLineEdit::getAdjustedCompletionItems( bool fullSearch ) +{ + TQStringList items = fullSearch ? + s_completion->allMatches( m_searchString ) + : s_completion->substringCompletion( m_searchString ); + + // For weighted mode, the algorithm is the following: + // In the first loop, we add each item to its section (there is one section per completion source) + // We also add spaces in front of the items. + // The sections are appended to the items list. + // In the second loop, we then walk through the sections and add all the items in there to the + // sorted item list, which is the final result. + // + // The algo for non-weighted mode is different. + + int lastSourceIndex = -1; + unsigned int i = 0; + + // Maps indices of the items list, which are section headers/source items, + // to a TQStringList which are the items of that section/source. + TQMap<int, TQStringList> sections; + TQStringList sortedItems; + for ( TQStringList::Iterator it = items.begin(); it != items.end(); ++it, ++i ) { + CompletionItemsMap::const_iterator cit = s_completionItemMap->find(*it); + if ( cit == s_completionItemMap->end() ) + continue; + int idx = (*cit).second; + + if ( s_completion->order() == KCompletion::Weighted ) { + if ( lastSourceIndex == -1 || lastSourceIndex != idx ) { + const TQString sourceLabel( (*s_completionSources)[idx] ); + if ( sections.find(idx) == sections.end() ) { + items.insert( it, sourceLabel ); + } + lastSourceIndex = idx; + } + (*it) = (*it).prepend( s_completionItemIndentString ); + // remove preferred email sort <blank> added in addContact() + (*it).replace( " <", " <" ); + } + sections[idx].append( *it ); + + if ( s_completion->order() == KCompletion::Sorted ) { + sortedItems.append( *it ); + } + } + + if ( s_completion->order() == KCompletion::Weighted ) { + + // Sort the sections + TQValueList<SourceWithWeight> sourcesAndWeights; + for ( uint i = 0; i < s_completionSources->size(); i++ ) { + SourceWithWeight sww; + sww.sourceName = (*s_completionSources)[i]; + sww.weight = (*s_completionSourceWeights)[sww.sourceName]; + sww.index = i; + sourcesAndWeights.append( sww ); + } + qHeapSort( sourcesAndWeights ); + + // Add the sections and their items to the final sortedItems result list + for( uint i = 0; i < sourcesAndWeights.size(); i++ ) { + TQStringList sectionItems = sections[sourcesAndWeights[i].index]; + if ( !sectionItems.isEmpty() ) { + sortedItems.append( sourcesAndWeights[i].sourceName ); + TQStringList sectionItems = sections[sourcesAndWeights[i].index]; + for ( TQStringList::Iterator sit( sectionItems.begin() ), send( sectionItems.end() ); + sit != send; ++sit ) { + sortedItems.append( *sit ); + } + } + } + } else { + sortedItems.sort(); + } + return sortedItems; +} +#include "addresseelineedit.moc" diff --git a/libtdepim/addresseelineedit.h b/libtdepim/addresseelineedit.h new file mode 100644 index 000000000..209151b6e --- /dev/null +++ b/libtdepim/addresseelineedit.h @@ -0,0 +1,183 @@ +/* + This file is part of libtdepim. + Copyright (c) 2002 Helge Deller <deller@gmx.de> + 2002 Lubos Lunak <llunak@suse.cz> + 2001,2003 Carsten Pfeiffer <pfeiffer@kde.org> + 2001 Waldo Bastian <bastian@kde.org> + 2004 Daniel Molkentin <danimo@klaralvdalens-datakonsult.se> + 2004 Karl-Heinz Zimmer <khz@klaralvdalens-datakonsult.se> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef ADDRESSEELINEEDIT_H +#define ADDRESSEELINEEDIT_H + +#include <tqobject.h> +#include <tqptrlist.h> +#include <tqtimer.h> +#include <tqpair.h> +#include <tqvaluelist.h> + +#include <kabc/addressee.h> + +#include "clicklineedit.h" +#include "kmailcompletion.h" +#include <dcopobject.h> +#include <tdepimmacros.h> + +class KConfig; + +namespace KPIM { +class LdapSearch; +class LdapResult; +typedef TQValueList<LdapResult> LdapResultList; +typedef TQMap< TQString, TQPair<int,int> > CompletionItemsMap; +} + +namespace KPIM { + +class KDE_EXPORT AddresseeLineEdit : public ClickLineEdit, public DCOPObject +{ + K_DCOP + Q_OBJECT + TQ_OBJECT + + public: + AddresseeLineEdit( TQWidget* parent, bool useCompletion = true, + const char *name = 0L); + virtual ~AddresseeLineEdit(); + + virtual void setFont( const TQFont& ); + void allowSemiColonAsSeparator( bool ); + + /// Sets if distribution lists will be used for completion. + /// This is true by default. + /// Call this right after the constructor, before anything calls loadContacts(), + /// otherwise this has no effect. + void allowDistributionLists( bool allowDistLists ); + + public slots: + void cursorAtEnd(); + void enableCompletion( bool enable ); + /** Reimplemented for stripping whitespace after completion */ + virtual void setText( const TQString& txt ); + + protected slots: + virtual void loadContacts(); + protected: + void addContact( const KABC::Addressee&, int weight, int source = -1 ); + virtual void keyPressEvent( TQKeyEvent* ); + /** + * Reimplemented for smart insertion of email addresses. + * Features: + * - Automatically adds ',' if necessary to separate email addresses + * - Correctly decodes mailto URLs + * - Recognizes email addresses which are protected against address + * harvesters, i.e. "name at kde dot org" and "name(at)kde.org" + */ + virtual void insert( const TQString &text ); + /** Reimplemented for smart insertion of pasted email addresses. */ + virtual void paste(); + /** Reimplemented for smart insertion with middle mouse button. */ + virtual void mouseReleaseEvent( TQMouseEvent *e ); + /** Reimplemented for smart insertion of dragged email addresses. */ + virtual void dropEvent( TQDropEvent *e ); + void doCompletion( bool ctrlT ); + virtual TQPopupMenu *createPopupMenu(); + + /** + * Adds the name of a completion source to the internal list of + * such sources and returns its index, such that that can be used + * for insertion of items associated with that source. + * + * If the source already exists, the weight will be updated. + */ + int addCompletionSource( const TQString&, int weight ); + + /** return whether we are using sorted or weighted display */ + static KCompletion::CompOrder completionOrder(); + + k_dcop: + // Connected to the DCOP signal + void slotIMAPCompletionOrderChanged(); + + private slots: + void slotCompletion(); + void slotPopupCompletion( const TQString& ); + void slotReturnPressed( const TQString& ); + void slotStartLDAPLookup(); + void slotLDAPSearchData( const KPIM::LdapResultList& ); + void slotEditCompletionOrder(); + void slotUserCancelled( const TQString& ); + + private: + virtual bool eventFilter(TQObject *o, TQEvent *e); + void init(); + void startLoadingLDAPEntries(); + void stopLDAPLookup(); + void updateLDAPWeights(); + + void setCompletedItems( const TQStringList& items, bool autoSuggest ); + void addCompletionItem( const TQString& string, int weight, int source, const TQStringList * keyWords=0 ); + TQString completionSearchText( TQString& ); + const TQStringList getAdjustedCompletionItems( bool fullSearch ); + void updateSearchString(); + + TQString m_previousAddresses; + TQString m_searchString; + bool m_useCompletion; + bool m_completionInitialized; + bool m_smartPaste; + bool m_addressBookConnected; + bool m_lastSearchMode; + bool m_searchExtended; //has \" been added? + bool m_useSemiColonAsSeparator; + bool m_allowDistLists; + + //TQMap<TQString, KABC::Addressee> m_contactMap; + + static bool s_addressesDirty; + static KMailCompletion *s_completion; + static CompletionItemsMap* s_completionItemMap; + static TQTimer *s_LDAPTimer; + static KPIM::LdapSearch *s_LDAPSearch; + static TQString *s_LDAPText; + static AddresseeLineEdit *s_LDAPLineEdit; + static TQStringList *s_completionSources; + static TQMap<int,int> *s_ldapClientToCompletionSourceMap; + + class AddresseeLineEditPrivate; + AddresseeLineEditPrivate *d; + + //until MenuID moves into protected in KLineEdit, we keep a copy here + //Constants that represent the ID's of the popup menu. + enum MenuID + { + Default = 42, + NoCompletion, + AutoCompletion, + ShellCompletion, + PopupCompletion, + ShortAutoCompletion, + PopupAutoCompletion + }; + +}; + +} + +#endif diff --git a/libtdepim/addresseeselector.cpp b/libtdepim/addresseeselector.cpp new file mode 100644 index 000000000..792d57625 --- /dev/null +++ b/libtdepim/addresseeselector.cpp @@ -0,0 +1,581 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <tqheader.h> +#include <tqlabel.h> +#include <tqlayout.h> +#include <tqsignalmapper.h> +#include <tqtoolbutton.h> + +#include <kabc/stdaddressbook.h> +#include <kcombobox.h> +#include <kdialog.h> +#include <kglobal.h> +#include <kiconloader.h> +#include <klineedit.h> +#include <klistview.h> +#include <klocale.h> + +#include "addresseeselector.h" + +using namespace KPIM; + +class AddresseeSelector::AddressBookManager +{ + public: + TQStringList titles() const; + + void addResource( KABC::Resource* ); + void addAddressBook( const TQString &title, SelectionItem::List &list ); + + void clear(); + bool contains( uint index, const SelectionItem& ); + + private: + struct AddressBookEntry { + TQString title; + SelectionItem::List list; + }; + + TQValueList<KABC::Resource*> mResources; + TQValueList<AddressBookEntry> mAddressBooks; +}; + +TQStringList AddresseeSelector::AddressBookManager::titles() const +{ + TQStringList titles; + + // we've always an 'all' entry + titles.append( i18n( "All" ) ); + + TQValueList<KABC::Resource*>::ConstIterator resIt; + for ( resIt = mResources.begin(); resIt != mResources.end(); ++resIt ) + titles.append( (*resIt)->resourceName() ); + + TQValueList<AddressBookEntry>::ConstIterator abIt; + for ( abIt = mAddressBooks.begin(); abIt != mAddressBooks.end(); ++abIt ) + titles.append( (*abIt).title ); + + return titles; +} + +void AddresseeSelector::AddressBookManager::addResource( KABC::Resource *resource ) +{ + if ( mResources.find( resource ) == mResources.end() ) + mResources.append( resource ); +} + +void AddresseeSelector::AddressBookManager::addAddressBook( const TQString &title, + SelectionItem::List &list ) +{ + AddressBookEntry entry; + entry.title = title; + entry.list = list; + + + // TODO: check for duplicates + mAddressBooks.append( entry ); +} + +void AddresseeSelector::AddressBookManager::clear() +{ + mResources.clear(); + mAddressBooks.clear(); +} + +bool AddresseeSelector::AddressBookManager::contains( uint index, const SelectionItem &item ) +{ + if ( index == 0 ) // the 'all' entry + return true; + + if ( mResources.count() > 0 ) { + if ( index <= mResources.count() ) { + index--; + if ( item.addressee().resource() == mResources[ index ] ) + return true; + else + return false; + } + } + + index = index - mResources.count(); + + if ( mAddressBooks.count() > 0 ) { + if ( index <= mAddressBooks.count() ) { + index--; + AddressBookEntry entry = mAddressBooks[ index ]; + SelectionItem::List::ConstIterator it; + for ( it = entry.list.begin(); it != entry.list.end(); ++it ) + if ( (*it).addressee() == item.addressee() ) + return true; + + return false; + } + } + + return false; +} + + +SelectionItem::SelectionItem( const KABC::Addressee &addressee, uint index ) + : mAddressee( addressee ), mDistributionList( 0 ), mIndex( index ) +{ + mField.fill( false, 10 ); +} + +SelectionItem::SelectionItem( KABC::DistributionList *list, uint index ) + : mDistributionList( list ), mIndex( index ) +{ + mField.fill( false, 10 ); +} + +SelectionItem::SelectionItem() + : mDistributionList( 0 ), mIndex( 0 ) +{ + mField.fill( false, 10 ); +} + +void SelectionItem::addToField( int index ) +{ + mField.setBit( index ); +} + +void SelectionItem::removeFromField( int index ) +{ + mField.clearBit( index ); +} + +bool SelectionItem::isInField( int index ) +{ + return mField.testBit( index ); +} + +KABC::Addressee SelectionItem::addressee() const +{ + return mAddressee; +} + +KABC::DistributionList* SelectionItem::distributionList() const +{ + return mDistributionList; +} + +uint SelectionItem::index() const +{ + return mIndex; +} + + +class SelectionViewItem : public TQListViewItem +{ + public: + SelectionViewItem( TQListView *parent, Selection *selection, + SelectionItem *item ) + : TQListViewItem( parent, "" ), mSelection( selection ), mItem( item ) + { + if ( mItem->distributionList() == 0 ) + mIcon = mSelection->itemIcon( mItem->addressee(), mItem->index() ); + else + mIcon = mSelection->distributionListIcon( mItem->distributionList() ); + } + + TQString text( int column ) const + { + if ( column == 0 ) { + if ( mItem->distributionList() == 0 ) + return mSelection->itemText( mItem->addressee(), mItem->index() ); + else + return mSelection->distributionListText( mItem->distributionList() ); + } else + return TQString(); + } + + const TQPixmap* pixmap( int column ) const + { + if ( column == 0 ) { + return &mIcon; + } else + return 0; + } + + SelectionItem* item() const { return mItem; } + + private: + Selection *mSelection; + SelectionItem *mItem; + TQPixmap mIcon; +}; + +AddresseeSelector::AddresseeSelector( Selection *selection, TQWidget *parent, const char *name ) + : TQWidget( parent, name ), mSelection( selection ), mManager( 0 ) +{ + mMoveMapper = new TQSignalMapper( TQT_TQOBJECT(this) ); + mRemoveMapper = new TQSignalMapper( TQT_TQOBJECT(this) ); + + mAddressBookManager = new AddressBookManager(); + + initGUI(); + + init(); + + mSelection->setSelector( this ); +} + +AddresseeSelector::~AddresseeSelector() +{ + delete mManager; + mManager = 0; + + delete mAddressBookManager; + mAddressBookManager = 0; +} + +void AddresseeSelector::init() +{ + connect( KABC::StdAddressBook::self( true ), TQT_SIGNAL( addressBookChanged( AddressBook* ) ), + this, TQT_SLOT( reloadAddressBook() ) ); + connect( mAddresseeFilter, TQT_SIGNAL( textChanged( const TQString& ) ), + this, TQT_SLOT( updateAddresseeView() ) ); + connect( mAddressBookCombo, TQT_SIGNAL( activated( int ) ), + this, TQT_SLOT( updateAddresseeView() ) ); + + connect( mMoveMapper, TQT_SIGNAL( mapped( int ) ), + this, TQT_SLOT( move( int ) ) ); + connect( mRemoveMapper, TQT_SIGNAL( mapped( int ) ), + this, TQT_SLOT( remove( int ) ) ); + + reloadAddressBook(); +} + +void AddresseeSelector::initGUI() +{ + TQGridLayout *tqlayout = new TQGridLayout( this, 2, 3, KDialog::marginHint(), KDialog::spacingHint() ); + TQGridLayout *topLayout = new TQGridLayout( this, 2, 2, KDialog::marginHint() ); + + TQLabel *label = new TQLabel( i18n( "Address book:" ), this ); + mAddressBookCombo = new KComboBox( false, this ); + + topLayout->addWidget( label, 0, 0 ); + topLayout->addWidget( mAddressBookCombo, 0, 1 ); + + label = new TQLabel( i18n( "Search:" ), this ); + mAddresseeFilter = new KLineEdit( this ); + + topLayout->addWidget( label, 1, 0 ); + topLayout->addWidget( mAddresseeFilter, 1, 1 ); + + topLayout->setColStretch( 1, 1 ); + + tqlayout->addMultiCellLayout( topLayout, 0, 0, 0, 2 ); + + int row = 1; + + TQIconSet moveSet = KGlobal::iconLoader()->loadIconSet( "next", KIcon::Small ); + TQIconSet removeSet = KGlobal::iconLoader()->loadIconSet( "previous", KIcon::Small ); + + uint count = mSelection->fieldCount(); + for ( uint i = 0; i < count; ++i, ++row ) { + KListView *listView = new KListView( this ); + listView->addColumn( mSelection->fieldTitle( i ) ); + listView->setFullWidth( true ); + mSelectionViews.append( listView ); + + connect( listView, TQT_SIGNAL( doubleClicked( TQListViewItem*, const TQPoint&, int ) ), + mRemoveMapper, TQT_SLOT( map() ) ); + mRemoveMapper->setMapping( TQT_TQOBJECT(listView), i ); + + TQVBoxLayout *buttonLayout = new TQVBoxLayout( this ); + buttonLayout->tqsetAlignment( TQt::AlignBottom ); + tqlayout->addLayout( buttonLayout, row, 1 ); + + // move button + TQToolButton *moveButton = new TQToolButton( this ); + moveButton->setIconSet( moveSet ); + moveButton->setFixedSize( 30, 30 ); + + connect( moveButton, TQT_SIGNAL( clicked() ), + mMoveMapper, TQT_SLOT( map() ) ); + mMoveMapper->setMapping( TQT_TQOBJECT(moveButton), i ); + + // remove button + TQToolButton *removeButton = new TQToolButton( this ); + removeButton->setIconSet( removeSet ); + removeButton->setFixedSize( 30, 30 ); + + connect( removeButton, TQT_SIGNAL( clicked() ), + mRemoveMapper, TQT_SLOT( map() ) ); + mRemoveMapper->setMapping( TQT_TQOBJECT(removeButton), i ); + + buttonLayout->addWidget( moveButton ); + buttonLayout->addWidget( removeButton ); + + tqlayout->addWidget( listView, row, 2 ); + } + + mAddresseeView = new KListView( this ); + mAddresseeView->addColumn( "" ); + mAddresseeView->header()->hide(); + mAddresseeView->setFullWidth( true ); + + tqlayout->addMultiCellWidget( mAddresseeView, 1, row, 0, 0 ); +} + +void AddresseeSelector::finish() +{ + SelectionItem::List::Iterator it; + + for ( uint field = 0; field < mSelection->fieldCount(); ++field ) { + for ( it = mSelectionItems.begin(); it != mSelectionItems.end(); ++it ) { + if ( (*it).isInField( field ) ) { + if ( (*it).distributionList() == 0 ) + mSelection->addSelectedAddressees( field, (*it).addressee(), (*it).index() ); + else + mSelection->addSelectedDistributionList( field, (*it).distributionList() ); + } + } + } +} + +void AddresseeSelector::updateAddresseeView() +{ + mAddresseeView->clear(); + + int addressBookIndex = mAddressBookCombo->currentItem(); + + SelectionItem::List::Iterator it; + for ( it = mSelectionItems.begin(); it != mSelectionItems.end(); ++it ) { + if ( mAddressBookManager->contains( addressBookIndex, *it ) ) { + if ( (*it).distributionList() == 0 ) { + if ( mAddresseeFilter->text().isEmpty() || + mSelection->itemMatches( (*it).addressee(), (*it).index(), + mAddresseeFilter->text() ) ) + new SelectionViewItem( mAddresseeView, mSelection, &(*it) ); + } else { + if ( mAddresseeFilter->text().isEmpty() || + mSelection->distributionListMatches( (*it).distributionList(), + mAddresseeFilter->text() ) ) + new SelectionViewItem( mAddresseeView, mSelection, &(*it) ); + } + } + } + + updateSelectionViews(); +} + +void AddresseeSelector::move( int index ) +{ + SelectionViewItem *item = dynamic_cast<SelectionViewItem*>( mAddresseeView->selectedItem() ); + if ( item ) { + item->item()->addToField( index ); + updateSelectionView( index ); + } +} + +void AddresseeSelector::remove( int index ) +{ + KListView *view = mSelectionViews[ index ]; + + SelectionViewItem *item = dynamic_cast<SelectionViewItem*>( view->selectedItem() ); + if ( item ) { + item->item()->removeFromField( index ); + updateSelectionView( index ); + } +} + +void AddresseeSelector::setItemSelected( uint fieldIndex, const KABC::Addressee &addr, uint itemIndex ) +{ + bool found = false; + + SelectionItem::List::Iterator it; + for ( it = mSelectionItems.begin(); it != mSelectionItems.end(); ++it ) { + if ( (*it).addressee() == addr && (*it).index() == itemIndex ) { + (*it).addToField( fieldIndex ); + found = true; + } + } + + if ( !found ) { + SelectionItem item( addr, itemIndex ); + item.addToField( fieldIndex ); + + mSelectionItems.append( item ); + } + + updateSelectionView( fieldIndex ); +} + +void AddresseeSelector::setItemSelected( uint fieldIndex, const KABC::Addressee &addr, + uint itemIndex, const TQString &text ) +{ + bool found = false; + + SelectionItem::List::Iterator it; + for ( it = mSelectionItems.begin(); it != mSelectionItems.end(); ++it ) { + if ( mSelection->itemEquals( (*it).addressee(), (*it).index(), text ) ) { + (*it).addToField( fieldIndex ); + found = true; + } + } + + if ( !found ) { + SelectionItem item( addr, itemIndex ); + item.addToField( fieldIndex ); + + mSelectionItems.append( item ); + } + + updateSelectionView( fieldIndex ); +} + +void AddresseeSelector::updateSelectionView( int index ) +{ + KListView *view = mSelectionViews[ index ]; + view->clear(); + + SelectionItem::List::Iterator it; + for ( it = mSelectionItems.begin(); it != mSelectionItems.end(); ++it ) { + if ( (*it).isInField( index ) ) + new SelectionViewItem( view, mSelection, &(*it) ); + } +} + +void AddresseeSelector::updateSelectionViews() +{ + for ( uint i = 0; i < mSelection->fieldCount(); ++i ) + updateSelectionView( i ); +} + +void AddresseeSelector::reloadAddressBook() +{ + // load contacts + KABC::Addressee::List list = KABC::StdAddressBook::self( true )->allAddressees(); + KABC::Addressee::List::Iterator it; + + SelectionItem::List selectedItems; + + SelectionItem::List::Iterator itemIt; + for ( itemIt = mSelectionItems.begin(); itemIt != mSelectionItems.end(); ++itemIt ) { + bool isSelected = false; + for ( uint i = 0; i < mSelection->fieldCount(); ++i ) { + if ( (*itemIt).isInField( i ) ) { + isSelected = true; + break; + } + } + + // we don't save distribution lists, since this leads to crashes + if ( isSelected && (*itemIt).distributionList() == 0 ) { + selectedItems.append( *itemIt ); + } + } + + mSelectionItems.clear(); + mSelectionItems = selectedItems; + + for ( it = list.begin(); it != list.end(); ++it ) { + uint itemCount = mSelection->itemCount( *it ); + for ( uint index = 0; index < itemCount; ++index ) { + bool available = false; + for ( itemIt = mSelectionItems.begin(); itemIt != mSelectionItems.end(); ++itemIt ) { + if ( (*itemIt).addressee() == (*it) && (*itemIt).index() == index ) { + available = true; + break; + } + } + + if ( !available ) { + SelectionItem item( *it, index ); + mSelectionItems.append( item ); + } + } + } + + // load distribution lists + delete mManager; + mManager = new KABC::DistributionListManager( KABC::StdAddressBook::self( true ) ); + + mManager->load(); + + TQStringList lists = mManager->listNames(); + + TQStringList::Iterator listIt; + for ( listIt = lists.begin(); listIt != lists.end(); ++listIt ) { + KABC::DistributionList *list = mManager->list( *listIt ); + SelectionItem item( list, 0 ); + mSelectionItems.append( item ); + } + + mAddressBookManager->clear(); + + // update address book combo + mAddressBookCombo->clear(); + + TQPtrList<KABC::Resource> resources = KABC::StdAddressBook::self( true )->resources(); + TQPtrListIterator<KABC::Resource> resIt( resources ); + while ( resIt.current() ) { + if ( resIt.current()->isActive() ) + mAddressBookManager->addResource( resIt ); + + ++resIt; + } + + for ( uint i = 0; i < mSelection->addressBookCount(); ++i ) { + SelectionItem::List itemList; + + KABC::Addressee::List addrList = mSelection->addressBookContent( i ); + for ( it = addrList.begin(); it != addrList.end(); ++it ) { + uint itemCount = mSelection->itemCount( *it ); + for ( uint index = 0; index < itemCount; ++index ) { + SelectionItem item( *it, index ); + mSelectionItems.append( item ); + itemList.append( item ); + } + } + + mAddressBookManager->addAddressBook( mSelection->addressBookTitle( i ), + itemList ); + } + + mAddressBookCombo->insertStringList( mAddressBookManager->titles() ); + + updateAddresseeView(); +} + + +AddresseeSelectorDialog::AddresseeSelectorDialog( Selection *selection, + TQWidget *parent, const char *name ) + : KDialogBase( Plain, "", Ok | Cancel, Ok, parent, name, true ) +{ + TQFrame *frame = plainPage(); + TQVBoxLayout *tqlayout = new TQVBoxLayout( frame ); + mSelector = new KPIM::AddresseeSelector( selection, frame ); + tqlayout->addWidget( mSelector ); + + resize( 500, 490 ); +} + +void AddresseeSelectorDialog::accept() +{ + mSelector->finish(); + TQDialog::accept(); +} + +#include "addresseeselector.moc" diff --git a/libtdepim/addresseeselector.h b/libtdepim/addresseeselector.h new file mode 100644 index 000000000..7ce70fb84 --- /dev/null +++ b/libtdepim/addresseeselector.h @@ -0,0 +1,234 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef KPIM_ADDRESSEESELECTOR_H +#define KPIM_ADDRESSEESELECTOR_H + +#include <kabc/addressee.h> +#include <kabc/distributionlist.h> +#include <kabc/resource.h> +#include <kdialogbase.h> +#include <tdepimmacros.h> + +#include <tqbitarray.h> +#include <tqpixmap.h> +#include <tqwidget.h> + +class KComboBox; +class KLineEdit; +class KListView; +class TQSignalMapper; + +namespace KPIM { + +class AddresseeSelector; + +class KDE_EXPORT Selection +{ + friend class AddresseeSelector; + + public: + virtual ~Selection() {} + + /** + Returns the number of fields the selection offers. + */ + virtual uint fieldCount() const = 0; + + /** + Returns the title for the field specified by index. + */ + virtual TQString fieldTitle( uint index ) const = 0; + + /** + Returns the number of items for the given addressee. + */ + virtual uint itemCount( const KABC::Addressee &addresse ) const = 0; + + /** + Returns the text that's used for the item specified by index. + */ + virtual TQString itemText( const KABC::Addressee &addresse, uint index ) const = 0; + + /** + Returns the icon that's used for the item specified by index. + */ + virtual TQPixmap itemIcon( const KABC::Addressee &addresse, uint index ) const = 0; + + /** + Returns whether the item specified by index is enabled. + */ + virtual bool itemEnabled( const KABC::Addressee &addresse, uint index ) const = 0; + + /** + Returns whether the item specified by index matches the passed pattern. + */ + virtual bool itemMatches( const KABC::Addressee &addresse, uint index, const TQString &pattern ) const = 0; + + /** + Returns whether the item specified by index equals the passed pattern. + */ + virtual bool itemEquals( const KABC::Addressee &addresse, uint index, const TQString &pattern ) const = 0; + + /** + Returns the text that's used for the given distribution list. + */ + virtual TQString distributionListText( const KABC::DistributionList *distributionList ) const = 0; + + /** + Returns the icon that's used for the given distribution list. + */ + virtual TQPixmap distributionListIcon( const KABC::DistributionList *distributionList ) const = 0; + + /** + Returns whether the given distribution list is enabled. + */ + virtual bool distributionListEnabled( const KABC::DistributionList *distributionList ) const = 0; + + /** + Returns whether the given distribution list matches the passed pattern. + */ + virtual bool distributionListMatches( const KABC::DistributionList *distributionList, + const TQString &pattern ) const = 0; + + /** + Returns the number of additional address books. + */ + virtual uint addressBookCount() const = 0; + + /** + Returns the title for an additional address book. + */ + virtual TQString addressBookTitle( uint index ) const = 0; + + /** + Returns the content for an additional address book. + */ + virtual KABC::Addressee::List addressBookContent( uint index ) const = 0; + + protected: + AddresseeSelector* selector() { return mSelector; } + + private: + virtual void addSelectedAddressees( uint fieldIndex, const KABC::Addressee&, uint itemIndex ) = 0; + virtual void addSelectedDistributionList( uint fieldIndex, const KABC::DistributionList* ) = 0; + + void setSelector( AddresseeSelector *selector ) { mSelector = selector; } + + AddresseeSelector *mSelector; +}; + +/** + Internal helper class + */ +class SelectionItem +{ + public: + typedef TQValueList<SelectionItem> List; + + SelectionItem( const KABC::Addressee &addressee, uint index ); + SelectionItem( KABC::DistributionList *list, uint index ); + SelectionItem(); + + void addToField( int index ); + void removeFromField( int index ); + bool isInField( int index ); + + KABC::Addressee addressee() const; + KABC::DistributionList* distributionList() const; + uint index() const; + + private: + KABC::Addressee mAddressee; + KABC::DistributionList *mDistributionList; + uint mIndex; + TQBitArray mField; +}; + +class KDE_EXPORT AddresseeSelector : public TQWidget +{ + Q_OBJECT + TQ_OBJECT + + public: + AddresseeSelector( Selection *selection, + TQWidget *parent, const char *name = 0 ); + ~AddresseeSelector(); + + /** + Writes back the selected items to the selection. + */ + void finish(); + + void setItemSelected( uint fieldIndex, const KABC::Addressee&, uint itemIndex ); + void setItemSelected( uint fieldIndex, const KABC::Addressee&, + uint itemIndex, const TQString& ); + + private slots: + void move( int index ); + void remove( int index ); + + void updateAddresseeView(); + void reloadAddressBook(); + + private: + void init(); + void initGUI(); + + void updateSelectionView( int index ); + void updateSelectionViews(); + + Selection *mSelection; + + KComboBox *mAddressBookCombo; + KLineEdit *mAddresseeFilter; + KListView *mAddresseeView; + SelectionItem::List mSelectionItems; + + TQValueList<KListView*> mSelectionViews; + TQSignalMapper *mMoveMapper; + TQSignalMapper *mRemoveMapper; + + KABC::DistributionListManager *mManager; + + class AddressBookManager; + AddressBookManager *mAddressBookManager; +}; + +class KDE_EXPORT AddresseeSelectorDialog : public KDialogBase +{ + Q_OBJECT + TQ_OBJECT + + public: + AddresseeSelectorDialog( Selection *selection, + TQWidget *parent = 0, const char *name = 0 ); + + protected slots: + void accept(); + + private: + AddresseeSelector *mSelector; +}; + +} + +#endif diff --git a/libtdepim/addresseeview.cpp b/libtdepim/addresseeview.cpp new file mode 100644 index 000000000..15c7215ed --- /dev/null +++ b/libtdepim/addresseeview.cpp @@ -0,0 +1,792 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2003 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <tqbuffer.h> +#include <tqimage.h> +#include <tqpopupmenu.h> +#include <tqurl.h> + +#include <kabc/address.h> +#include <kabc/addressee.h> +#include <kabc/phonenumber.h> +#include <kabc/resource.h> +#include <kactionclasses.h> +#include <kapplication.h> +#include <kconfig.h> +#include <kglobal.h> +#include <kglobalsettings.h> +#include <kiconloader.h> +#include <kio/job.h> +#include <klocale.h> +#include <kmdcodec.h> +#include <kmessagebox.h> +#include <krun.h> +#include <kstringhandler.h> +#include <ktempfile.h> + +#include <kdebug.h> + +#include "addresseeview.h" +#include "sendsmsdialog.h" +#include "resourceabc.h" + +using namespace KPIM; + +AddresseeView::AddresseeView( TQWidget *parent, const char *name, + KConfig *config ) + : KTextBrowser( parent, name ), mDefaultConfig( false ), mImageJob( 0 ), + mLinkMask( AddressLinks | EmailLinks | PhoneLinks | URLLinks | IMLinks | CustomFields ) +{ + setWrapPolicy( TQTextEdit::AtWordBoundary ); + setLinkUnderline( false ); + setVScrollBarMode( TQScrollView::AlwaysOff ); + setHScrollBarMode( TQScrollView::AlwaysOff ); + + TQStyleSheet *sheet = styleSheet(); + TQStyleSheetItem *link = sheet->item( "a" ); + link->setColor( KGlobalSettings::linkColor() ); + + connect( this, TQT_SIGNAL( mailClick( const TQString&, const TQString& ) ), + this, TQT_SLOT( slotMailClicked( const TQString&, const TQString& ) ) ); + connect( this, TQT_SIGNAL( urlClick( const TQString& ) ), + this, TQT_SLOT( slotUrlClicked( const TQString& ) ) ); + connect( this, TQT_SIGNAL( highlighted( const TQString& ) ), + this, TQT_SLOT( slotHighlighted( const TQString& ) ) ); + + setNotifyClick( true ); + + mActionShowBirthday = new KToggleAction( i18n( "Show Birthday" ) ); + mActionShowBirthday->setCheckedState( i18n( "Hide Birthday" ) ); + mActionShowAddresses = new KToggleAction( i18n( "Show Postal Addresses" ) ); + mActionShowAddresses->setCheckedState( i18n( "Hide Postal Addresses" ) ); + mActionShowEmails = new KToggleAction( i18n( "Show Email Addresses" ) ); + mActionShowEmails->setCheckedState( i18n( "Hide Email Addresses" ) ); + mActionShowPhones = new KToggleAction( i18n( "Show Telephone Numbers" ) ); + mActionShowPhones->setCheckedState( i18n( "Hide Telephone Numbers" ) ); + mActionShowURLs = new KToggleAction( i18n( "Show Web Pages (URLs)" ) ); + mActionShowURLs->setCheckedState( i18n( "Hide Web Pages (URLs)" ) ); + mActionShowIMAddresses = new KToggleAction( i18n( "Show Instant Messaging Addresses" ) ); + mActionShowIMAddresses->setCheckedState( i18n( "Hide Instant Messaging Addresses" ) ); + mActionShowCustomFields = new KToggleAction( i18n( "Show Custom Fields" ) ); + mActionShowCustomFields->setCheckedState( i18n( "Hide Custom Fields" ) ); + + connect( mActionShowBirthday, TQT_SIGNAL( toggled( bool ) ), TQT_SLOT( configChanged() ) ); + connect( mActionShowAddresses, TQT_SIGNAL( toggled( bool ) ), TQT_SLOT( configChanged() ) ); + connect( mActionShowEmails, TQT_SIGNAL( toggled( bool ) ), TQT_SLOT( configChanged() ) ); + connect( mActionShowPhones, TQT_SIGNAL( toggled( bool ) ), TQT_SLOT( configChanged() ) ); + connect( mActionShowURLs, TQT_SIGNAL( toggled( bool ) ), TQT_SLOT( configChanged() ) ); + connect( mActionShowIMAddresses, TQT_SIGNAL( toggled( bool ) ), TQT_SLOT( configChanged() ) ); + connect( mActionShowCustomFields, TQT_SIGNAL( toggled( bool ) ), TQT_SLOT( configChanged() ) ); + + if ( !config ) { + mConfig = new KConfig( "kaddressbookrc" ); + mDefaultConfig = true; + } else + mConfig = config; + + load(); + + // set up IMProxy to display contacts' IM presence and make connections to keep the display live + mKIMProxy = ::KIMProxy::instance( kapp->dcopClient() ); + connect( mKIMProxy, TQT_SIGNAL( sigContactPresenceChanged( const TQString& ) ), + this, TQT_SLOT( slotPresenceChanged( const TQString& ) ) ); + connect( mKIMProxy, TQT_SIGNAL( sigPresenceInfoExpired() ), + this, TQT_SLOT( slotPresenceInfoExpired() ) ); +} + +AddresseeView::~AddresseeView() +{ + if ( mDefaultConfig ) + delete mConfig; + mConfig = 0; + + delete mActionShowBirthday; + delete mActionShowAddresses; + delete mActionShowEmails; + delete mActionShowPhones; + delete mActionShowURLs; + delete mActionShowIMAddresses; + delete mActionShowCustomFields; + + mKIMProxy = 0; +} + +void AddresseeView::setAddressee( const KABC::Addressee& addr ) +{ + mAddressee = addr; + + if ( mImageJob ) { + mImageJob->kill(); + mImageJob = 0; + } + + mImageData.truncate( 0 ); + + updateView(); +} + +void AddresseeView::enableLinks( int linkMask ) +{ + mLinkMask = linkMask; +} + +TQString AddresseeView::vCardAsHTML( const KABC::Addressee& addr, ::KIMProxy *proxy, LinkMask linkMask, + bool internalLoading, FieldMask fieldMask ) +{ + TQString image = TQString( "contact_%1_image" ).tqarg( addr.uid() ); + + // Style strings from Gentix; this is just an initial version. + // + // These will be substituted into various HTML strings with .tqarg(). + // Search for @STYLE@ to find where. Note how we use %1 as a + // placeholder where we fill in something else (in this case, + // the global background color). + // + TQString backgroundColor = KGlobalSettings::alternateBackgroundColor().name(); + TQString cellStyle = TQString::tqfromLatin1( + "style=\"" + "padding-right: 2px; " + "border-right: #000 dashed 1px; " + "background: %1;\"").tqarg(backgroundColor); + TQString backgroundColor2 = KGlobalSettings::baseColor().name(); + TQString cellStyle2 = TQString::tqfromLatin1( + "style=\"" + "padding-left: 2px; " + "background: %1;\"").tqarg(backgroundColor2); + TQString tableStyle = TQString::tqfromLatin1( + "style=\"" + "border: solid 1px; " + "margin: 0em;\""); + + // We'll be building a table to display the vCard in. + // Each row of the table will be built using this string for its HTML. + // + TQString rowFmtStr = TQString::tqfromLatin1( + "<tr>" + "<td align=\"right\" valign=\"top\" width=\"30%\" "); // Tag unclosed + rowFmtStr.append( cellStyle ); + rowFmtStr.append( TQString::tqfromLatin1( + ">" // Close tag + "<b>%1</b>" + "</td>" + "<td align=\"left\" valign=\"top\" width=\"70%\" ") ); // Tag unclosed + rowFmtStr.append( cellStyle2 ); + rowFmtStr.append( TQString::tqfromLatin1( + ">" // Close tag + "%2" + "</td>" + "</tr>\n" + ) ); + + // Build the table's rows here + TQString dynamicPart; + + + if ( !internalLoading ) { + KABC::Picture pic = addr.photo(); + if ( pic.isIntern() && !pic.data().isNull() ) { + image = pixmapAsDataUrl( pic.data() ); + } else if ( !pic.url().isEmpty() ) { + image = (pic.url().startsWith( "http://" ) || pic.url().startsWith( "https://" ) ? pic.url() : "http://" + pic.url()); + } else { + image = "file:" + KGlobal::iconLoader()->iconPath( "personal", KIcon::Desktop ); + } + } + + if ( fieldMask & BirthdayFields ) { + TQDate date = addr.birthday().date(); + + if ( date.isValid() ) + dynamicPart += rowFmtStr + .tqarg( KABC::Addressee::birthdayLabel() ) + .tqarg( KGlobal::locale()->formatDate( date, true ) ); + } + + if ( fieldMask & PhoneFields ) { + KABC::PhoneNumber::List phones = addr.phoneNumbers(); + KABC::PhoneNumber::List::ConstIterator phoneIt; + for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt ) { + TQString number = TQStyleSheet::escape( (*phoneIt).number() ); + + TQString url; + if ( (*phoneIt).type() & KABC::PhoneNumber::Fax ) + url = TQString::tqfromLatin1( "fax:" ) + number; + else + url = TQString::tqfromLatin1( "phone:" ) + number; + + if ( linkMask & PhoneLinks ) { + TQString smsURL; + if ( (*phoneIt).type() & KABC::PhoneNumber::Cell ) + smsURL = TQString(" (<a href=\"sms:%1\">%2</a>)" ).tqarg( number ).tqarg( i18n( "SMS") ); + + dynamicPart += rowFmtStr + .tqarg( (*phoneIt).typeLabel().replace( " ", " " ) ) + .tqarg( TQString::tqfromLatin1( "<a href=\"%1\">%2</a>%3" ).tqarg( url ).tqarg( number ).tqarg( smsURL ) ); + } else { + dynamicPart += rowFmtStr + .tqarg( (*phoneIt).typeLabel().replace( " ", " " ) ) + .tqarg( number ); + } + } + } + + if ( fieldMask & EmailFields ) { + TQStringList emails = addr.emails(); + TQStringList::ConstIterator emailIt; + TQString type = i18n( "Email" ); + for ( emailIt = emails.begin(); emailIt != emails.end(); ++emailIt ) { + TQString fullEmail = addr.fullEmail( *emailIt ); + TQUrl::encode( fullEmail ); + + if ( linkMask & EmailLinks ) { + dynamicPart += rowFmtStr.tqarg( type ) + .tqarg( TQString::tqfromLatin1( "<a href=\"mailto:%1\">%2</a>" ) + .tqarg( fullEmail, TQStyleSheet::escape( *emailIt ) ) ); + } else { + dynamicPart += rowFmtStr.tqarg( type ).tqarg( *emailIt ); + } + } + } + + if ( fieldMask & URLFields ) { + if ( !addr.url().url().isEmpty() ) { + TQString url; + if ( linkMask & URLLinks ) { + url = (addr.url().url().startsWith( "http://" ) || addr.url().url().startsWith( "https://" ) ? addr.url().prettyURL() : + "http://" + addr.url().prettyURL()); + url = KStringHandler::tagURLs( url ); + } else { + url = addr.url().prettyURL(); + } + dynamicPart += rowFmtStr.tqarg( i18n("Homepage") ).tqarg( url ); + } + + TQString blog = addr.custom( "KADDRESSBOOK", "BlogFeed" ); + if ( !blog.isEmpty() ) { + if ( linkMask & URLLinks ) { + blog = KStringHandler::tagURLs( blog ); + } + dynamicPart += rowFmtStr.tqarg( i18n("Blog Feed") ).tqarg( blog ); + } + } + + if ( fieldMask & AddressFields ) { + KABC::Address::List addresses = addr.addresses(); + KABC::Address::List::ConstIterator addrIt; + for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) { + if ( (*addrIt).label().isEmpty() ) { + TQString formattedAddress; + + formattedAddress = TQStyleSheet::escape( (*addrIt).formattedAddress().stripWhiteSpace() ); + formattedAddress = formattedAddress.replace( '\n', "<br>" ); + + TQString link = "<a href=\"addr:" + (*addrIt).id() + "\">" + + formattedAddress + "</a>"; + + if ( linkMask & AddressLinks ) { + dynamicPart += rowFmtStr + .tqarg( KABC::Address::typeLabel( (*addrIt).type() ) ) + .tqarg( link ); + } else { + dynamicPart += rowFmtStr + .tqarg( KABC::Address::typeLabel( (*addrIt).type() ) ) + .tqarg( formattedAddress ); + } + } else { + TQString link = "<a href=\"addr:" + (*addrIt).id() + "\">" + + (*addrIt).label().replace( '\n', "<br>" ) + "</a>"; + + if ( linkMask & AddressLinks ) { + dynamicPart += rowFmtStr + .tqarg( KABC::Address::typeLabel( (*addrIt).type() ) ) + .tqarg( link ); + } else { + dynamicPart += rowFmtStr + .tqarg( KABC::Address::typeLabel( (*addrIt).type() ) ) + .tqarg( (*addrIt).label().replace( '\n', "<br>" ) ); + } + } + } + } + + TQString notes; + if ( !addr.note().isEmpty() ) { + // @STYLE@ - substitute the cell style in first, and append + // the data afterwards (keeps us safe from possible % signs + // in either one). + notes = TQStyleSheet::escape( addr.note() ); + notes = rowFmtStr.tqarg( i18n( "Notes" ) ).tqarg( notes.replace( '\n', "<br>" ) ) ; + } + + TQString customData; + if ( fieldMask & CustomFields ) { + static TQMap<TQString, TQString> titleMap; + if ( titleMap.isEmpty() ) { + titleMap.insert( "Department", i18n( "Department" ) ); + titleMap.insert( "Profession", i18n( "Profession" ) ); + titleMap.insert( "AssistantsName", i18n( "Assistant's Name" ) ); + titleMap.insert( "ManagersName", i18n( "Manager's Name" ) ); + titleMap.insert( "SpousesName", i18n( "Partner's Name" ) ); + titleMap.insert( "Office", i18n( "Office" ) ); + titleMap.insert( "Anniversary", i18n( "Anniversary" ) ); + } + + if ( !addr.customs().empty() ) { + TQStringList customs = addr.customs(); + TQStringList::Iterator it( customs.begin() ); + const TQStringList::Iterator endIt( customs.end() ); + for ( ; it != endIt; ++it ) { + TQString customEntry = *it; + if ( customEntry.startsWith ( "KADDRESSBOOK-" ) ) { + customEntry.remove( "KADDRESSBOOK-X-" ); + customEntry.remove( "KADDRESSBOOK-" ); + + int pos = customEntry.find( ':' ); + TQString key = customEntry.left( pos ); + const TQString value = customEntry.mid( pos + 1 ); + + // blog and im address is handled separated + if ( key == "BlogFeed" || key == "IMAddress" ) + continue; + + const TQMap<TQString, TQString>::ConstIterator keyIt = titleMap.find( key ); + if ( keyIt != titleMap.end() ) + key = keyIt.data(); + + customData += rowFmtStr.tqarg( key ).tqarg( TQStyleSheet::escape( value ) ) ; + } + } + } + } + + TQString name( TQStyleSheet::escape( addr.realName() ) ); + TQString role( TQStyleSheet::escape( addr.role() ) ); + TQString organization( TQStyleSheet::escape( addr.organization() ) ); + + if ( fieldMask & IMFields ) { + + const TQString imAddress = addr.custom( "KADDRESSBOOK", "X-IMAddress" ); + if ( !imAddress.isEmpty() ) { + customData += rowFmtStr.tqarg( i18n( "IM Address" ) ).tqarg( TQStyleSheet::escape( imAddress ) ) ; + } + + if ( proxy ) { + if ( proxy->isPresent( addr.uid() ) && proxy->presenceNumeric( addr.uid() ) > 0 ) { + // set image source to either a TQMimeSourceFactory key or a data:/ URL + TQString imgSrc; + if ( internalLoading ) { + imgSrc = TQString::tqfromLatin1( "im_status_%1_image").tqarg( addr.uid() ); + TQMimeSourceFactory::defaultFactory()->setPixmap( imgSrc, proxy->presenceIcon( addr.uid() ) ); + } else + imgSrc = pixmapAsDataUrl( proxy->presenceIcon( addr.uid() ) ); + + // make the status a link, if required + TQString imtqStatus; + if ( linkMask & IMLinks ) + imtqStatus = TQString::tqfromLatin1( "<a href=\"im:\"><img src=\"%1\"> (%2)</a>" ); + else + imtqStatus = TQString::tqfromLatin1( "<img src=\"%1\"> (%2)" ); + + // append our status to the rest of the dynamic part of the addressee + dynamicPart += rowFmtStr + .tqarg( i18n( "Presence" ) ) + .tqarg( imtqStatus + .tqarg( imgSrc ) + .tqarg( proxy->presenceString( addr.uid() ) ) + ); + } + } + } + + // @STYLE@ - construct the string by parts, substituting in + // the styles first. There are lots of appends, but we need to + // do it this way to avoid cases where the substituted string + // contains %1 and the like. + // + TQString strAddr = TQString::tqfromLatin1( + "<div align=\"center\">" + "<table cellpadding=\"1\" cellspacing=\"0\" %1>" + "<tr>").tqarg(tableStyle); + + strAddr.append( TQString::tqfromLatin1( + "<td align=\"right\" valign=\"top\" width=\"30%\" rowspan=\"3\" %2>") + .tqarg( cellStyle ) ); + strAddr.append( TQString::tqfromLatin1( + "<img src=\"%1\" width=\"50\" vspace=\"1\">" // image + "</td>") + .tqarg( image ) ); + strAddr.append( TQString::tqfromLatin1( + "<td align=\"left\" width=\"70%\" %2>") + .tqarg( cellStyle2 ) ); + strAddr.append( TQString::tqfromLatin1( + "<font size=\"+2\"><b>%2</b></font></td>" // name + "</tr>") + .tqarg( name ) ); + strAddr.append( TQString::tqfromLatin1( + "<tr>" + "<td align=\"left\" width=\"70%\" %2>") + .tqarg( cellStyle2 ) ); + strAddr.append( TQString::tqfromLatin1( + "%3</td>" // role + "</tr>") + .tqarg( role ) ); + strAddr.append( TQString::tqfromLatin1( + "<tr>" + "<td align=\"left\" width=\"70%\" %2>") + .tqarg( cellStyle2 ) ); + strAddr.append( TQString::tqfromLatin1( + "%4</td>" // organization + "</tr>") + .tqarg( organization ) ); + strAddr.append( TQString::tqfromLatin1( + "<tr><td %2>") + .tqarg( cellStyle ) ); + strAddr.append( TQString::tqfromLatin1( + " </td><td %2> </td></tr>") + .tqarg( cellStyle2 ) ); + strAddr.append( dynamicPart ); + strAddr.append( notes ); + strAddr.append( customData ); + strAddr.append( TQString::tqfromLatin1( "</table></div>\n" ) ); + + if ( addr.resource() ) { + TQString addrBookName = addr.resource()->resourceName(); + ResourceABC *r = dynamic_cast<ResourceABC*>( addr.resource() ); + if ( r && !r->subresources().isEmpty() ) { + const TQString subRes = r->uidToResourceMap()[ addr.uid() ]; + const TQString label = r->subresourceLabel( subRes ); + if ( !label.isEmpty() ) + addrBookName = label; + } + strAddr.append( i18n( "<p><b>Address book</b>: %1</p>" ).tqarg( addrBookName ) ); + } + return strAddr; +} + +TQString AddresseeView::pixmapAsDataUrl( const TQPixmap& pixmap ) +{ + TQByteArray ba; + TQBuffer buffer( ba ); + buffer.open( IO_WriteOnly ); + pixmap.save( &buffer, "PNG" ); + TQString encoded( "data:image/png;base64," ); + encoded.append( KCodecs::base64Encode( ba ) ); + return encoded; +} + +void AddresseeView::updateView() +{ + // clear view + setText( TQString() ); + + if ( mAddressee.isEmpty() ) + return; + + if ( mImageJob ) { + mImageJob->kill(); + mImageJob = 0; + + mImageData.truncate( 0 ); + } + + int fieldMask = NoFields; + if ( mActionShowBirthday->isChecked() ) + fieldMask |= ( FieldMask )BirthdayFields; + if ( mActionShowAddresses->isChecked() ) + fieldMask |= AddressFields; + if ( mActionShowEmails->isChecked() ) + fieldMask |= EmailFields; + if ( mActionShowPhones->isChecked() ) + fieldMask |= PhoneFields; + if ( mActionShowURLs->isChecked() ) + fieldMask |= URLFields; + if ( mActionShowIMAddresses->isChecked() ) + fieldMask |= IMFields; + if ( mActionShowCustomFields->isChecked() ) + fieldMask |= CustomFields; + + TQString strAddr = vCardAsHTML( mAddressee, mKIMProxy, (LinkMask)mLinkMask, + true, (FieldMask)fieldMask ); + + strAddr = TQString::tqfromLatin1( + "<html>" + "<body text=\"%1\" bgcolor=\"%2\">" // text and background color + "%3" // dynamic part + "</body>" + "</html>" ) + .tqarg( KGlobalSettings::textColor().name() ) + .tqarg( KGlobalSettings::baseColor().name() ) + .tqarg( strAddr ); + + TQString imageURL = TQString( "contact_%1_image" ).tqarg( mAddressee.uid() ); + + KABC::Picture picture = mAddressee.photo(); + if ( picture.isIntern() && !picture.data().isNull() ) + TQMimeSourceFactory::defaultFactory()->setImage( imageURL, picture.data() ); + else { + if ( !picture.url().isEmpty() ) { + if ( mImageData.count() > 0 ) + TQMimeSourceFactory::defaultFactory()->setImage( imageURL, TQImage(mImageData) ); + else { + mImageJob = KIO::get( KURL( picture.url() ), false, false ); + connect( mImageJob, TQT_SIGNAL( data( KIO::Job*, const TQByteArray& ) ), + this, TQT_SLOT( data( KIO::Job*, const TQByteArray& ) ) ); + connect( mImageJob, TQT_SIGNAL( result( KIO::Job* ) ), + this, TQT_SLOT( result( KIO::Job* ) ) ); + } + } else { + TQMimeSourceFactory::defaultFactory()->setPixmap( imageURL, + KGlobal::iconLoader()->loadIcon( "personal", KIcon::Desktop, 128 ) ); + } + } + + // at last display it... + setText( strAddr ); +} + +KABC::Addressee AddresseeView::addressee() const +{ + return mAddressee; +} + +void AddresseeView::urlClicked( const TQString &url ) +{ + kapp->invokeBrowser( url ); +} + +void AddresseeView::emailClicked( const TQString &email ) +{ + if ( email.startsWith( "mailto:" ) ) + kapp->invokeMailer( email.mid( 7 ), TQString() ); + else + kapp->invokeMailer( email, TQString() ); +} + +void AddresseeView::phoneNumberClicked( const TQString &number ) +{ + KConfig config( "kaddressbookrc" ); + config.setGroup( "General" ); + TQString commandLine = config.readEntry( "PhoneHookApplication" ); + + if ( commandLine.isEmpty() ) { + KMessageBox::sorry( this, i18n( "There is no application set which could be executed. Please go to the settings dialog and configure one." ) ); + return; + } + + commandLine.replace( "%N", number ); + KRun::runCommand( commandLine ); +} + +void AddresseeView::smsTextClicked( const TQString &number ) +{ + KConfig config( "kaddressbookrc" ); + config.setGroup( "General" ); + TQString commandLine = config.readEntry( "SMSHookApplication" ); + + if ( commandLine.isEmpty() ) { + KMessageBox::sorry( this, i18n( "There is no application set which could be executed. Please go to the settings dialog and configure one." ) ); + return; + } + + SendSMSDialog dlg( mAddressee.realName(), this ); + + if ( dlg.exec() ) + sendSMS ( number, dlg.text() ); +} + +void AddresseeView::sendSMS( const TQString &number, const TQString &text ) +{ + KConfig config( "kaddressbookrc" ); + config.setGroup( "General" ); + TQString commandLine = config.readEntry( "SMSHookApplication" ); + + KTempFile file ; + TQTextStream* stream = file.textStream(); + *stream << text; + file.close(); + + commandLine.replace( "%N", number ); + commandLine.replace( "%F", file.name() ); + + KRun::runCommand( commandLine ); +} + +void AddresseeView::faxNumberClicked( const TQString &number ) +{ + KConfig config( "kaddressbookrc" ); + config.setGroup( "General" ); + TQString commandLine = config.readEntry( "FaxHookApplication", "tdeprintfax --phone %N" ); + + if ( commandLine.isEmpty() ) { + KMessageBox::sorry( this, i18n( "There is no application set which could be executed. Please go to the settings dialog and configure one." ) ); + return; + } + + commandLine.replace( "%N", number ); + KRun::runCommand( commandLine ); +} + +void AddresseeView::imAddressClicked() +{ + mKIMProxy->chatWithContact( mAddressee.uid() ); +} + +TQPopupMenu *AddresseeView::createPopupMenu( const TQPoint& ) +{ + TQPopupMenu *menu = new TQPopupMenu( this ); + mActionShowBirthday->plug( menu ); + mActionShowAddresses->plug( menu ); + mActionShowEmails->plug( menu ); + mActionShowPhones->plug( menu ); + mActionShowURLs->plug( menu ); + mActionShowIMAddresses->plug( menu ); + mActionShowCustomFields->plug( menu ); + + return menu; +} + +void AddresseeView::slotMailClicked( const TQString&, const TQString &email ) +{ + emailClicked( email ); +} + +void AddresseeView::slotUrlClicked( const TQString &url ) +{ + if ( url.startsWith( "phone:" ) ) + phoneNumberClicked( strippedNumber( url.mid( 8 ) ) ); + else if ( url.startsWith( "sms:" ) ) + smsTextClicked( strippedNumber( url.mid( 6 ) ) ); + else if ( url.startsWith( "fax:" ) ) + faxNumberClicked( strippedNumber( url.mid( 6 ) ) ); + else if ( url.startsWith( "addr:" ) ) + emit addressClicked( url.mid( 7 ) ); + else if ( url.startsWith( "im:" ) ) + imAddressClicked(); + else + urlClicked( url ); +} + +void AddresseeView::slotHighlighted( const TQString &link ) +{ + if ( link.startsWith( "mailto:" ) ) { + TQString email = link.mid( 7 ); + + emit emailHighlighted( email ); + emit highlightedMessage( i18n( "Send mail to '%1'" ).tqarg( email ) ); + } else if ( link.startsWith( "phone:" ) ) { + TQString number = link.mid( 8 ); + + emit phoneNumberHighlighted( strippedNumber( number ) ); + emit highlightedMessage( i18n( "Call number %1" ).tqarg( number ) ); + } else if ( link.startsWith( "fax:" ) ) { + TQString number = link.mid( 6 ); + + emit faxNumberHighlighted( strippedNumber( number ) ); + emit highlightedMessage( i18n( "Send fax to %1" ).tqarg( number ) ); + } else if ( link.startsWith( "addr:" ) ) { + emit highlightedMessage( i18n( "Show address on map" ) ); + } else if ( link.startsWith( "sms:" ) ) { + TQString number = link.mid( 6 ); + emit highlightedMessage( i18n( "Send SMS to %1" ).tqarg( number ) ); + } else if ( link.startsWith( "http:" ) || link.startsWith( "https:" ) ) { + emit urlHighlighted( link ); + emit highlightedMessage( i18n( "Open URL %1" ).tqarg( link ) ); + } else if ( link.startsWith( "im:" ) ) { + emit highlightedMessage( i18n( "Chat with %1" ).tqarg( mAddressee.realName() ) ); + } else + emit highlightedMessage( "" ); +} + +void AddresseeView::slotPresenceChanged( const TQString &uid ) +{ + kdDebug() << k_funcinfo << " uid is: " << uid << " mAddressee is: " << mAddressee.uid() << endl; + if ( uid == mAddressee.uid() ) + updateView(); +} + + +void AddresseeView::slotPresenceInfoExpired() +{ + updateView(); +} + +void AddresseeView::configChanged() +{ + save(); + updateView(); +} + +void AddresseeView::data( KIO::Job*, const TQByteArray &d ) +{ + unsigned int oldSize = mImageData.size(); + mImageData.resize( oldSize + d.size() ); + memcpy( mImageData.data() + oldSize, d.data(), d.size() ); +} + +void AddresseeView::result( KIO::Job *job ) +{ + mImageJob = 0; + + if ( job->error() ) + mImageData.truncate( 0 ); + else + updateView(); +} + +void AddresseeView::load() +{ + mConfig->setGroup( "AddresseeViewSettings" ); + mActionShowBirthday->setChecked( mConfig->readBoolEntry( "ShowBirthday", false ) ); + mActionShowAddresses->setChecked( mConfig->readBoolEntry( "ShowAddresses", true ) ); + mActionShowEmails->setChecked( mConfig->readBoolEntry( "ShowEmails", true ) ); + mActionShowPhones->setChecked( mConfig->readBoolEntry( "ShowPhones", true ) ); + mActionShowURLs->setChecked( mConfig->readBoolEntry( "ShowURLs", true ) ); + mActionShowIMAddresses->setChecked( mConfig->readBoolEntry( "ShowIMAddresses", false ) ); + mActionShowCustomFields->setChecked( mConfig->readBoolEntry( "ShowCustomFields", false ) ); +} + +void AddresseeView::save() +{ + mConfig->setGroup( "AddresseeViewSettings" ); + mConfig->writeEntry( "ShowBirthday", mActionShowBirthday->isChecked() ); + mConfig->writeEntry( "ShowAddresses", mActionShowAddresses->isChecked() ); + mConfig->writeEntry( "ShowEmails", mActionShowEmails->isChecked() ); + mConfig->writeEntry( "ShowPhones", mActionShowPhones->isChecked() ); + mConfig->writeEntry( "ShowURLs", mActionShowURLs->isChecked() ); + mConfig->writeEntry( "ShowIMAddresses", mActionShowIMAddresses->isChecked() ); + mConfig->writeEntry( "ShowCustomFields", mActionShowCustomFields->isChecked() ); + mConfig->sync(); +} + +TQString AddresseeView::strippedNumber( const TQString &number ) +{ + TQString retval; + + for ( uint i = 0; i < number.length(); ++i ) { + TQChar c = number[ i ]; + if ( c.isDigit() || (((c == '*') || (c == '#') || (c == '+')) && (i == 0)) ) + retval.append( c ); + } + + return retval; +} + +#include "addresseeview.moc" diff --git a/libtdepim/addresseeview.h b/libtdepim/addresseeview.h new file mode 100644 index 000000000..0b7c13b05 --- /dev/null +++ b/libtdepim/addresseeview.h @@ -0,0 +1,205 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2003 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef KPIM_ADDRESSEEVIEW_H +#define KPIM_ADDRESSEEVIEW_H + +#include <tqcstring.h> + +#include <kabc/addressee.h> +#include <ktextbrowser.h> +#include <kimproxy.h> +#include <tdepimmacros.h> + +namespace KIO { +class Job; +} +class KToggleAction; + +class TQPopupMenu; + + +namespace KPIM { + + +class KDE_EXPORT AddresseeView : public KTextBrowser +{ + Q_OBJECT + TQ_OBJECT + public: + /** + Constructor. + + @param config The config object where the settings are stored + which fields will be shown. + */ + AddresseeView( TQWidget *parent = 0, const char *name = 0, + KConfig *config = 0 ); + + ~AddresseeView(); + + /** + Sets the addressee object. The addressee is displayed immediately. + + @param addr The addressee object. + */ + void setAddressee( const KABC::Addressee& addr ); + + /** + Returns the current addressee object. + */ + KABC::Addressee addressee() const; + + + /** + This enums are used by enableLinks to set which kind of links shall + be enabled. + */ + enum LinkMask { + NoLinks = 0, + AddressLinks = 1, + EmailLinks = 2, + PhoneLinks = 4, + URLLinks = 8, + IMLinks = 16, + DefaultLinks = AddressLinks | EmailLinks | PhoneLinks | URLLinks | IMLinks + }; + + /** + Sets which parts of the contact shall be presented as links. + The mask can be OR'ed LinkMask. By default all links are enabled. + */ + void enableLinks( int linkMask ); + + /** + This enums are used by vCardAsHTML to decide which fields shall be + shown. + */ + enum FieldMask { + NoFields = 0, + BirthdayFields = 1, + AddressFields = 2, + EmailFields = 4, + PhoneFields = 8, + URLFields = 16, + IMFields = 32, + CustomFields = 64, + DefaultFields = AddressFields | EmailFields | PhoneFields | URLFields + }; + + /** + Returns the HTML representation of a contact. + The HTML code looks like + <div> + <table> + ... + </table> + </div> + + @param addr The addressee object. + @param linkMask The mask for which parts of the contact will + be displayed as links. + The links looks like this: + "addr://<addr id>" for addresses + "mailto:<email address>" for emails + "phone://<phone number>" for phone numbers + "http://<url>" for urls + "im:<im addrss>" for instant messaging addresses + "sms://<phone number>" for sending a sms + @param internalLoading If true, the loading of internal pictures is done automatically. + @param fieldMask The mask for which fields of the contact will + be displayed. + */ + static TQString vCardAsHTML( const KABC::Addressee& addr, ::KIMProxy *proxy, LinkMask linkMask = DefaultLinks, + bool internalLoading = true, FieldMask fieldMask = DefaultFields ); + + /** + * Encodes a TQPixmap as a PNG into a data: URL (rfc2397), readable by the data kio protocol + * @param pixmap the pixmap to encode + * @return a data: URL + */ + static TQString pixmapAsDataUrl( const TQPixmap& pixmap ); + + signals: + void urlHighlighted( const TQString &url ); + void emailHighlighted( const TQString &email ); + void phoneNumberHighlighted( const TQString &number ); + void faxNumberHighlighted( const TQString &number ); + + void highlightedMessage( const TQString &message ); + + void addressClicked( const TQString &uid ); + + protected: + virtual void urlClicked( const TQString &url ); + virtual void emailClicked( const TQString &mail ); + virtual void phoneNumberClicked( const TQString &number ); + virtual void smsTextClicked( const TQString &number ); + virtual void sendSMS( const TQString &number, const TQString &msg ); + virtual void faxNumberClicked( const TQString &number ); + virtual void imAddressClicked(); + + virtual TQPopupMenu *createPopupMenu( const TQPoint& ); + + private slots: + void slotMailClicked( const TQString&, const TQString& ); + void slotUrlClicked( const TQString& ); + void slotHighlighted( const TQString& ); + void slotPresenceChanged( const TQString & ); + void slotPresenceInfoExpired(); + void configChanged(); + + void data( KIO::Job*, const TQByteArray& ); + void result( KIO::Job* ); + + private: + void load(); + void save(); + + void updateView(); + + TQString strippedNumber( const TQString &number ); + + KConfig *mConfig; + bool mDefaultConfig; + + TQByteArray mImageData; + KIO::Job *mImageJob; + + KToggleAction *mActionShowBirthday; + KToggleAction *mActionShowAddresses; + KToggleAction *mActionShowEmails; + KToggleAction *mActionShowPhones; + KToggleAction *mActionShowURLs; + KToggleAction *mActionShowIMAddresses; + KToggleAction *mActionShowCustomFields; + + KABC::Addressee mAddressee; + int mLinkMask; + + class AddresseeViewPrivate; + AddresseeViewPrivate *d; + ::KIMProxy *mKIMProxy; +}; + +} + +#endif diff --git a/libtdepim/addressesdialog.cpp b/libtdepim/addressesdialog.cpp new file mode 100644 index 000000000..27f601833 --- /dev/null +++ b/libtdepim/addressesdialog.cpp @@ -0,0 +1,1197 @@ +/* -*- mode: C++; c-file-style: "gnu" -*- + * + * This file is part of libtdepim. + * + * Copyright (c) 2003 Zack Rusin <zack@kde.org> + * Copyright (c) 2003 Aaron J. Seigo <aseigo@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 as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * 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 "addressesdialog.h" +#include "addresspicker.h" +#include "ldapsearchdialog.h" + +#include <config.h> + +#include <libemailfunctions/email.h> + +#ifdef KDEPIM_NEW_DISTRLISTS +#include "distributionlist.h" +#include <kresources/selectdialog.h> +#include <kabc/resource.h> +#else +#include <kabc/distributionlist.h> +#endif + +#include <kabc/stdaddressbook.h> +#include <kapplication.h> +#include <kdebug.h> +#include <kglobal.h> +#include <kiconloader.h> +#include <kinputdialog.h> +#include <klineedit.h> +#include <klocale.h> +#include <kmessagebox.h> +#include <kprocess.h> +#include <kpushbutton.h> +#include <krun.h> +#include <kstandarddirs.h> + +#include <tqdict.h> +#include <tqlayout.h> +#include <tqvbox.h> +#include <tqwidget.h> + +namespace KPIM { + +// private start : +struct AddresseeViewItem::AddresseeViewItemPrivate { + KABC::Addressee address; + AddresseeViewItem::Category category; + KABC::Addressee::List addresses; +}; + +struct AddressesDialog::AddressesDialogPrivate { + AddressesDialogPrivate() : + ui(0), personal(0), recent(0), + toItem(0), ccItem(0), bccItem(0), + ldapSearchDialog(0) + {} + + AddressPickerUI *ui; + + AddresseeViewItem *personal; + AddresseeViewItem *recent; + AddresseeViewItem *topdist; + TQPtrList<AddresseeViewItem> dists; + + AddresseeViewItem *toItem; + AddresseeViewItem *ccItem; + AddresseeViewItem *bccItem; + + TQDict<AddresseeViewItem> groupDict; + + KABC::Addressee::List recentAddresses; + LDAPSearchDialog *ldapSearchDialog; +}; +// privates end + +AddresseeViewItem::AddresseeViewItem( AddresseeViewItem *parent, const KABC::Addressee& addr, + int emailIndex ) + : TQObject( 0 ), KListViewItem( parent, addr.realName(), + ( emailIndex == 0 ? addr.preferredEmail() : addr.emails()[ emailIndex ] ) ) +{ + d = new AddresseeViewItemPrivate; + d->address = addr; + d->category = Entry; + + if ( text( 0 ).stripWhiteSpace().isEmpty() ) + setText( 0, addr.preferredEmail() ); + + if ( addr.photo().url().isEmpty() ) { + if ( addr.photo().data().isNull() ) + setPixmap( 0, KGlobal::iconLoader()->loadIcon( "personal", KIcon::Small ) ); + else + setPixmap( 0, addr.photo().data().smoothScale( 16, 16 ) ); + } else { + setPixmap( 0, KGlobal::iconLoader()->loadIcon( addr.photo().url(), KIcon::Small ) ); + } +} + +AddresseeViewItem::AddresseeViewItem( KListView *lv, const TQString& name, Category cat ) + : TQObject(0), KListViewItem( lv, name ) +{ + d = new AddresseeViewItemPrivate; + d->category = cat; +} + +AddresseeViewItem::AddresseeViewItem( AddresseeViewItem *parent, const TQString& name, + const KABC::Addressee::List &lst ) + : TQObject(0), KListViewItem( parent, name, i18n("<group>") ) +{ + d = new AddresseeViewItemPrivate; + d->category = FilledGroup; + d->addresses = lst; +} + +AddresseeViewItem::AddresseeViewItem( AddresseeViewItem *parent, const TQString& name ) + : TQObject(0), KListViewItem( parent, name, i18n("<group>") ) +{ + d = new AddresseeViewItemPrivate; + d->category = DistList; + + setPixmap( 0, KGlobal::iconLoader()->loadIcon( "kdmconfig", KIcon::Small ) ); +} + +AddresseeViewItem::~AddresseeViewItem() +{ + delete d; + d = 0; +} + +KABC::Addressee +AddresseeViewItem::addressee() const +{ + return d->address; +} + +KABC::Addressee::List +AddresseeViewItem::addresses() const +{ + return d->addresses; +} + +AddresseeViewItem::Category +AddresseeViewItem::category() const +{ + return d->category; +} + +TQString +AddresseeViewItem::name() const +{ + return text(0); +} + +TQString +AddresseeViewItem::email() const +{ + return text(1); +} + +bool AddresseeViewItem::matches(const TQString& txt) const +{ + return d->address.realName().contains(txt, false) || d->address.preferredEmail().contains(txt, false); +} + +void AddresseeViewItem::setSelected(bool selected) +{ + if (selected == isSelected()) + { + return; + } + + emit addressSelected( this, selected ); + TQListViewItem::setSelected(selected); +} + +int +AddresseeViewItem::compare( TQListViewItem * i, int col, bool ascending ) const +{ + if ( category() == Group || category() == Entry ) + return KListViewItem::compare( i , col, ascending ); + + AddresseeViewItem *item = static_cast<AddresseeViewItem*>( i ); + int a = static_cast<int>( category() ); + int b = static_cast<int>( item->category() ); + + if ( ascending ) + if ( a < b ) + return -1; + else + return 1; + else + if ( a < b ) + return 1; + else + return -1; +} + +AddressesDialog::AddressesDialog( TQWidget *widget, const char *name ) + : KDialogBase( widget, name, true, i18n("Address Selection"), + Ok|Cancel, Ok, true ) +{ + TQVBox *page = makeVBoxMainWidget(); + d = new AddressesDialogPrivate; + d->ui = new AddressPickerUI( page ); + + KABC::StdAddressBook::self( true ); + updateAvailableAddressees(); + initConnections(); + + d->ui->mAvailableView->setFocus(); + + setMainWidget( page ); + page->setMinimumSize( 750, 400 ); +} + +AddressesDialog::~AddressesDialog() +{ + delete d; + d = 0; +} + +AddresseeViewItem* AddressesDialog::selectedToItem() +{ + if ( !d->toItem ) + { + d->toItem = new AddresseeViewItem( d->ui->mSelectedView, i18n("To"), AddresseeViewItem::To ); + connect(d->toItem, TQT_SIGNAL(addressSelected(AddresseeViewItem*, bool)), + this, TQT_SLOT(selectedAddressSelected(AddresseeViewItem*, bool))); + } + return d->toItem; +} + +AddresseeViewItem* AddressesDialog::selectedCcItem() +{ + if ( !d->ccItem ) + { + d->ccItem = new AddresseeViewItem( d->ui->mSelectedView, i18n("CC"), AddresseeViewItem::CC ); + connect(d->ccItem, TQT_SIGNAL(addressSelected(AddresseeViewItem*, bool)), + this, TQT_SLOT(selectedAddressSelected(AddresseeViewItem*, bool))); + } + return d->ccItem; +} + +AddresseeViewItem* AddressesDialog::selectedBccItem() +{ + if ( !d->bccItem ) + { + d->bccItem = new AddresseeViewItem( d->ui->mSelectedView, i18n("BCC"), AddresseeViewItem::BCC ); + connect(d->bccItem, TQT_SIGNAL(addressSelected(AddresseeViewItem*, bool)), + this, TQT_SLOT(selectedAddressSelected(AddresseeViewItem*, bool))); + } + return d->bccItem; +} + +void +AddressesDialog::setSelectedTo( const TQStringList& l ) +{ + TQString name, email; + for ( TQStringList::ConstIterator it = l.begin(); it != l.end(); ++it ) { + KABC::Addressee addr; + KABC::Addressee::parseEmailAddress( *it, name, email ); + addr.setNameFromString( name ); + addr.insertEmail( email ); + addAddresseeToSelected( addr, selectedToItem() ); + } +} + +void +AddressesDialog::setSelectedCC( const TQStringList& l ) +{ + TQString name, email; + for ( TQStringList::ConstIterator it = l.begin(); it != l.end(); ++it ) { + KABC::Addressee addr; + KABC::Addressee::parseEmailAddress( *it, name, email ); + addr.setNameFromString( name ); + addr.insertEmail( email ); + addAddresseeToSelected( addr, selectedCcItem() ); + } +} + +void +AddressesDialog::setSelectedBCC( const TQStringList& l ) +{ + TQString name, email; + for ( TQStringList::ConstIterator it = l.begin(); it != l.end(); ++it ) { + KABC::Addressee addr; + KABC::Addressee::parseEmailAddress( *it, name, email ); + addr.setNameFromString( name ); + addr.insertEmail( email ); + addAddresseeToSelected( addr, selectedBccItem() ); + } +} + +void +AddressesDialog::setRecentAddresses( const KABC::Addressee::List& list ) +{ + d->recentAddresses = list; + + updateRecentAddresses(); + + checkForSingleAvailableGroup(); +} + +void +AddressesDialog::updateRecentAddresses() +{ + static const TQString &recentGroup = KGlobal::staticQString( i18n( "Recent Addresses" ) ); + + if ( !d->recent ) { + d->recent = new AddresseeViewItem( d->ui->mAvailableView, recentGroup ); + connect(d->recent, TQT_SIGNAL(addressSelected(AddresseeViewItem*, bool)), + this, TQT_SLOT(availableAddressSelected(AddresseeViewItem*, bool))); + d->recent->setVisible( false ); + d->groupDict.insert( recentGroup, d->recent ); + } + + KABC::Addressee::List::ConstIterator it; + for ( it = d->recentAddresses.begin(); it != d->recentAddresses.end(); ++it ) + addAddresseeToAvailable( *it, d->recent ); + + if ( d->recent->childCount() > 0 ) { + d->recent->setOpen( true ); + d->recent->setVisible( true ); + } +} + +void +AddressesDialog::setShowCC( bool b ) +{ + d->ui->mCCButton->setShown( b ); +} + +void +AddressesDialog::setShowBCC( bool b ) +{ + d->ui->mBCCButton->setShown( b ); +} + +TQStringList +AddressesDialog::to() const +{ + TQStringList emails = allDistributionLists( d->toItem ); + KABC::Addressee::List l = toAddresses(); + emails += entryToString( l ); + + return emails; +} + +TQStringList +AddressesDialog::cc() const +{ + TQStringList emails = allDistributionLists( d->ccItem ); + KABC::Addressee::List l = ccAddresses(); + emails += entryToString( l ); + + return emails; +} + +TQStringList +AddressesDialog::bcc() const +{ + TQStringList emails = allDistributionLists( d->bccItem ); + + KABC::Addressee::List l = bccAddresses(); + emails += entryToString( l ); + + return emails; +} + +KABC::Addressee::List +AddressesDialog::toAddresses() const +{ + return allAddressee( d->toItem ); +} +KABC::Addressee::List +AddressesDialog::allToAddressesNoDuplicates() const +{ + KABC::Addressee::List aList = allAddressee( d->toItem ); + const TQStringList dList = toDistributionLists(); + KABC::AddressBook* abook = KABC::StdAddressBook::self( true ); +#ifdef KDEPIM_NEW_DISTRLISTS + for ( TQStringList::ConstIterator it = dList.begin(); it != dList.end(); ++it ) { + const TQValueList<KPIM::DistributionList::Entry> eList + = KPIM::DistributionList::findByName(abook, *it).entries(abook); + TQValueList<KPIM::DistributionList::Entry>::ConstIterator eit; + for( eit = eList.begin(); eit != eList.end(); ++eit ) { + KABC::Addressee a = (*eit).addressee; + if ( !a.preferredEmail().isEmpty() && aList.find( a ) == aList.end() ) { + aList.append( a ) ; + } + } + } +#else + KABC::DistributionListManager manager( abook ); + manager.load(); + for ( TQStringList::ConstIterator it = dList.begin(); it != dList.end(); ++it ) { + const TQValueList<KABC::DistributionList::Entry> eList = manager.list( *it )->entries(); + TQValueList<KABC::DistributionList::Entry>::ConstIterator eit; + for( eit = eList.begin(); eit != eList.end(); ++eit ) { + KABC::Addressee a = (*eit).addressee; + if ( !a.preferredEmail().isEmpty() && aList.find( a ) == aList.end() ) { + aList.append( a ) ; + } + } + } +#endif + return aList; +} + +KABC::Addressee::List +AddressesDialog::ccAddresses() const +{ + return allAddressee( d->ccItem ); +} + +KABC::Addressee::List +AddressesDialog::bccAddresses() const +{ + return allAddressee( d->bccItem ); +} + + +TQStringList +AddressesDialog::toDistributionLists() const +{ + return allDistributionLists( d->toItem ); +} + +TQStringList +AddressesDialog::ccDistributionLists() const +{ + return allDistributionLists( d->ccItem ); +} + +TQStringList +AddressesDialog::bccDistributionLists() const +{ + return allDistributionLists( d->bccItem ); +} + +void +AddressesDialog::updateAvailableAddressees() +{ + d->ui->mAvailableView->clear(); + d->groupDict.clear(); + + static const TQString &personalGroup = KGlobal::staticQString( i18n( "Other Addresses" ) ); + d->ui->mAvailableView->setRootIsDecorated( true ); + d->personal = new AddresseeViewItem( d->ui->mAvailableView, personalGroup ); + //connect(d->personal, TQT_SIGNAL(addressSelected(AddresseeViewItem*, bool)), + // this, TQT_SLOT(selectedAddressSelected(AddresseeViewItem*, bool))); + d->personal->setVisible( false ); + d->groupDict.insert( personalGroup, d->personal ); + + KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true ); + for( KABC::AddressBook::Iterator it = addressBook->begin(); + it != addressBook->end(); ++it ) { + addAddresseeToAvailable( *it, d->personal ); + } + + d->recent = 0; + updateRecentAddresses(); + + d->topdist = 0; + addDistributionLists(); + if ( d->personal->childCount() > 0 ) { + d->personal->setOpen( true ); + d->personal->setVisible( true ); + } + + checkForSingleAvailableGroup(); +} + +void AddressesDialog::checkForSingleAvailableGroup() +{ + TQListViewItem* item = d->ui->mAvailableView->firstChild(); + TQListViewItem* firstGroup = 0; + int found = 0; + while (item) + { + if (item->isVisible()) + { + if (!firstGroup && static_cast<AddresseeViewItem*>(item)->category() != AddresseeViewItem::Entry) + { + firstGroup = item; + } + ++found; + } + item = item->nextSibling(); + } + + if (found == 1 && firstGroup) + { + firstGroup->setOpen(true); + } +} + +void +AddressesDialog::availableSelectionChanged() +{ + bool selection = !selectedAvailableAddresses.isEmpty(); + d->ui->mToButton->setEnabled(selection); + d->ui->mCCButton->setEnabled(selection); + d->ui->mBCCButton->setEnabled(selection); +} + +void +AddressesDialog::selectedSelectionChanged() +{ + bool selection = !selectedSelectedAddresses.isEmpty(); + d->ui->mRemoveButton->setEnabled(selection); +} + +void +AddressesDialog::availableAddressSelected( AddresseeViewItem* item, bool selected ) +{ + if (selected) + { + selectedAvailableAddresses.append(item); + } + else + { + selectedAvailableAddresses.remove(item); + } +} + +void +AddressesDialog::selectedAddressSelected( AddresseeViewItem* item, bool selected ) +{ + // we have to avoid that a parent and a child is selected together + // because in this case we get a double object deletion ( program crashes ) + // when removing the selected items from list + AddresseeViewItem* parent = static_cast<AddresseeViewItem*>(((TQListViewItem*)item)->parent()); + if ( parent && selected ) + parent->setSelected( false ); + if (selected) + { + selectedSelectedAddresses.append(item); + } + else + { + selectedSelectedAddresses.remove(item); + } + if ( selected ) { + AddresseeViewItem* child = static_cast<AddresseeViewItem*>(item->firstChild()); + while (child) { + child->setSelected( false ); + child = static_cast<AddresseeViewItem*>(child->nextSibling()); + } + } +} + +void +AddressesDialog::initConnections() +{ + connect( d->ui->mFilterEdit, TQT_SIGNAL(textChanged(const TQString &)), + TQT_SLOT(filterChanged(const TQString &)) ); + connect( d->ui->mToButton, TQT_SIGNAL(clicked()), + TQT_SLOT(addSelectedTo()) ); + connect( d->ui->mCCButton, TQT_SIGNAL(clicked()), + TQT_SLOT(addSelectedCC()) ); + connect( d->ui->mBCCButton, TQT_SIGNAL(clicked()), + TQT_SLOT(addSelectedBCC()) ); + connect( d->ui->mSaveAs, TQT_SIGNAL(clicked()), + TQT_SLOT(saveAs()) ); + connect( d->ui->mLdapSearch, TQT_SIGNAL(clicked()), + TQT_SLOT(searchLdap()) ); + connect( d->ui->mRemoveButton, TQT_SIGNAL(clicked()), + TQT_SLOT(removeEntry()) ); + connect( d->ui->mAvailableView, TQT_SIGNAL(selectionChanged()), + TQT_SLOT(availableSelectionChanged()) ); + connect( d->ui->mAvailableView, TQT_SIGNAL(doubleClicked(TQListViewItem*)), + TQT_SLOT(addSelectedTo()) ); + connect( d->ui->mSelectedView, TQT_SIGNAL(selectionChanged()), + TQT_SLOT(selectedSelectionChanged()) ); + connect( d->ui->mSelectedView, TQT_SIGNAL(doubleClicked(TQListViewItem*)), + TQT_SLOT(removeEntry()) ); + +#ifndef KDEPIM_NEW_DISTRLISTS + connect( KABC::DistributionListWatcher::self(), TQT_SIGNAL( changed() ), + this, TQT_SLOT( updateAvailableAddressees() ) ); +#endif + + connect( KABC::StdAddressBook::self( true ), TQT_SIGNAL( addressBookChanged(AddressBook*) ), + this, TQT_SLOT( updateAvailableAddressees() ) ); +} + +void +AddressesDialog::addAddresseeToAvailable( const KABC::Addressee& addr, AddresseeViewItem* defaultParent, bool useCategory ) +{ + if ( addr.preferredEmail().isEmpty() ) + return; + + if ( useCategory ) { + TQStringList categories = addr.categories(); + + for ( TQStringList::Iterator it = categories.begin(); it != categories.end(); ++it ) { + if ( !d->groupDict[ *it ] ) { //we don't have the category yet + AddresseeViewItem* category = new AddresseeViewItem( d->ui->mAvailableView, *it ); + d->groupDict.insert( *it, category ); + } + + for ( uint i = 0; i < addr.emails().count(); ++i ) { + AddresseeViewItem* addressee = new AddresseeViewItem( d->groupDict[ *it ], addr, i ); + connect(addressee, TQT_SIGNAL(addressSelected(AddresseeViewItem*, bool)), + this, TQT_SLOT(availableAddressSelected(AddresseeViewItem*, bool))); + } + } + } + + bool noCategory = false; + if ( useCategory ) { + if ( addr.categories().isEmpty() ) + noCategory = true; + } else + noCategory = true; + + if ( defaultParent && noCategory ) { // only non-categorized items here + AddresseeViewItem* addressee = new AddresseeViewItem( defaultParent, addr ); + connect(addressee, TQT_SIGNAL(addressSelected(AddresseeViewItem*, bool)), + this, TQT_SLOT(availableAddressSelected(AddresseeViewItem*, bool))); + } +} + +void +AddressesDialog::addAddresseeToSelected( const KABC::Addressee& addr, AddresseeViewItem* defaultParent ) +{ + if ( addr.preferredEmail().isEmpty() ) + return; + + if ( defaultParent ) { + AddresseeViewItem *myChild = static_cast<AddresseeViewItem*>( defaultParent->firstChild() ); + while( myChild ) { + if ( myChild->addressee().preferredEmail() == addr.preferredEmail() ) + return;//already got it + myChild = static_cast<AddresseeViewItem*>( myChild->nextSibling() ); + } + AddresseeViewItem* addressee = new AddresseeViewItem( defaultParent, addr ); + connect(addressee, TQT_SIGNAL(addressSelected(AddresseeViewItem*, bool)), + this, TQT_SLOT(selectedAddressSelected(AddresseeViewItem*, bool))); + defaultParent->setOpen( true ); + } + + d->ui->mSaveAs->setEnabled(true); +} + +void +AddressesDialog::addAddresseesToSelected( AddresseeViewItem *parent, + const TQPtrList<AddresseeViewItem>& addresses ) +{ + Q_ASSERT( parent ); + + TQPtrListIterator<AddresseeViewItem> itr( addresses ); + + if (itr.current()) + { + d->ui->mSaveAs->setEnabled(true); + } + + while ( itr.current() ) { + AddresseeViewItem* address = itr.current(); + ++itr; + + if (selectedToAvailableMapping.find(address) != 0) + { + continue; + } + + AddresseeViewItem* newItem = 0; + if (address->category() == AddresseeViewItem::Entry) + { + newItem = new AddresseeViewItem( parent, address->addressee() ); + } + else if (address->category() == AddresseeViewItem::DistList) + { + newItem = new AddresseeViewItem( parent, address->name() ); + } + else + { + newItem = new AddresseeViewItem( parent, address->name(), allAddressee( address ) ); + } + + address->setSelected( false ); + address->setVisible( false ); + selectedToAvailableMapping.insert(address, newItem); + selectedToAvailableMapping.insert(newItem, address); + connect(newItem, TQT_SIGNAL(addressSelected(AddresseeViewItem*, bool)), + this, TQT_SLOT(selectedAddressSelected(AddresseeViewItem*, bool))); + } + + parent->setOpen( true ); +} + +TQStringList +AddressesDialog::entryToString( const KABC::Addressee::List& l ) const +{ + TQStringList entries; + + for( KABC::Addressee::List::ConstIterator it = l.begin(); it != l.end(); ++it ) { + entries += (*it).fullEmail(); + } + return entries; +} + +void +AddressesDialog::addSelectedTo() +{ + if ( !d->toItem ) + { + d->toItem = new AddresseeViewItem( d->ui->mSelectedView, i18n("To"), AddresseeViewItem::To ); + connect(d->toItem, TQT_SIGNAL(addressSelected(AddresseeViewItem*, bool)), + this, TQT_SLOT(selectedAddressSelected(AddresseeViewItem*, bool))); + } + + addAddresseesToSelected( d->toItem, selectedAvailableAddresses ); + selectedAvailableAddresses.clear(); + + if ( d->toItem->childCount() > 0 ) { + d->toItem->setVisible( true ); + } else { + delete d->toItem; + d->toItem = 0; + } +} + +void +AddressesDialog::addSelectedCC() +{ + if ( !d->ccItem ) + { + d->ccItem = new AddresseeViewItem( d->ui->mSelectedView, i18n("CC"), AddresseeViewItem::CC ); + connect(d->ccItem , TQT_SIGNAL(addressSelected(AddresseeViewItem*, bool)), + this, TQT_SLOT(selectedAddressSelected(AddresseeViewItem*, bool))); + } + + addAddresseesToSelected( d->ccItem, selectedAvailableAddresses ); + selectedAvailableAddresses.clear(); + + if ( d->ccItem->childCount() > 0 ) { + d->ccItem->setVisible( true ); + } else { + delete d->ccItem; + d->ccItem = 0; + } +} + +void +AddressesDialog::addSelectedBCC() +{ + if ( !d->bccItem ) + { + d->bccItem = new AddresseeViewItem( d->ui->mSelectedView, i18n("BCC"), AddresseeViewItem::BCC ); + connect(d->bccItem , TQT_SIGNAL(addressSelected(AddresseeViewItem*, bool)), + this, TQT_SLOT(selectedAddressSelected(AddresseeViewItem*, bool))); + } + + addAddresseesToSelected( d->bccItem, selectedAvailableAddresses ); + selectedAvailableAddresses.clear(); + + if ( d->bccItem->childCount() > 0 ) { + d->bccItem->setVisible( true ); + } else { + delete d->bccItem; + d->bccItem = 0; + } +} + +void AddressesDialog::unmapSelectedAddress(AddresseeViewItem* item) +{ + AddresseeViewItem* correspondingItem = selectedToAvailableMapping[item]; + if (correspondingItem) + { + correspondingItem->setVisible( true ); + selectedToAvailableMapping.remove( item ); + selectedToAvailableMapping.remove( correspondingItem ); + } + + AddresseeViewItem* child = static_cast<AddresseeViewItem*>(item->firstChild()); + while (child) + { + unmapSelectedAddress(child); + child = static_cast<AddresseeViewItem*>(child->nextSibling()); + } +} + +void +AddressesDialog::removeEntry() +{ + TQPtrList<AddresseeViewItem> lst; + bool resetTo = false; + bool resetCC = false; + bool resetBCC = false; + + lst.setAutoDelete( false ); + TQPtrListIterator<AddresseeViewItem> it( selectedSelectedAddresses ); + while ( it.current() ) { + AddresseeViewItem* item = it.current(); + ++it; + if ( d->toItem == item ) + resetTo = true; + else if ( d->ccItem == item ) + resetCC = true; + else if( d->bccItem == item ) + resetBCC = true; + // we may only append parent items + unmapSelectedAddress(item); + lst.append( item ); + } + selectedSelectedAddresses.clear(); + lst.setAutoDelete( true ); + lst.clear(); + if ( resetTo ) + d->toItem = 0; + else if ( d->toItem && d->toItem->childCount() == 0 ) + { + delete d->toItem; + d->toItem = 0; + } + if ( resetCC ) + d->ccItem = 0; + else if ( d->ccItem && d->ccItem->childCount() == 0 ) + { + delete d->ccItem; + d->ccItem = 0; + } + if ( resetBCC ) + d->bccItem = 0; + else if ( d->bccItem && d->bccItem->childCount() == 0 ) + { + delete d->bccItem; + d->bccItem = 0; + } + d->ui->mSaveAs->setEnabled(d->ui->mSelectedView->firstChild() != 0); +} + +#ifdef KDEPIM_NEW_DISTRLISTS + +// copied from kabcore.cpp :( +// KDE4: should be in libkabc I think +static KABC::Resource *requestResource( KABC::AddressBook* abook, TQWidget *parent ) +{ + TQPtrList<KABC::Resource> kabcResources = abook->resources(); + + TQPtrList<KRES::Resource> kresResources; + TQPtrListIterator<KABC::Resource> resIt( kabcResources ); + KABC::Resource *resource; + while ( ( resource = resIt.current() ) != 0 ) { + ++resIt; + if ( !resource->readOnly() ) { + KRES::Resource *res = static_cast<KRES::Resource*>( resource ); + if ( res ) + kresResources.append( res ); + } + } + + KRES::Resource *res = KRES::SelectDialog::getResource( kresResources, parent ); + return static_cast<KABC::Resource*>( res ); +} +#endif + +void +AddressesDialog::saveAs() +{ +#ifndef KDEPIM_NEW_DISTRLISTS + KABC::DistributionListManager manager( KABC::StdAddressBook::self( true ) ); + manager.load(); +#endif + + if ( !d->ui->mSelectedView->firstChild() ) { + KMessageBox::information( 0, + i18n("There are no addresses in your list. " + "First add some addresses from your address book, " + "then try again.") ); + return; + } + + bool ok = false; + TQString name = KInputDialog::getText( i18n("New Distribution List"), + i18n("Please enter name:"), + TQString(), &ok, + this ); + if ( !ok || name.isEmpty() ) + return; + + bool alreadyExists = false; +#ifdef KDEPIM_NEW_DISTRLISTS + KABC::AddressBook* abook = KABC::StdAddressBook::self( true ); + KPIM::DistributionList dlist = KPIM::DistributionList::findByName( abook, name ); + alreadyExists = !dlist.isEmpty(); +#else + alreadyExists = manager.list( name ); +#endif + + if ( alreadyExists ) { + KMessageBox::information( 0, + i18n( "<qt>Distribution list with the given name <b>%1</b> " + "already exists. Please select a different name.</qt>" ) + .tqarg( name ) ); + return; + } + +#ifdef KDEPIM_NEW_DISTRLISTS + KABC::Resource* resource = requestResource( abook, this ); + if ( !resource ) + return; + + dlist.setResource( resource ); + dlist.setName( name ); + KABC::Addressee::List addrl = allAddressee( d->ui->mSelectedView, false ); + for ( KABC::Addressee::List::iterator itr = addrl.begin(); + itr != addrl.end(); ++itr ) { + dlist.insertEntry( *itr ); + } + abook->insertAddressee( dlist ); +#else + KABC::DistributionList *dlist = new KABC::DistributionList( &manager, name ); + KABC::Addressee::List addrl = allAddressee( d->ui->mSelectedView, false ); + for ( KABC::Addressee::List::iterator itr = addrl.begin(); + itr != addrl.end(); ++itr ) { + dlist->insertEntry( *itr ); + } + + manager.save(); +#endif +} + +void +AddressesDialog::searchLdap() +{ + if ( !d->ldapSearchDialog ) { + d->ldapSearchDialog = new LDAPSearchDialog( this ); + connect( d->ldapSearchDialog, TQT_SIGNAL( addresseesAdded() ), + TQT_SLOT(ldapSearchResult() ) ); + } + d->ldapSearchDialog->show(); +} + +void +AddressesDialog::ldapSearchResult() +{ + TQStringList emails = KPIM::splitEmailAddrList( d->ldapSearchDialog->selectedEMails() ); + TQStringList::iterator it( emails.begin() ); + TQStringList::iterator end( emails.end() ); + for ( ; it != end; ++it ){ + TQString name; + TQString email; + KPIM::getNameAndMail( (*it), name, email ); + KABC::Addressee ad; + ad.setNameFromString( name ); + ad.insertEmail( email ); + addAddresseeToSelected( ad, selectedToItem() ); + } +} + +void +AddressesDialog::launchAddressBook() +{ + kapp->startServiceByDesktopName( "kaddressbook", TQString() ); +} + +void +AddressesDialog::filterChanged( const TQString& txt ) +{ + TQListViewItemIterator it( d->ui->mAvailableView ); + bool showAll = false; + + if ( txt.isEmpty() ) + showAll = true; + + int personalVisible = 0; + int recentVisible = 0; + while ( it.current() ) { + AddresseeViewItem* item = static_cast<AddresseeViewItem*>( it.current() ); + ++it; + + if ( showAll ) { + item->setOpen( true ); + item->setVisible( true ); + // allen: I do not like the following behavior. comment out and see if anyone screams + //if ( item->category() == AddresseeViewItem::Group ) + // item->setOpen( false );//close to not have too many entries + continue; + } + + if ( item->category() == AddresseeViewItem::Entry ) { + bool matches = item->matches( txt ); + item->setVisible( matches ); + TQListViewItem *parent = static_cast<TQListViewItem*>( item )->parent(); + if ( matches && parent ) { + if ( parent == d->personal ) { + personalVisible++; + } else if ( parent == d->recent ) { + recentVisible++; + } + } + } + if ( item->category() == AddresseeViewItem::Group ) { + item->setOpen( true ); + item->setVisible( true ); + } + } + + if ( !showAll && personalVisible == 0 ) { + d->personal->setOpen( false ); + d->personal->setVisible( false ); + } + if ( !showAll && recentVisible == 0 ) { + d->recent->setOpen( false ); + d->recent->setVisible( false ); + } + + int distlistgroupVisible = 0; + if ( !showAll ) { + TQPtrListIterator<AddresseeViewItem> it( d->dists ); + for ( ; it.current(); ++it ) { + TQListViewItem *p = *it; + p->setVisible( true ); + AddresseeViewItem *p2 = static_cast<AddresseeViewItem*>( p->firstChild() ); + int pcount = 0; + while ( p2 ) { + if ( p2->matches( txt ) ) { + p2->setVisible( true ); + pcount++; + } else { + p2->setVisible( false ); + } + p2 = static_cast<AddresseeViewItem*>( p2->nextSibling() ); + } + if ( !pcount && !p->text( 0 ).contains( txt, false ) ) { + p->setVisible( false ); + } + distlistgroupVisible += pcount; + if ( p->text( 0 ).contains( txt, false ) ) { + distlistgroupVisible++; + } + } + } + if ( d->topdist ) { + if ( showAll || distlistgroupVisible > 0 ) { + d->topdist->setOpen( true ); + } else { + d->topdist->setOpen( false ); + d->topdist->setVisible( false ); + } + } +} + +KABC::Addressee::List +AddressesDialog::allAddressee( KListView* view, bool onlySelected ) const +{ + KABC::Addressee::List lst; + TQListViewItemIterator it( view ); + while ( it.current() ) { + AddresseeViewItem* item = static_cast<AddresseeViewItem*>( it.current() ); + if ( !onlySelected || item->isSelected() ) { + if ( item->category() != AddresseeViewItem::Entry ) { + AddresseeViewItem *myChild = static_cast<AddresseeViewItem*>( item->firstChild() ); + while( myChild ) { + lst.append( myChild->addressee() ); + myChild = static_cast<AddresseeViewItem*>( myChild->nextSibling() ); + } + } else { + lst.append( item->addressee() ); + } + } + ++it; + } + + return lst; +} + +KABC::Addressee::List +AddressesDialog::allAddressee( AddresseeViewItem* parent ) const +{ + KABC::Addressee::List lst; + + if ( !parent ) return lst; + + if ( parent->category() == AddresseeViewItem::Entry ) + { + lst.append( parent->addressee() ); + return lst; + } + + AddresseeViewItem *myChild = static_cast<AddresseeViewItem*>( parent->firstChild() ); + while( myChild ) { + if ( myChild->category() == AddresseeViewItem::FilledGroup ) + lst += myChild->addresses(); + else if ( !myChild->addressee().isEmpty() ) + lst.append( myChild->addressee() ); + myChild = static_cast<AddresseeViewItem*>( myChild->nextSibling() ); + } + + return lst; +} + +TQStringList +AddressesDialog::allDistributionLists( AddresseeViewItem* parent ) const +{ + TQStringList lists; + + if ( !parent ) + return TQStringList(); + + AddresseeViewItem *item = static_cast<AddresseeViewItem*>( parent->firstChild() ); + while ( item ) { + if ( item->category() == AddresseeViewItem::DistList && !item->name().isEmpty() ) + lists.append( item->name() ); + + item = static_cast<AddresseeViewItem*>( item->nextSibling() ); + } + + return lists; +} + +void +AddressesDialog::addDistributionLists() +{ + KABC::AddressBook* abook = KABC::StdAddressBook::self( true ); + +#ifdef KDEPIM_NEW_DISTRLISTS + const TQValueList<KPIM::DistributionList> distLists = + KPIM::DistributionList::allDistributionLists( abook ); +#else + KABC::DistributionListManager manager( abook ); + manager.load(); + + TQStringList distLists = manager.listNames(); +#endif + + if ( distLists.isEmpty() ) + return; + + if ( !d->topdist ) { + d->topdist = new AddresseeViewItem( d->ui->mAvailableView, i18n( "Distribution Lists" ) ); + } + +#ifdef KDEPIM_NEW_DISTRLISTS + TQValueList<KPIM::DistributionList>::ConstIterator listIt; +#else + TQStringList::Iterator listIt; +#endif + int total = 0; + for ( listIt = distLists.begin(); listIt != distLists.end(); ++listIt ) { +#ifdef KDEPIM_NEW_DISTRLISTS + KPIM::DistributionList dlist = *listIt; + KPIM::DistributionList::Entry::List entries = dlist.entries(abook); +#else + KABC::DistributionList& dlist = *manager.list( *listIt ); + KABC::DistributionList::Entry::List entries = dlist.entries(); +#endif + + AddresseeViewItem *item = new AddresseeViewItem( d->topdist, dlist.name() ); + d->dists.append( item ); + connect( item, TQT_SIGNAL( addressSelected( AddresseeViewItem*, bool ) ), + this, TQT_SLOT( availableAddressSelected( AddresseeViewItem*, bool ) ) ); + +#ifdef KDEPIM_NEW_DISTRLISTS + KPIM::DistributionList::Entry::List::Iterator itemIt; +#else + KABC::DistributionList::Entry::List::Iterator itemIt; +#endif + for ( itemIt = entries.begin(); itemIt != entries.end(); ++itemIt ) { + addAddresseeToAvailable( (*itemIt).addressee, item, false ); + } + if ( item->childCount() > 0 ) { + item->setOpen( true ); + item->setVisible( true ); + } + total += item->childCount(); + } + if ( total > 0 ) { + d->topdist->setOpen( true ); + d->topdist->setVisible( true ); + } +} + +} // namespace + +#include "addressesdialog.moc" diff --git a/libtdepim/addressesdialog.h b/libtdepim/addressesdialog.h new file mode 100644 index 000000000..c553c6421 --- /dev/null +++ b/libtdepim/addressesdialog.h @@ -0,0 +1,223 @@ +/* -*- mode: C++; c-file-style: "gnu" -*- + * + * This file is part of libtdepim. + * + * Copyright (c) 2003 Zack Rusin <zack@kde.org> + * Copyright (c) 2003 Aaron J. Seigo <aseigo@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 as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * 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. + * + */ + +#ifndef ADDRESSESDIALOG_H +#define ADDRESSESDIALOG_H + +#include <kabc/addressee.h> +#include <kdialogbase.h> +#include <klistview.h> +#include <tqstringlist.h> +#include <tqptrlist.h> +#include <tqptrdict.h> +#include <tdepimmacros.h> + +namespace KPIM { + + class AddresseeViewItem : public TQObject, public KListViewItem + { + Q_OBJECT + TQ_OBJECT + + public: + enum Category { + To =0, + CC =1, + BCC =2, + Group =3, + Entry =4, + FilledGroup =5, + DistList =6 + }; + AddresseeViewItem( AddresseeViewItem *parent, const KABC::Addressee& addr, int emailIndex = 0 ); + AddresseeViewItem( KListView *lv, const TQString& name, Category cat=Group ); + AddresseeViewItem( AddresseeViewItem *parent, const TQString& name, const KABC::Addressee::List &lst ); + AddresseeViewItem( AddresseeViewItem *parent, const TQString& name ); + ~AddresseeViewItem(); + + KABC::Addressee addressee() const; + KABC::Addressee::List addresses() const; + Category category() const; + + TQString name() const; + TQString email() const; + + bool matches( const TQString& ) const; + + virtual int compare( TQListViewItem * i, int col, bool ascending ) const; + virtual void setSelected( bool ); + + signals: + void addressSelected( AddresseeViewItem*, bool ); + + private: + struct AddresseeViewItemPrivate; + AddresseeViewItemPrivate *d; + }; + + class KDE_EXPORT AddressesDialog : public KDialogBase + { + Q_OBJECT + TQ_OBJECT + public: + AddressesDialog( TQWidget *widget=0, const char *name=0 ); + ~AddressesDialog(); + + /** + * Returns the list of picked "To" addresses as a TQStringList. + */ + TQStringList to() const; + /** + * Returns the list of picked "CC" addresses as a TQStringList. + */ + TQStringList cc() const; + /** + * Returns the list of picked "BCC" addresses as a TQStringList. + */ + TQStringList bcc() const; + + /** + * Returns the list of picked "To" addresses as KABC::Addressee::List. + * Note that this doesn't include the distribution lists + */ + KABC::Addressee::List toAddresses() const; + /** + * Returns the list of picked "To" addresses as KABC::Addressee::List. + * Note that this does include the distribution lists + * Multiple Addressees are removed + */ + KABC::Addressee::List allToAddressesNoDuplicates() const; + /** + * Returns the list of picked "CC" addresses as KABC::Addressee::List. + * Note that this doesn't include the distribution lists + */ + KABC::Addressee::List ccAddresses() const; + /** + * Returns the list of picked "BCC" addresses as KABC::Addressee::List. + * Note that this doesn't include the distribution lists + */ + KABC::Addressee::List bccAddresses() const; + + /** + * Returns the list of picked "To" distribution lists. + * This complements @ref toAddresses. + */ + TQStringList toDistributionLists() const; + /** + * Returns the list of picked "CC" distribution lists. + * This complements @ref ccAddresses. + */ + TQStringList ccDistributionLists() const; + /** + * Returns the list of picked "BCC" distribution lists. + * This complements @ref bccAddresses. + */ + TQStringList bccDistributionLists() const; + + public slots: + /** + * Displays the CC field if @p b is true, else + * hides it. By default displays it. + */ + void setShowCC( bool b ); + /** + * Displays the BCC field if @p b is true, else + * hides it. By default displays it. + */ + void setShowBCC( bool b ); + /** + * If called adds "Recent Addresses" item to the picker list view, + * with the addresses given in @p addr. + */ + void setRecentAddresses( const KABC::Addressee::List& addr ); + /** + * Adds addresses in @p l to the selected "To" group. + */ + void setSelectedTo( const TQStringList& l ); + /** + * Adds addresses in @p l to the selected "CC" group. + */ + void setSelectedCC( const TQStringList& l ); + /** + * Adds addresses in @p l to the selected "BCC" group. + */ + void setSelectedBCC( const TQStringList& l ); + + protected slots: + void addSelectedTo(); + void addSelectedCC(); + void addSelectedBCC(); + + void removeEntry(); + void saveAs(); + void searchLdap(); + void ldapSearchResult(); + void launchAddressBook(); + + void filterChanged( const TQString & ); + + void updateAvailableAddressees(); + void availableSelectionChanged(); + void selectedSelectionChanged(); + void availableAddressSelected( AddresseeViewItem* item, bool selected ); + void selectedAddressSelected( AddresseeViewItem* item, bool selected ); + + protected: + AddresseeViewItem* selectedToItem(); + AddresseeViewItem* selectedCcItem(); + AddresseeViewItem* selectedBccItem(); + + void initConnections(); + void addDistributionLists(); + void addAddresseeToAvailable( const KABC::Addressee& addr, + AddresseeViewItem* defaultParent=0, bool useCategory=true ); + void addAddresseeToSelected( const KABC::Addressee& addr, + AddresseeViewItem* defaultParent=0 ); + void addAddresseesToSelected( AddresseeViewItem *parent, + const TQPtrList<AddresseeViewItem>& addresses ); + TQStringList entryToString( const KABC::Addressee::List& l ) const; + KABC::Addressee::List allAddressee( AddresseeViewItem* parent ) const; + KABC::Addressee::List allAddressee( KListView* view, bool onlySelected = true ) const; + TQStringList allDistributionLists( AddresseeViewItem* parent ) const; + + private: + // if there's only one group in the available list, open it + void checkForSingleAvailableGroup(); + + // used to re-show items in the available list + // it is recursive, but should only ever recurse once so should be fine + void unmapSelectedAddress(AddresseeViewItem* item); + void updateRecentAddresses(); + + struct AddressesDialogPrivate; + AddressesDialogPrivate *d; + + TQPtrList<AddresseeViewItem> selectedAvailableAddresses; + TQPtrList<AddresseeViewItem> selectedSelectedAddresses; + TQPtrDict<AddresseeViewItem> selectedToAvailableMapping; + }; + +} + +#endif /* ADDRESSESDIALOG_H */ diff --git a/libtdepim/addresspicker.ui b/libtdepim/addresspicker.ui new file mode 100644 index 000000000..fc88d52a0 --- /dev/null +++ b/libtdepim/addresspicker.ui @@ -0,0 +1,327 @@ +<!DOCTYPE UI><UI version="3.3" stdsetdef="1"> +<class>AddressPickerUI</class> +<widget class="TQWidget"> + <property name="name"> + <cstring>AddressPickerUI</cstring> + </property> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>591</width> + <height>442</height> + </rect> + </property> + <property name="caption"> + <string>Address Selection</string> + </property> + <grid> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <widget class="KListView" row="1" column="0"> + <column> + <property name="text"> + <string>Name</string> + </property> + <property name="clickable"> + <bool>true</bool> + </property> + <property name="resizable"> + <bool>true</bool> + </property> + </column> + <column> + <property name="text"> + <string>Email Address</string> + </property> + <property name="clickable"> + <bool>true</bool> + </property> + <property name="resizable"> + <bool>true</bool> + </property> + </column> + <property name="name"> + <cstring>mAvailableView</cstring> + </property> + <property name="selectionMode" stdset="0"> + <enum>Extended</enum> + </property> + <property name="allColumnsShowFocus"> + <bool>true</bool> + </property> + <property name="showSortIndicator"> + <bool>true</bool> + </property> + <property name="rootIsDecorated"> + <bool>true</bool> + </property> + <property name="resizeMode"> + <enum>AllColumns</enum> + </property> + <property name="fullWidth"> + <bool>true</bool> + </property> + </widget> + <widget class="TQLayoutWidget" row="1" column="1"> + <property name="name"> + <cstring>tqlayout10</cstring> + </property> + <vbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <spacer> + <property name="name"> + <cstring>spacer1</cstring> + </property> + <property name="orientation"> + <enum>Vertical</enum> + </property> + <property name="sizeType"> + <enum>Expanding</enum> + </property> + <property name="tqsizeHint"> + <size> + <width>20</width> + <height>16</height> + </size> + </property> + </spacer> + <widget class="KPushButton"> + <property name="name"> + <cstring>mToButton</cstring> + </property> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="text"> + <string>&To >></string> + </property> + </widget> + <widget class="KPushButton"> + <property name="name"> + <cstring>mCCButton</cstring> + </property> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="text"> + <string>&CC >></string> + </property> + </widget> + <widget class="KPushButton"> + <property name="name"> + <cstring>mBCCButton</cstring> + </property> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="text"> + <string>&BCC >></string> + </property> + </widget> + <spacer> + <property name="name"> + <cstring>spacer2</cstring> + </property> + <property name="orientation"> + <enum>Vertical</enum> + </property> + <property name="sizeType"> + <enum>Expanding</enum> + </property> + <property name="tqsizeHint"> + <size> + <width>20</width> + <height>16</height> + </size> + </property> + </spacer> + <widget class="KPushButton"> + <property name="name"> + <cstring>mRemoveButton</cstring> + </property> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="text"> + <string><< &Remove</string> + </property> + </widget> + <spacer> + <property name="name"> + <cstring>spacer2_2</cstring> + </property> + <property name="orientation"> + <enum>Vertical</enum> + </property> + <property name="sizeType"> + <enum>Expanding</enum> + </property> + <property name="tqsizeHint"> + <size> + <width>20</width> + <height>20</height> + </size> + </property> + </spacer> + </vbox> + </widget> + <widget class="TQLabel" row="0" column="2"> + <property name="name"> + <cstring>textLabel2</cstring> + </property> + <property name="font"> + <font> + <bold>1</bold> + </font> + </property> + <property name="text"> + <string>&Selected Addresses</string> + </property> + <property name="tqalignment"> + <set>WordBreak|AlignCenter</set> + </property> + <property name="buddy" stdset="0"> + <cstring>mSelectedView</cstring> + </property> + </widget> + <widget class="TQLabel" row="0" column="0"> + <property name="name"> + <cstring>textLabel1</cstring> + </property> + <property name="font"> + <font> + <bold>1</bold> + </font> + </property> + <property name="text"> + <string>&Address Book</string> + </property> + <property name="tqalignment"> + <set>WordBreak|AlignCenter</set> + </property> + <property name="buddy" stdset="0"> + <cstring>mAvailableView</cstring> + </property> + </widget> + <widget class="TQLayoutWidget" row="2" column="0"> + <property name="name"> + <cstring>tqlayout1</cstring> + </property> + <hbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <widget class="TQLabel"> + <property name="name"> + <cstring>TextLabel2</cstring> + </property> + <property name="text"> + <string>&Filter on:</string> + </property> + <property name="buddy" stdset="0"> + <cstring>mFilterEdit</cstring> + </property> + </widget> + <widget class="TQLineEdit"> + <property name="name"> + <cstring>mFilterEdit</cstring> + </property> + <property name="sizePolicy"> + <sizepolicy> + <hsizetype>7</hsizetype> + <vsizetype>0</vsizetype> + <horstretch>1</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string></string> + </property> + </widget> + </hbox> + </widget> + <widget class="KListView" row="1" column="2"> + <column> + <property name="text"> + <string>Name</string> + </property> + <property name="clickable"> + <bool>true</bool> + </property> + <property name="resizable"> + <bool>true</bool> + </property> + </column> + <column> + <property name="text"> + <string>Email Address</string> + </property> + <property name="clickable"> + <bool>true</bool> + </property> + <property name="resizable"> + <bool>true</bool> + </property> + </column> + <property name="name"> + <cstring>mSelectedView</cstring> + </property> + <property name="selectionMode" stdset="0"> + <enum>Extended</enum> + </property> + <property name="allColumnsShowFocus"> + <bool>true</bool> + </property> + <property name="showSortIndicator"> + <bool>true</bool> + </property> + <property name="rootIsDecorated"> + <bool>true</bool> + </property> + <property name="resizeMode"> + <enum>AllColumns</enum> + </property> + <property name="fullWidth"> + <bool>true</bool> + </property> + </widget> + <widget class="TQPushButton" row="2" column="2"> + <property name="name"> + <cstring>mSaveAs</cstring> + </property> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="text"> + <string>Save as &Distribution List...</string> + </property> + <property name="accel"> + <string>Alt+D</string> + </property> + </widget> + <widget class="TQPushButton" row="3" column="0"> + <property name="name"> + <cstring>mLdapSearch</cstring> + </property> + <property name="text"> + <string>&Search Directory Service</string> + </property> + </widget> + </grid> +</widget> +<customwidgets> +</customwidgets> +<layoutdefaults spacing="6" margin="11"/> +<includehints> + <includehint>klistview.h</includehint> + <includehint>kpushbutton.h</includehint> + <includehint>kpushbutton.h</includehint> + <includehint>kpushbutton.h</includehint> + <includehint>kpushbutton.h</includehint> + <includehint>klistview.h</includehint> +</includehints> +</UI> diff --git a/libtdepim/alarmclient.cpp b/libtdepim/alarmclient.cpp new file mode 100644 index 000000000..5616cd029 --- /dev/null +++ b/libtdepim/alarmclient.cpp @@ -0,0 +1,60 @@ +/* + This file is part of KOrganizer. + + Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> + Copyright (c) 2005 David Faure <faure@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "alarmclient.h" + +#include <kapplication.h> +#include <kdebug.h> +#include <kstandarddirs.h> + +#include <dcopclient.h> +#include <dcopref.h> + +AlarmClient::AlarmClient() +{ + kdDebug(5850) << "AlarmClient::AlarmClient()" << endl; +} + +void AlarmClient::startDaemon() +{ + if ( kapp->dcopClient()->isApplicationRegistered( "korgac" ) ) { + // Alarm daemon already runs + return; + } + + KGlobal::dirs()->addResourceType("autostart", "share/autostart"); + TQString desktopFile = locate( "autostart", "korgac.desktop" ); + if ( desktopFile.isEmpty() ) { + kdWarning() << "Couldn't find autostart/korgac.desktop!" << endl; + } + else { + TQString error; + if ( kapp->startServiceByDesktopPath( desktopFile, TQStringList(), &error ) != 0 ) + kdWarning() << "Failure starting korgac:" << error << endl; + } +} + +void AlarmClient::stopDaemon() +{ + DCOPRef ref( "korgac", "ac" ); + ref.send( "quit" ); +} diff --git a/libtdepim/alarmclient.h b/libtdepim/alarmclient.h new file mode 100644 index 000000000..2d294143d --- /dev/null +++ b/libtdepim/alarmclient.h @@ -0,0 +1,44 @@ +/* + This file is part of the KOrganizer interfaces. + + Copyright (c) 2003 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef ALARMCLIENT_H +#define ALARMCLIENT_H + +/** + This class provides the interface for communicating with the alarm daemon. It + can be subclassed for specific daemons. +*/ +class AlarmClient +{ + public: + AlarmClient(); + + /** + Start alarm daemon. + */ + virtual void startDaemon(); + + /** + Stop alarm daemon. + */ + virtual void stopDaemon(); +}; + +#endif diff --git a/libtdepim/broadcaststatus.cpp b/libtdepim/broadcaststatus.cpp new file mode 100644 index 000000000..a50479195 --- /dev/null +++ b/libtdepim/broadcaststatus.cpp @@ -0,0 +1,165 @@ +/* + broadcaststatus.cpp + + This file is part of KDEPIM. + + Author: Don Sanders <sanders@kde.org> + + Copyright (C) 2000 Don Sanders <sanders@kde.org> + + License GPL +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <tqdatetime.h> + +#include <klocale.h> +#include <kglobal.h> +#include <kstaticdeleter.h> + +#include "broadcaststatus.h" +#include "progressmanager.h" + +KPIM::BroadcastStatus* KPIM::BroadcastStatus::instance_ = 0; +static KStaticDeleter<KPIM::BroadcastStatus> broadcasStatusDeleter; + +namespace KPIM { + +BroadcastStatus* BroadcastStatus::instance() +{ + if (!instance_) + broadcasStatusDeleter.setObject( instance_, new BroadcastStatus() ); + + return instance_; +} + +BroadcastStatus::BroadcastStatus() + :mTransientActive( false ) +{ +} + +BroadcastStatus::~BroadcastStatus() +{ + instance_ = 0; +} + +void BroadcastStatus::seStatusMsg( const TQString& message ) +{ + mStatusMsg = message; + if( !mTransientActive ) + emit statusMsg( message ); +} + +void BroadcastStatus::seStatusMsgWithTimestamp( const TQString& message ) +{ + KLocale* locale = KGlobal::locale(); + seStatusMsg( i18n( "%1 is a time, %2 is a status message", "[%1] %2" ) + .tqarg( locale->formatTime( TQTime::currentTime(), + true /* with seconds */ ) ) + .tqarg( message ) ); +} + +void BroadcastStatus::seStatusMsgTransmissionCompleted( int numMessages, + int numBytes, + int numBytesRead, + int numBytesToRead, + bool mLeaveOnServer, + KPIM::ProgressItem* item ) +{ + TQString statusMsg; + if( numMessages > 0 ) { + if( numBytes != -1 ) { + if( ( numBytesToRead != numBytes ) && mLeaveOnServer ) + statusMsg = i18n( "Transmission complete. %n new message in %1 KB " + "(%2 KB remaining on the server).", + "Transmission complete. %n new messages in %1 KB " + "(%2 KB remaining on the server).", + numMessages ) + .tqarg( numBytesRead / 1024 ) + .tqarg( numBytes / 1024 ); + else + statusMsg = i18n( "Transmission complete. %n message in %1 KB.", + "Transmission complete. %n messages in %1 KB.", + numMessages ) + .tqarg( numBytesRead / 1024 ); + } + else + statusMsg = i18n( "Transmission complete. %n new message.", + "Transmission complete. %n new messages.", + numMessages ); + } + else + statusMsg = i18n( "Transmission complete. No new messages." ); + + seStatusMsgWithTimestamp( statusMsg ); + if ( item ) + item->setqStatus( statusMsg ); +} + +void BroadcastStatus::seStatusMsgTransmissionCompleted( const TQString& account, + int numMessages, + int numBytes, + int numBytesRead, + int numBytesToRead, + bool mLeaveOnServer, + KPIM::ProgressItem* item ) +{ + TQString statusMsg; + if( numMessages > 0 ) { + if( numBytes != -1 ) { + if( ( numBytesToRead != numBytes ) && mLeaveOnServer ) + statusMsg = i18n( "Transmission for account %3 complete. " + "%n new message in %1 KB " + "(%2 KB remaining on the server).", + "Transmission for account %3 complete. " + "%n new messages in %1 KB " + "(%2 KB remaining on the server).", + numMessages ) + .tqarg( numBytesRead / 1024 ) + .tqarg( numBytes / 1024 ) + .tqarg( account ); + else + statusMsg = i18n( "Transmission for account %2 complete. " + "%n message in %1 KB.", + "Transmission for account %2 complete. " + "%n messages in %1 KB.", + numMessages ) + .tqarg( numBytesRead / 1024 ) + .tqarg( account ); + } + else + statusMsg = i18n( "Transmission for account %1 complete. " + "%n new message.", + "Transmission for account %1 complete. " + "%n new messages.", + numMessages ) + .tqarg( account ); + } + else + statusMsg = i18n( "Transmission for account %1 complete. No new messages.") + .tqarg( account ); + + seStatusMsgWithTimestamp( statusMsg ); + if ( item ) + item->setqStatus( statusMsg ); +} + +void BroadcastStatus::setTransienStatusMsg( const TQString& msg ) +{ + mTransientActive = true; + emit statusMsg( msg ); +} + +void BroadcastStatus::reset() +{ + mTransientActive = false; + // restore + emit statusMsg( mStatusMsg ); +} + +} + +#include "broadcaststatus.moc" diff --git a/libtdepim/broadcaststatus.h b/libtdepim/broadcaststatus.h new file mode 100644 index 000000000..c29e0ff05 --- /dev/null +++ b/libtdepim/broadcaststatus.h @@ -0,0 +1,94 @@ +/* + broadcaststatus.h + + This file is part of KDEPIM. + + Copyright (C) 2000 Don Sanders <sanders@kde.org> + + License GPL +*/ + +#ifndef __kpim_broadcast_status_h +#define __kpim_broadcast_status_h + +#include <tqobject.h> +#include <tqmap.h> + +#include <tdepimmacros.h> + +#undef None + +namespace KPIM { + +class ProgressItem; + +/** + Provides a singleton which broadcasts status messages by emitting + signals. Interested mainwindows can connect to the statusMsg() + signal and update statusBars or whatever they use for showing status. + */ + + +class KDE_EXPORT BroadcastStatus : public TQObject +{ + + Q_OBJECT + TQ_OBJECT + +public: + virtual ~BroadcastStatus(); + + /** Return the instance of the singleton object for this class */ + static BroadcastStatus *instance(); + + /** Return the last status message from seStatusMsg() */ + TQString statusMsg() const { return mStatusMsg; } + /** Sets a status bar message with timestamp */ + void seStatusMsgWithTimestamp( const TQString& message ); + /** Sets a transmission completed status bar message */ + void seStatusMsgTransmissionCompleted( int numMessages, + int numBytes = -1, + int numBytesRead = -1, + int numBytesToRead = -1, + bool mLeaveOnServer = false, + KPIM::ProgressItem* progressItem = 0 ); // set the same status in this progress item + void seStatusMsgTransmissionCompleted( const TQString& account, + int numMessages, + int numBytes = -1, + int numBytesRead = -1, + int numBytesToRead = -1, + bool mLeaveOnServer = false, + KPIM::ProgressItem* progressItem = 0 ); // set the same status in this progress item + +public slots: + /** Emit an update status bar signal. It's a slot so it can be hooked up + to other signals. */ + void seStatusMsg( const TQString& message ); + + /** + Set a status message that will go away again with the next call of + reset(). + */ + void setTransienStatusMsg( const TQString& msg ); + /** + Reset the status message to what ever non-transient message was last + active or has since been set. + */ + void reset(); + +signals: + + /** Emitted when seStatusMsg is called. */ + void statusMsg( const TQString& ); + +protected: + + BroadcastStatus(); + TQString mStatusMsg; + bool mTransientActive; + static BroadcastStatus* instance_; +}; + + +} +#endif diff --git a/libtdepim/calendardiffalgo.cpp b/libtdepim/calendardiffalgo.cpp new file mode 100644 index 000000000..587d0dc0c --- /dev/null +++ b/libtdepim/calendardiffalgo.cpp @@ -0,0 +1,213 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <klocale.h> + +#include "calendardiffalgo.h" + +using namespace KPIM; + +#ifndef KDE_USE_FINAL +static bool compareString( const TQString &left, const TQString &right ) +{ + if ( left.isEmpty() && right.isEmpty() ) + return true; + else + return left == right; +} +#endif + +static TQString toString( KCal::Attendee *attendee ) +{ + return attendee->name() + "<" + attendee->email() + ">"; +} + +static TQString toString( KCal::Alarm * ) +{ + return TQString(); +} + +static TQString toString( KCal::Incidence * ) +{ + return TQString(); +} + +static TQString toString( KCal::Attachment * ) +{ + return TQString(); +} + +static TQString toString( const TQDate &date ) +{ + return date.toString(); +} + +static TQString toString( const TQDateTime &dateTime ) +{ + return dateTime.toString(); +} + +static TQString toString( const TQString str ) +{ + return str; +} + +static TQString toString( bool value ) +{ + if ( value ) + return i18n( "Yes" ); + else + return i18n( "No" ); +} + +CalendarDiffAlgo::CalendarDiffAlgo( KCal::Incidence *leftIncidence, + KCal::Incidence *rightIncidence ) + : mLeftIncidence( leftIncidence ), mRightIncidence( rightIncidence ) +{ +} + +void CalendarDiffAlgo::run() +{ + begin(); + + diffIncidenceBase( mLeftIncidence, mRightIncidence ); + diffIncidence( mLeftIncidence, mRightIncidence ); + + KCal::Event *leftEvent = dynamic_cast<KCal::Event*>( mLeftIncidence ); + KCal::Event *rightEvent = dynamic_cast<KCal::Event*>( mRightIncidence ); + if ( leftEvent && rightEvent ) { + diffEvent( leftEvent, rightEvent ); + } else { + KCal::Todo *leftTodo = dynamic_cast<KCal::Todo*>( mLeftIncidence ); + KCal::Todo *rightTodo = dynamic_cast<KCal::Todo*>( mRightIncidence ); + if ( leftTodo && rightTodo ) { + diffTodo( leftTodo, rightTodo ); + } + } + + end(); +} + +void CalendarDiffAlgo::diffIncidenceBase( KCal::IncidenceBase *left, KCal::IncidenceBase *right ) +{ + diffList( i18n( "Attendees" ), left->attendees(), right->attendees() ); + + if ( left->dtStart() != right->dtStart() ) + conflictField( i18n( "Start time" ), left->dtStartStr(), right->dtStartStr() ); + + if ( !compareString( left->organizer().fullName(), right->organizer().fullName() ) ) + conflictField( i18n( "Organizer" ), left->organizer().fullName(), right->organizer().fullName() ); + + if ( !compareString( left->uid(), right->uid() ) ) + conflictField( i18n( "UID" ), left->uid(), right->uid() ); + + if ( left->doesFloat() != right->doesFloat() ) + conflictField( i18n( "Is floating" ), toString( left->doesFloat() ), toString( right->doesFloat() ) ); + + if ( left->hasDuration() != right->hasDuration() ) + conflictField( i18n( "Has duration" ), toString( left->hasDuration() ), toString( right->hasDuration() ) ); + + if ( left->duration() != right->duration() ) + conflictField( i18n( "Duration" ), TQString::number( left->duration() ), TQString::number( right->duration() ) ); +} + +void CalendarDiffAlgo::diffIncidence( KCal::Incidence *left, KCal::Incidence *right ) +{ + if ( !compareString( left->description(), right->description() ) ) + conflictField( i18n( "Description" ), left->description(), right->description() ); + + if ( !compareString( left->summary(), right->summary() ) ) + conflictField( i18n( "Summary" ), left->summary(), right->summary() ); + + if ( left->status() != right->status() ) + conflictField( i18n( "Status" ), left->statusStr(), right->statusStr() ); + + if ( left->secrecy() != right->secrecy() ) + conflictField( i18n( "Secrecy" ), toString( left->secrecy() ), toString( right->secrecy() ) ); + + if ( left->priority() != right->priority() ) + conflictField( i18n( "Priority" ), toString( left->priority() ), toString( right->priority() ) ); + + if ( !compareString( left->location(), right->location() ) ) + conflictField( i18n( "Location" ), left->location(), right->location() ); + + diffList( i18n( "Categories" ), left->categories(), right->categories() ); + diffList( i18n( "Alarms" ), left->alarms(), right->alarms() ); + diffList( i18n( "Resources" ), left->resources(), right->resources() ); + diffList( i18n( "Relations" ), left->relations(), right->relations() ); + diffList( i18n( "Attachments" ), left->attachments(), right->attachments() ); + diffList( i18n( "Exception Dates" ), left->recurrence()->exDates(), right->recurrence()->exDates() ); + diffList( i18n( "Exception Times" ), left->recurrence()->exDateTimes(), right->recurrence()->exDateTimes() ); + // TODO: recurrence dates and date/times, exrules, rrules + + if ( left->created() != right->created() ) + conflictField( i18n( "Created" ), left->created().toString(), right->created().toString() ); + + if ( !compareString( left->relatedToUid(), right->relatedToUid() ) ) + conflictField( i18n( "Related Uid" ), left->relatedToUid(), right->relatedToUid() ); +} + +void CalendarDiffAlgo::diffEvent( KCal::Event *left, KCal::Event *right ) +{ + if ( left->hasEndDate() != right->hasEndDate() ) + conflictField( i18n( "Has End Date" ), toString( left->hasEndDate() ), toString( right->hasEndDate() ) ); + + if ( left->dtEnd() != right->dtEnd() ) + conflictField( i18n( "End Date" ), left->dtEndStr(), right->dtEndStr() ); + + // TODO: check transparency +} + +void CalendarDiffAlgo::diffTodo( KCal::Todo *left, KCal::Todo *right ) +{ + if ( left->hasStartDate() != right->hasStartDate() ) + conflictField( i18n( "Has Start Date" ), toString( left->hasStartDate() ), toString( right->hasStartDate() ) ); + + if ( left->hasDueDate() != right->hasDueDate() ) + conflictField( i18n( "Has Due Date" ), toString( left->hasDueDate() ), toString( right->hasDueDate() ) ); + + if ( left->dtDue() != right->dtDue() ) + conflictField( i18n( "Due Date" ), left->dtDue().toString(), right->dtDue().toString() ); + + if ( left->hasCompletedDate() != right->hasCompletedDate() ) + conflictField( i18n( "Has Complete Date" ), toString( left->hasCompletedDate() ), toString( right->hasCompletedDate() ) ); + + if ( left->percentComplete() != right->percentComplete() ) + conflictField( i18n( "Complete" ), TQString::number( left->percentComplete() ), TQString::number( right->percentComplete() ) ); + + if ( left->completed() != right->completed() ) + conflictField( i18n( "Completed" ), toString( left->completed() ), toString( right->completed() ) ); +} + +template <class L> +void CalendarDiffAlgo::diffList( const TQString &id, + const TQValueList<L> &left, const TQValueList<L> &right ) +{ + for ( uint i = 0; i < left.count(); ++i ) { + if ( right.find( left[ i ] ) == right.end() ) + additionalLeftField( id, toString( left[ i ] ) ); + } + + for ( uint i = 0; i < right.count(); ++i ) { + if ( left.find( right[ i ] ) == left.end() ) + additionalRightField( id, toString( right[ i ] ) ); + } +} diff --git a/libtdepim/calendardiffalgo.h b/libtdepim/calendardiffalgo.h new file mode 100644 index 000000000..9ccd6c8c0 --- /dev/null +++ b/libtdepim/calendardiffalgo.h @@ -0,0 +1,54 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef KPIM_CALENDARDIFFALGO_H +#define KPIM_CALENDARDIFFALGO_H + +#include "diffalgo.h" + +#include <libkcal/event.h> +#include <libkcal/todo.h> + +namespace KPIM { + +class KDE_EXPORT CalendarDiffAlgo : public DiffAlgo +{ + public: + CalendarDiffAlgo( KCal::Incidence *leftIncidence, KCal::Incidence *rightIncidence ); + + void run(); + + private: + template <class L> + void diffList( const TQString &id, const TQValueList<L> &left, const TQValueList<L> &right ); + + void diffIncidenceBase( KCal::IncidenceBase*, KCal::IncidenceBase* ); + void diffIncidence( KCal::Incidence*, KCal::Incidence* ); + void diffEvent( KCal::Event*, KCal::Event* ); + void diffTodo( KCal::Todo*, KCal::Todo* ); + + KCal::Incidence *mLeftIncidence; + KCal::Incidence *mRightIncidence; +}; + +} + +#endif diff --git a/libtdepim/categoryeditdialog.cpp b/libtdepim/categoryeditdialog.cpp new file mode 100644 index 000000000..72e1de8ae --- /dev/null +++ b/libtdepim/categoryeditdialog.cpp @@ -0,0 +1,193 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2000, 2001, 2002 Cornelius Schumacher <schumacher@kde.org> + Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <tqstringlist.h> +#include <tqlineedit.h> +#include <tqlistview.h> +#include <tqlayout.h> +#include <tqheader.h> +#include <tqpushbutton.h> +#include <klocale.h> + +#include "kpimprefs.h" + +#include "categoryeditdialog.h" + +using namespace KPIM; + +class CategoryEditDialog::Private +{ + public: + TQListView *mView; + TQPushButton *mAddButton; + TQPushButton *mEditButton; + TQPushButton *mDeleteButton; +}; + +class CategoryListViewItem : public TQListViewItem +{ + public: + CategoryListViewItem( TQListView *view, const TQString &text ) : + TQListViewItem( view, text ) + { + } + + void okRename ( int col ) // we need that public to explicitly accept renaming when closing the dialog + { + TQListViewItem::okRename( col ); + } +}; + +CategoryEditDialog::CategoryEditDialog( KPimPrefs *prefs, TQWidget* parent, + const char* name, bool modal ) + : KDialogBase::KDialogBase( parent, name, modal, + i18n("Edit Categories"), Ok|Apply|Cancel|Help, Ok, true ), + mPrefs( prefs ), d( new Private ) +{ + TQWidget *widget = new TQWidget( this ); + setMainWidget( widget ); + + TQGridLayout *tqlayout = new TQGridLayout( widget, 4, 2, marginHint(), spacingHint() ); + + d->mView = new TQListView( widget ); + d->mView->addColumn( "" ); + d->mView->header()->hide(); + d->mView->setDefaultRenameAction( TQListView::Accept ); + + tqlayout->addMultiCellWidget( d->mView, 0, 3, 0, 0 ); + + d->mAddButton = new TQPushButton( i18n( "Add" ), widget ); + tqlayout->addWidget( d->mAddButton, 0, 1 ); + + d->mEditButton = new TQPushButton( i18n( "Edit" ), widget ); + tqlayout->addWidget( d->mEditButton, 1, 1 ); + + d->mDeleteButton = new TQPushButton( i18n( "Remove" ), widget ); + tqlayout->addWidget( d->mDeleteButton, 2, 1 ); + + + fillList(); + + connect( d->mAddButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( add() ) ); + connect( d->mEditButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( edit() ) ); + connect( d->mDeleteButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( remove() ) ); +} + +/* + * Destroys the object and frees any allocated resources + */ +CategoryEditDialog::~CategoryEditDialog() +{ + delete d; +} + +void CategoryEditDialog::fillList() +{ + d->mView->clear(); + TQStringList::Iterator it; + bool categoriesExist=false; + for ( it = mPrefs->mCustomCategories.begin(); + it != mPrefs->mCustomCategories.end(); ++it ) { + + TQListViewItem *item = new CategoryListViewItem( d->mView, *it ); + item->setRenameEnabled( 0, true ); + + categoriesExist = true; + } + + d->mEditButton->setEnabled( categoriesExist ); + d->mDeleteButton->setEnabled( categoriesExist ); + d->mView->setSelected( d->mView->firstChild(), true ); +} + +void CategoryEditDialog::add() +{ + if ( d->mView->firstChild() ) + d->mView->setCurrentItem( d->mView->firstChild() ); + + TQListViewItem *item = new CategoryListViewItem( d->mView, i18n( "New category" ) ); + item->setRenameEnabled( 0, true ); + + d->mView->setSelected( item, true ); + d->mView->ensureItemVisible( item ); + item->startRename( 0 ); + + bool itemCount = d->mView->childCount() > 0; + d->mEditButton->setEnabled( itemCount ); + d->mDeleteButton->setEnabled( itemCount ); +} + +void CategoryEditDialog::edit() +{ + if ( d->mView->currentItem() ) + d->mView->currentItem()->startRename( 0 ); +} + +void CategoryEditDialog::remove() +{ + if ( d->mView->currentItem() ) { + delete d->mView->currentItem(); + + d->mView->setSelected( d->mView->currentItem(), true ); + + bool itemCount = d->mView->childCount() > 0; + d->mEditButton->setEnabled( itemCount ); + d->mDeleteButton->setEnabled( itemCount ); + } +} + +void CategoryEditDialog::slotOk() +{ + // accept the currently ongoing rename + if ( d->mView->selectedItem() ) + static_cast<CategoryListViewItem*>( d->mView->selectedItem() )->okRename( 0 ); + slotApply(); + accept(); +} + +void CategoryEditDialog::slotApply() +{ + mPrefs->mCustomCategories.clear(); + + TQListViewItem *item = d->mView->firstChild(); + while ( item ) { + if ( !item->text( 0 ).isEmpty() ) + mPrefs->mCustomCategories.append( item->text( 0 ) ); + item = item->nextSibling(); + } + mPrefs->writeConfig(); + + emit categoryConfigChanged(); +} + +void CategoryEditDialog::slotCancel() +{ + reload(); + KDialogBase::slotCancel(); +} + +void CategoryEditDialog::reload() +{ + fillList(); +} + +#include "categoryeditdialog.moc" diff --git a/libtdepim/categoryeditdialog.h b/libtdepim/categoryeditdialog.h new file mode 100644 index 000000000..064f8b682 --- /dev/null +++ b/libtdepim/categoryeditdialog.h @@ -0,0 +1,67 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2000, 2001, 2002 Cornelius Schumacher <schumacher@kde.org> + Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef KPIM_CATEGORYEDITDIALOG_H +#define KPIM_CATEGORYEDITDIALOG_H + +#include <kdialogbase.h> +#include <tdepimmacros.h> + +class KPimPrefs; + +namespace KPIM { + +class KDE_EXPORT CategoryEditDialog : public KDialogBase +{ + Q_OBJECT + TQ_OBJECT + public: + CategoryEditDialog( KPimPrefs *prefs, TQWidget* parent = 0, + const char* name = 0, + bool modal = false ); + ~CategoryEditDialog(); + + public slots: + void reload(); + + protected slots: + void slotOk(); + void slotApply(); + void slotCancel(); + void add(); + void edit(); + void remove(); + + signals: + void categoryConfigChanged(); + + protected: + void fillList(); + + private: + KPimPrefs *mPrefs; + class Private; + Private* const d; +}; + +} + +#endif diff --git a/libtdepim/categoryeditdialog_base.ui b/libtdepim/categoryeditdialog_base.ui new file mode 100644 index 000000000..11cd98740 --- /dev/null +++ b/libtdepim/categoryeditdialog_base.ui @@ -0,0 +1,107 @@ +<!DOCTYPE UI><UI version="3.3" stdsetdef="1"> +<class>CategoryEditDialog_base</class> +<widget class="TQWidget"> + <property name="name"> + <cstring>CategoryEditDialog_base</cstring> + </property> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>386</width> + <height>270</height> + </rect> + </property> + <property name="caption"> + <string>Edit Categories</string> + </property> + <grid> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>0</number> + </property> + <property name="spacing"> + <number>6</number> + </property> + <widget class="TQLineEdit" row="1" column="0" rowspan="1" colspan="2"> + <property name="name"> + <cstring>mEdit</cstring> + </property> + </widget> + <widget class="TQListView" row="0" column="0"> + <column> + <property name="text"> + <string>Category</string> + </property> + <property name="clickable"> + <bool>true</bool> + </property> + <property name="resizable"> + <bool>true</bool> + </property> + </column> + <property name="name"> + <cstring>mCategories</cstring> + </property> + <property name="allColumnsShowFocus"> + <bool>true</bool> + </property> + <property name="resizeMode"> + <enum>AllColumns</enum> + </property> + </widget> + <widget class="TQLayoutWidget" row="0" column="1"> + <property name="name"> + <cstring>tqlayout103</cstring> + </property> + <vbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <widget class="TQPushButton"> + <property name="name"> + <cstring>mButtonAdd</cstring> + </property> + <property name="text"> + <string>A&dd</string> + </property> + </widget> + <widget class="TQPushButton"> + <property name="name"> + <cstring>mButtonRemove</cstring> + </property> + <property name="text"> + <string>&Remove</string> + </property> + </widget> + <spacer> + <property name="name"> + <cstring>Spacer3</cstring> + </property> + <property name="orientation"> + <enum>Vertical</enum> + </property> + <property name="sizeType"> + <enum>Expanding</enum> + </property> + <property name="tqsizeHint"> + <size> + <width>20</width> + <height>20</height> + </size> + </property> + </spacer> + </vbox> + </widget> + </grid> +</widget> +<tabstops> + <tabstop>mCategories</tabstop> + <tabstop>mEdit</tabstop> + <tabstop>mButtonAdd</tabstop> + <tabstop>mButtonRemove</tabstop> +</tabstops> +<layoutdefaults spacing="6" margin="11"/> +</UI> diff --git a/libtdepim/categoryselectdialog.cpp b/libtdepim/categoryselectdialog.cpp new file mode 100644 index 000000000..36996614e --- /dev/null +++ b/libtdepim/categoryselectdialog.cpp @@ -0,0 +1,146 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2000, 2001, 2002 Cornelius Schumacher <schumacher@kde.org> + Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <tqlistview.h> +#include <tqpushbutton.h> +#include <tqheader.h> + +#include "categoryselectdialog_base.h" +#include <klocale.h> +#include "categoryselectdialog.h" + +#include "kpimprefs.h" + +using namespace KPIM; + +CategorySelectDialog::CategorySelectDialog( KPimPrefs *prefs, TQWidget* parent, + const char* name, bool modal ) + : KDialogBase::KDialogBase( parent, name, modal, + i18n("Select Categories"), Ok|Apply|Cancel|Help, Ok, true ), + mPrefs( prefs ) +{ + mWidget = new CategorySelectDialog_base( this, "CategorySelection" ); + mWidget->mCategories->header()->hide(); + setMainWidget( mWidget ); + + setCategories(); + + connect( mWidget->mButtonEdit, TQT_SIGNAL(clicked()), + TQT_SIGNAL(editCategories()) ); + connect( mWidget->mButtonClear, TQT_SIGNAL(clicked()), + TQT_SLOT(clear()) ); +} + +void CategorySelectDialog::setCategories( const TQStringList &categoryList ) +{ + mWidget->mCategories->clear(); + mCategoryList.clear(); + + TQStringList::ConstIterator it; + + for ( it = categoryList.begin(); it != categoryList.end(); ++it ) + if ( mPrefs->mCustomCategories.find( *it ) == mPrefs->mCustomCategories.end() ) + mPrefs->mCustomCategories.append( *it ); + + for ( it = mPrefs->mCustomCategories.begin(); + it != mPrefs->mCustomCategories.end(); ++it ) { + new TQCheckListItem( mWidget->mCategories, *it, TQCheckListItem::CheckBox ); + } +} + +CategorySelectDialog::~CategorySelectDialog() +{ +} + +void CategorySelectDialog::setSelected(const TQStringList &selList) +{ + clear(); + + TQStringList::ConstIterator it; + for ( it = selList.begin(); it != selList.end(); ++it ) { + TQCheckListItem *item = (TQCheckListItem *)mWidget->mCategories->firstChild(); + while (item) { + if (item->text() == *it) { + item->setOn(true); + break; + } + item = (TQCheckListItem *)item->nextSibling(); + } + } +} + +TQStringList CategorySelectDialog::selectedCategories() const +{ + return mCategoryList; +} + +void CategorySelectDialog::slotApply() +{ + TQStringList categories; + TQCheckListItem *item = (TQCheckListItem *)mWidget->mCategories->firstChild(); + while (item) { + if (item->isOn()) { + categories.append(item->text()); + } + item = (TQCheckListItem *)item->nextSibling(); + } + + TQString categoriesStr = categories.join(", "); + + mCategoryList = categories; + + emit categoriesSelected(categories); + emit categoriesSelected(categoriesStr); +} + +void CategorySelectDialog::slotOk() +{ + slotApply(); + accept(); +} + +void CategorySelectDialog::clear() +{ + TQCheckListItem *item = (TQCheckListItem *)mWidget->mCategories->firstChild(); + while (item) { + item->setOn(false); + item = (TQCheckListItem *)item->nextSibling(); + } +} + +void CategorySelectDialog::updateCategoryConfig() +{ + TQStringList selected; + TQCheckListItem *item = (TQCheckListItem *)mWidget->mCategories->firstChild(); + while (item) { + if (item->isOn()) { + selected.append(item->text()); + } + item = (TQCheckListItem *)item->nextSibling(); + } + + setCategories(); + + setSelected(selected); +} + +#include "categoryselectdialog.moc" diff --git a/libtdepim/categoryselectdialog.h b/libtdepim/categoryselectdialog.h new file mode 100644 index 000000000..3cff0313c --- /dev/null +++ b/libtdepim/categoryselectdialog.h @@ -0,0 +1,72 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2000, 2001, 2002 Cornelius Schumacher <schumacher@kde.org> + Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef KPIM_CATEGORYSELECTDIALOG_H +#define KPIM_CATEGORYSELECTDIALOG_H + +#include <kdialogbase.h> +#include <tdepimmacros.h> + +class KPimPrefs; +class CategorySelectDialog_base; + +namespace KPIM { + +class KDE_EXPORT CategorySelectDialog : public KDialogBase +{ + Q_OBJECT + TQ_OBJECT + public: + CategorySelectDialog( KPimPrefs *prefs, TQWidget *parent = 0, + const char *name = 0, bool modal = false ); + ~CategorySelectDialog(); + + /** + Adds this categories to the default categories. + */ + void setCategories( const TQStringList &categoryList = TQStringList() ); + void setSelected( const TQStringList &selList ); + + TQStringList selectedCategories() const; + + public slots: + void slotOk(); + void slotApply(); + void clear(); + void updateCategoryConfig(); + + signals: + void categoriesSelected( const TQString & ); + void categoriesSelected( const TQStringList & ); + void editCategories(); + + private: + KPimPrefs *mPrefs; + CategorySelectDialog_base *mWidget; + TQStringList mCategoryList; + + class CategorySelectDialogPrivate; + CategorySelectDialogPrivate *d; +}; + +} + +#endif diff --git a/libtdepim/categoryselectdialog_base.ui b/libtdepim/categoryselectdialog_base.ui new file mode 100644 index 000000000..1e45bd468 --- /dev/null +++ b/libtdepim/categoryselectdialog_base.ui @@ -0,0 +1,101 @@ +<!DOCTYPE UI><UI version="3.2" stdsetdef="1"> +<class>CategorySelectDialog_base</class> +<widget class="TQWidget"> + <property name="name"> + <cstring>CategorySelectDialog_base</cstring> + </property> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>387</width> + <height>280</height> + </rect> + </property> + <property name="caption"> + <string>Select Categories</string> + </property> + <vbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>0</number> + </property> + <property name="spacing"> + <number>6</number> + </property> + <widget class="TQListView"> + <column> + <property name="text"> + <string>Category</string> + </property> + <property name="clickable"> + <bool>true</bool> + </property> + <property name="resizable"> + <bool>true</bool> + </property> + </column> + <property name="name"> + <cstring>mCategories</cstring> + </property> + </widget> + <widget class="TQLayoutWidget"> + <property name="name"> + <cstring>Layout12</cstring> + </property> + <hbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>0</number> + </property> + <property name="spacing"> + <number>6</number> + </property> + <widget class="TQPushButton"> + <property name="name"> + <cstring>mButtonClear</cstring> + </property> + <property name="text"> + <string>&Clear Selection</string> + </property> + </widget> + <spacer> + <property name="name"> + <cstring>Spacer3</cstring> + </property> + <property name="orientation"> + <enum>Horizontal</enum> + </property> + <property name="sizeType"> + <enum>Expanding</enum> + </property> + <property name="tqsizeHint"> + <size> + <width>20</width> + <height>20</height> + </size> + </property> + </spacer> + <widget class="TQPushButton"> + <property name="name"> + <cstring>mButtonEdit</cstring> + </property> + <property name="text"> + <string>&Edit Categories...</string> + </property> + </widget> + </hbox> + </widget> + </vbox> +</widget> +<tabstops> + <tabstop>mCategories</tabstop> + <tabstop>mButtonClear</tabstop> + <tabstop>mButtonEdit</tabstop> +</tabstops> +<layoutdefaults spacing="6" margin="11"/> +</UI> diff --git a/libtdepim/cfgc/Makefile.am b/libtdepim/cfgc/Makefile.am new file mode 100644 index 000000000..ec9f505f0 --- /dev/null +++ b/libtdepim/cfgc/Makefile.am @@ -0,0 +1,14 @@ +AM_CPPFLAGS = -I$(top_builddir)/libtdepim -I$(top_srcdir) $(all_includes) + +check_PROGRAMS = example +#autoexample + +example_LDFLAGS = $(all_libraries) $(KDE_RPATH) $(LIB_QT) -lDCOP $(LIB_KDECORE) $(LIB_KDEUI) -ltdefx $(LIB_KIO) -lktexteditor +example_LDADD = ../libtdepim.la $(LIB_KDECORE) +example_SOURCES = example.cpp exampleprefs_base.kcfgc + +#autoexample_LDFLAGS = $(all_libraries) $(KDE_RPATH) $(LIB_QT) -lDCOP $(LIB_KDECORE) $(LIB_KDEUI) -ltdefx $(LIB_KIO) -lktexteditor +#autoexample_LDADD = ../libtdepim.la $(LIB_KDECORE) +#autoexample_SOURCES = general_base.ui myoptions_base.ui autoexample.cpp + +METASOURCES = AUTO diff --git a/libtdepim/cfgc/README b/libtdepim/cfgc/README new file mode 100644 index 000000000..6a7783817 --- /dev/null +++ b/libtdepim/cfgc/README @@ -0,0 +1,6 @@ +This directory contains text and example code for automatic creation of +configuration dialogs based on KConfigSkeleton, kconfig_compiler and +KPrefsDialog. + +If you have questions or comments please contact Cornelius Schumacher +<schumacher@kde.org>. diff --git a/libtdepim/cfgc/autoexample.cpp b/libtdepim/cfgc/autoexample.cpp new file mode 100644 index 000000000..45617e6f6 --- /dev/null +++ b/libtdepim/cfgc/autoexample.cpp @@ -0,0 +1,66 @@ +/* + This file is part of KDE. + + Copyright (c) 2003 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "general_base.h" +#include "myoptions_base.h" + +#include <libtdepim/kprefsdialog.h> + +#include <kaboutdata.h> +#include <kapplication.h> +#include <kdebug.h> +#include <klocale.h> +#include <kcmdlineargs.h> +#include <kglobal.h> +#include <kconfig.h> +#include <kstandarddirs.h> +#include <kautoconfigdialog.h> + +#include <tqlabel.h> + +int main( int argc, char **argv ) +{ + KAboutData aboutData( "example", I18N_NOOP("autoconfig example"), "0.1" ); + aboutData.addAuthor( "Cornelius Schumacher", 0, "schumacher@kde.org" ); + + KCmdLineArgs::init( argc, argv, &aboutData ); + + KApplication app; + + // Create a new dialog with the same name as the above checking code. + KAutoConfigDialog *dialog = new KAutoConfigDialog(0, "settings"); + + // Add the general page. Store the settings in the General group and + // use the icon package_settings. + GeneralBase *general = new GeneralBase( 0 ); + dialog->addPage( general, i18n("General"), "General", "" ); + + MyOptionsBase *myOptions = new MyOptionsBase( 0 ); + +// myOptions->show(); + dialog->addPage( myOptions, i18n("MyOptions"), "MyOptions", "" ); + +// app.setMainWidget( dialog ); + + dialog->show(); + + return app.exec(); +} diff --git a/libtdepim/cfgc/example.cfg b/libtdepim/cfgc/example.cfg new file mode 100644 index 000000000..18c7b8b15 --- /dev/null +++ b/libtdepim/cfgc/example.cfg @@ -0,0 +1,40 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE cfg SYSTEM "cfg.dtd"> +<cfg> + <cfgfile name="examplerc"/> + <class name="ExamplePrefsBase" file="exampleprefs_base" singleton="true"> + <include file="libtdepim/kpimprefs.h" /> + </class> + <group name="General"> + <entry type="bool"> + <name>OneOption</name> + <label>One option</label> + <default>true</default> + </entry> + <entry type="int"> + <key>Another Option</key> + <label>Another option</label> + <default>5</default> + </entry> + <entry type="int"> + <values> + <value>One</value> + <value>Two</value> + <value>Three</value> + </values> + <name>ListOption</name> + <default>One</default> + </entry> + </group> + <group name="MyOptions"> + <entry type=TQSTRING_OBJECT_NAME_STRING> + <name>MyString</name> + <label>This is a string</label> + <default>Default String</default> + </entry> + <entry type=TQSTRINGLIST_OBJECT_NAME_STRING> + <name>MyStringList</name> + <default>up,down</default> + </entry> + </group> +</cfg> diff --git a/libtdepim/cfgc/example.cpp b/libtdepim/cfgc/example.cpp new file mode 100644 index 000000000..4f818e900 --- /dev/null +++ b/libtdepim/cfgc/example.cpp @@ -0,0 +1,55 @@ +/* + This file is part of KDE. + + Copyright (c) 2003 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "exampleprefs_base.h" + +#include <kaboutdata.h> +#include <kapplication.h> +#include <kdebug.h> +#include <klocale.h> +#include <kcmdlineargs.h> +#include <kglobal.h> +#include <kconfig.h> +#include <kstandarddirs.h> + +#include <libtdepim/kprefsdialog.h> + +int main( int argc, char **argv ) +{ + KAboutData aboutData( "example", I18N_NOOP("cfgc example"), "0.1" ); + aboutData.addAuthor( "Cornelius Schumacher", 0, "schumacher@kde.org" ); + + KCmdLineArgs::init( argc, argv, &aboutData ); + + KApplication app; + + ExamplePrefsBase *prefs = ExamplePrefsBase::self(); + + KPrefsDialog *dialog = new KPrefsDialog( prefs ); + + dialog->autoCreate(); + + app.setMainWidget( dialog ); + + dialog->show(); + + return app.exec(); +} diff --git a/libtdepim/cfgc/exampleprefs_base.kcfg b/libtdepim/cfgc/exampleprefs_base.kcfg new file mode 100644 index 000000000..e197ae7cd --- /dev/null +++ b/libtdepim/cfgc/exampleprefs_base.kcfg @@ -0,0 +1,42 @@ +<?xml version="1.0" encoding="UTF-8"?> +<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0 + http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" > + <kcfgfile name="examplerc"/> + <group name="General-$(folder)"> + <entry name="OneOption" type="Bool"> + <label>One option</label> + <default>true</default> + </entry> + <entry name="AnotherOption" type="Int" key="Another Option"> + <label>Another option</label> + <default>5</default> + </entry> + <entry name="ListOption" type="Enum"> + <label>This is some funky option</label> + <whatsthis>And this is a longer description of this option. Just wondering, how will the translations of those be handled?</whatsthis> + <choices> + <choice name="One"> + <label>One</label> + </choice> + <choice name="Two"> + <label>Two</label> + </choice> + <choice name="Three"> + <label>Three</label> + </choice> + </choices> + <default>One</default> + </entry> + </group> + <group name="MyOptions"> + <entry name="MyString" type="String"> + <label>This is a string</label> + <default>Default String</default> + </entry> + <entry name="MyStringList" type="StringList"> + <default>up,down</default> + </entry> + </group> +</kcfg> diff --git a/libtdepim/cfgc/exampleprefs_base.kcfgc b/libtdepim/cfgc/exampleprefs_base.kcfgc new file mode 100644 index 000000000..36e5ed4e6 --- /dev/null +++ b/libtdepim/cfgc/exampleprefs_base.kcfgc @@ -0,0 +1,11 @@ +# Code generation options for kconfig_compiler +File=exampleprefs_base.kcfg +ClassName=ExamplePrefsBase +Singleton=true +Mutators=true +Inherits=KPimPrefs +IncludeFiles=libtdepim/kpimprefs.h +MemberVariables=public +GlobalEnums=true +ItemAccessors=true +SetUserTexts=true diff --git a/libtdepim/cfgc/general_base.ui b/libtdepim/cfgc/general_base.ui new file mode 100644 index 000000000..7cafaa011 --- /dev/null +++ b/libtdepim/cfgc/general_base.ui @@ -0,0 +1,46 @@ +<!DOCTYPE UI><UI version="3.1" stdsetdef="1"> +<class>GeneralBase</class> +<widget class="TQWidget"> + <property name="name"> + <cstring>GeneralBase</cstring> + </property> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>600</width> + <height>480</height> + </rect> + </property> + <property name="caption"> + <string>AutoExampleDialog</string> + </property> + <grid> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <widget class="TQCheckBox" row="0" column="1"> + <property name="name"> + <cstring>OneOption</cstring> + </property> + <property name="text"> + <string>OneOption</string> + </property> + </widget> + <widget class="TQSpinBox" row="1" column="1"> + <property name="name"> + <cstring>Another_Option</cstring> + </property> + </widget> + <widget class="TQLabel" row="1" column="0"> + <property name="name"> + <cstring>textLabel1</cstring> + </property> + <property name="text"> + <string>AnotherOption:</string> + </property> + </widget> + </grid> +</widget> +<layoutdefaults spacing="6" margin="11"/> +</UI> diff --git a/libtdepim/cfgc/myoptions_base.ui b/libtdepim/cfgc/myoptions_base.ui new file mode 100644 index 000000000..4d4f8247f --- /dev/null +++ b/libtdepim/cfgc/myoptions_base.ui @@ -0,0 +1,46 @@ +<!DOCTYPE UI><UI version="3.1" stdsetdef="1"> +<class>MyOptionsBase</class> +<widget class="TQWidget"> + <property name="name"> + <cstring>MyOptionsBase</cstring> + </property> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>600</width> + <height>480</height> + </rect> + </property> + <widget class="TQLabel"> + <property name="name"> + <cstring>textLabel1</cstring> + </property> + <property name="geometry"> + <rect> + <x>30</x> + <y>180</y> + <width>70</width> + <height>20</height> + </rect> + </property> + <property name="text"> + <string>MyString:</string> + </property> + </widget> + <widget class="TQLineEdit"> + <property name="name"> + <cstring>MyString</cstring> + </property> + <property name="geometry"> + <rect> + <x>130</x> + <y>180</y> + <width>123</width> + <height>22</height> + </rect> + </property> + </widget> +</widget> +<layoutdefaults spacing="6" margin="11"/> +</UI> diff --git a/libtdepim/clicklineedit.cpp b/libtdepim/clicklineedit.cpp new file mode 100644 index 000000000..28e66a0ba --- /dev/null +++ b/libtdepim/clicklineedit.cpp @@ -0,0 +1,85 @@ +/* + This file is part of libtdepim. + Copyright (c) 2004 Daniel Molkentin <molkentin@kde.org> + based on code by Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "clicklineedit.h" + +#include "tqpainter.h" + +using namespace KPIM; + +ClickLineEdit::ClickLineEdit(TQWidget *parent, const TQString &msg, const char* name) : + KLineEdit(parent, name) +{ + mDrawClickMsg = true; + setClickMessage( msg ); +} + +ClickLineEdit::~ClickLineEdit() {} + + +void ClickLineEdit::setClickMessage( const TQString &msg ) +{ + mClickMessage = msg; + tqrepaint(); +} + +void ClickLineEdit::setText( const TQString &txt ) +{ + mDrawClickMsg = txt.isEmpty(); + tqrepaint(); + KLineEdit::setText( txt ); +} + +void ClickLineEdit::drawContents( TQPainter *p ) +{ + KLineEdit::drawContents( p ); + + if ( mDrawClickMsg == true && !hasFocus() ) { + TQPen tmp = p->pen(); + p->setPen( palette().color( TQPalette::Disabled, TQColorGroup::Text ) ); + TQRect cr = contentsRect(); + p->drawText( cr, AlignAuto|AlignVCenter, mClickMessage ); + p->setPen( tmp ); + } +} + +void ClickLineEdit::focusInEvent( TQFocusEvent *ev ) +{ + if ( mDrawClickMsg == true ) + { + mDrawClickMsg = false; + tqrepaint(); + } + TQLineEdit::focusInEvent( ev ); +} + +void ClickLineEdit::focusOutEvent( TQFocusEvent *ev ) +{ + if ( text().isEmpty() ) + { + mDrawClickMsg = true; + tqrepaint(); + } + TQLineEdit::focusOutEvent( ev ); +} + +#include "clicklineedit.moc" diff --git a/libtdepim/clicklineedit.h b/libtdepim/clicklineedit.h new file mode 100644 index 000000000..28b771ff5 --- /dev/null +++ b/libtdepim/clicklineedit.h @@ -0,0 +1,65 @@ +/* + This file is part of libtdepim. + Copyright (c) 2004 Daniel Molkentin <molkentin@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef CLICKLINEEDIT_H +#define CLICKLINEEDIT_H + +#include <klineedit.h> +#include <tdepimmacros.h> + +namespace KPIM { + +/** + This class provides a KLineEdit which contains a greyed-out hinting + text as long as the user didn't enter any text + + @short LineEdit with customizable "Click here" text + @author Daniel Molkentin +*/ +class KDE_EXPORT ClickLineEdit : public KLineEdit +{ + Q_OBJECT + TQ_OBJECT + TQ_PROPERTY( TQString clickMessage READ clickMessage WRITE setClickMessage ) + public: + ClickLineEdit( TQWidget *parent, const TQString &msg = TQString(), const char* name = 0 ); + ~ClickLineEdit(); + + void setClickMessage( const TQString &msg ); + TQString clickMessage() const { return mClickMessage; } + + virtual void setText( const TQString& txt ); + + protected: + virtual void drawContents( TQPainter *p ); + virtual void focusInEvent( TQFocusEvent *ev ); + virtual void focusOutEvent( TQFocusEvent *ev ); + + private: + TQString mClickMessage; + bool mDrawClickMsg; + +}; + +} + +#endif // CLICKLINEEDIT_H + + diff --git a/libtdepim/collectingprocess.cpp b/libtdepim/collectingprocess.cpp new file mode 100644 index 000000000..58cdcb5ee --- /dev/null +++ b/libtdepim/collectingprocess.cpp @@ -0,0 +1,140 @@ +/* + collectingprocess.cpp + + This file is part of libtdepim. + Copyright (c) 2004 Ingo Kloecker <kloecker@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the TQt library by Trolltech AS, Norway (or with modified versions + of TQt that use the same license as TQt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + TQt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#include "collectingprocess.h" + +#include <tqvaluelist.h> + +#include <string.h> + +using namespace KPIM; + +struct CollectingProcess::Private { + Private() : stdoutSize( 0 ), stderrSize( 0 ) + {} + + uint stdoutSize; + TQValueList<TQByteArray> stdoutBuffer; + uint stderrSize; + TQValueList<TQByteArray> stderrBuffer; +}; + + +CollectingProcess::CollectingProcess( TQObject * parent, const char * name ) + : KProcess( parent, name ) +{ + d = new Private(); +} + +CollectingProcess::~CollectingProcess() { + delete d; d = 0; +} + +bool CollectingProcess::start( RunMode runmode, Communication comm ) { + // prevent duplicate connection + disconnect( this, TQT_SIGNAL( receivedStdout( KProcess *, char *, int ) ), + this, TQT_SLOT( slotReceivedStdout( KProcess *, char *, int ) ) ); + if ( comm & Stdout ) { + connect( this, TQT_SIGNAL( receivedStdout( KProcess *, char *, int ) ), + this, TQT_SLOT( slotReceivedStdout( KProcess *, char *, int ) ) ); + } + // prevent duplicate connection + disconnect( this, TQT_SIGNAL( receivedStderr( KProcess *, char *, int ) ), + this, TQT_SLOT( slotReceivedStderr( KProcess *, char *, int ) ) ); + if ( comm & Stderr ) { + connect( this, TQT_SIGNAL( receivedStderr( KProcess *, char *, int ) ), + this, TQT_SLOT( slotReceivedStderr( KProcess *, char *, int ) ) ); + } + return KProcess::start( runmode, comm ); +} + +void CollectingProcess::slotReceivedStdout( KProcess *, char *buf, int len ) +{ + TQByteArray b; + b.duplicate( buf, len ); + d->stdoutBuffer.append( b ); + d->stdoutSize += len; +} + +void CollectingProcess::slotReceivedStderr( KProcess *, char *buf, int len ) +{ + TQByteArray b; + b.duplicate( buf, len ); + d->stderrBuffer.append( b ); + d->stderrSize += len; +} + +TQByteArray CollectingProcess::collectedStdout() +{ + if ( d->stdoutSize == 0 ) { + return TQByteArray(); + } + + uint offset = 0; + TQByteArray b( d->stdoutSize ); + for ( TQValueList<TQByteArray>::const_iterator it = d->stdoutBuffer.begin(); + it != d->stdoutBuffer.end(); + ++it ) { + memcpy( b.data() + offset, (*it).data(), (*it).size() ); + offset += (*it).size(); + } + d->stdoutBuffer.clear(); + d->stdoutSize = 0; + + return b; +} + +TQByteArray CollectingProcess::collectedStderr() +{ + if ( d->stderrSize == 0 ) { + return TQByteArray(); + } + + uint offset = 0; + TQByteArray b( d->stderrSize ); + for ( TQValueList<TQByteArray>::const_iterator it = d->stderrBuffer.begin(); + it != d->stderrBuffer.end(); + ++it ) { + memcpy( b.data() + offset, (*it).data(), (*it).size() ); + offset += (*it).size(); + } + d->stderrBuffer.clear(); + d->stderrSize = 0; + + return b; +} + +void CollectingProcess::virtual_hook( int id, void * data ) { + KProcess::virtual_hook( id, data ); +} + +#include "collectingprocess.moc" diff --git a/libtdepim/collectingprocess.h b/libtdepim/collectingprocess.h new file mode 100644 index 000000000..ae7378264 --- /dev/null +++ b/libtdepim/collectingprocess.h @@ -0,0 +1,79 @@ +/* -*- mode: C++ -*- + collectingprocess.h + + This file is part of libtdepim. + Copyright (c) 2004 Ingo Kloecker <kloecker@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the TQt library by Trolltech AS, Norway (or with modified versions + of TQt that use the same license as TQt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + TQt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#ifndef __KPIM_COLLECTINGPROCESS_H__ +#define __KPIM_COLLECTINGPROCESS_H__ + +#include <kprocess.h> +#include <tdepimmacros.h> + +namespace KPIM { + +/** + * @short An output collecting KProcess class. + * + * This class simplifies the usage of KProcess by collecting all output + * (stdout/stderr) of the process. + * + * @author Ingo Kloecker <kloecker@kde.org> + */ +class KDE_EXPORT CollectingProcess : public KProcess { + Q_OBJECT + TQ_OBJECT +public: + CollectingProcess( TQObject * parent = 0, const char * name = 0 ); + ~CollectingProcess(); + + /** Starts the process in NotifyOnExit mode and writes in to stdin of + the process. + */ + bool start( RunMode runmode, Communication comm ); + + /** Returns the contents of the stdout buffer and clears it afterwards. */ + TQByteArray collectedStdout(); + /** Returns the contents of the stderr buffer and clears it afterwards. */ + TQByteArray collectedStderr(); + +private slots: + void slotReceivedStdout( KProcess *, char *, int ); + void slotReceivedStderr( KProcess *, char *, int ); + +private: + class Private; + Private * d; +protected: + void virtual_hook( int id, void * data ); +}; + +} // namespace KPIM + +#endif // __KPIM_COLLECTINGPROCESS_H__ diff --git a/libtdepim/completionordereditor.cpp b/libtdepim/completionordereditor.cpp new file mode 100644 index 000000000..2687188d0 --- /dev/null +++ b/libtdepim/completionordereditor.cpp @@ -0,0 +1,307 @@ +/** -*- c++ -*- + * completionordereditor.cpp + * + * Copyright (c) 2004 David Faure <faure@kde.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of this program with any edition of + * the TQt library by Trolltech AS, Norway (or with modified versions + * of TQt that use the same license as TQt), and distribute linked + * combinations including the two. You must obey the GNU General + * Public License in all respects for all of the code used other than + * TQt. If you modify this file, you may extend this exception to + * your version of the file, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from + * your version. + */ +#include <config.h> // FOR KDEPIM_NEW_DISTRLISTS + +#include "completionordereditor.h" +#include "ldapclient.h" +#include "resourceabc.h" + +#include <kabc/stdaddressbook.h> +#include <kabc/resource.h> + +#include <kdebug.h> +#include <klocale.h> +#include <kiconloader.h> +#include <klistview.h> +#include <kpushbutton.h> + +#include <tqhbox.h> +#include <tqvbox.h> +#include <tqheader.h> +#include <tqtoolbutton.h> +#include <kapplication.h> +#include <dcopclient.h> + +/* + +Several items are used in addresseelineedit's completion object: + LDAP servers, KABC resources (imap and non-imap), Recent addresses (in kmail only). + +The default completion weights are as follow: + Recent addresses (kmail) : 10 (see kmail/kmlineeditspell.cpp) + LDAP: 50, 49, 48 etc. (see ldapclient.cpp) + KABC non-imap resources: 60 (see addresseelineedit.cpp and SimpleCompletionItem here) + Distribution lists: 60 (see addresseelineedit.cpp and SimpleCompletionItem here) + KABC imap resources: 80 (see kresources/imap/kabc/resourceimap.cpp) + +This dialog allows to change those weights, by showing one item per: + - LDAP server + - KABC non-imap resource + - KABC imap subresource + plus one item for Distribution Lists. + + Maybe 'recent addresses' should be configurable too, but first it might + be better to add support for them in korganizer too. + +*/ + +using namespace KPIM; + +namespace KPIM { + +int CompletionItemList::compareItems( TQPtrCollection::Item s1, TQPtrCollection::Item s2 ) +{ + int w1 = ( (CompletionItem*)s1 )->completionWeight(); + int w2 = ( (CompletionItem*)s2 )->completionWeight(); + // s1 < s2 if it has a higher completion value, i.e. w1 > w2. + return w2 - w1; +} + +class LDAPCompletionItem : public CompletionItem +{ +public: + LDAPCompletionItem( LdapClient* ldapClient ) : mLdapClient( ldapClient ) {} + virtual TQString label() const { return i18n( "LDAP server %1" ).tqarg( mLdapClient->server().host() ); } + virtual int completionWeight() const { return mLdapClient->completionWeight(); } + virtual void save( CompletionOrderEditor* ); +protected: + virtual void setCompletionWeight( int weight ) { mWeight = weight; } +private: + LdapClient* mLdapClient; + int mWeight; +}; + +void LDAPCompletionItem::save( CompletionOrderEditor* ) +{ + KConfig * config = LdapSearch::config(); + config->setGroup( "LDAP" ); + config->writeEntry( TQString( "SelectedCompletionWeight%1" ).tqarg( mLdapClient->clientNumber() ), + mWeight ); + config->sync(); +} + +// A simple item saved into kpimcompletionorder (no subresources, just name/identifier/weight) +class SimpleCompletionItem : public CompletionItem +{ +public: + SimpleCompletionItem( CompletionOrderEditor* editor, const TQString& label, const TQString& identifier, int weight ) + : mLabel( label ), mIdentifier( identifier ) { + KConfigGroup group( editor->configFile(), "CompletionWeights" ); + mWeight = group.readNumEntry( mIdentifier, weight ); + } + virtual TQString label() const { return mLabel; } + virtual int completionWeight() const { return mWeight; } + virtual void save( CompletionOrderEditor* ); +protected: + virtual void setCompletionWeight( int weight ) { mWeight = weight; } +private: + TQString mLabel, mIdentifier; + int mWeight; +}; + +void SimpleCompletionItem::save( CompletionOrderEditor* editor ) +{ + // Maybe KABC::Resource could have a completionWeight setting (for readConfig/writeConfig) + // But for kdelibs-3.2 compat purposes I can't do that. + KConfigGroup group( editor->configFile(), "CompletionWeights" ); + group.writeEntry( mIdentifier, mWeight ); +} + +// An imap subresource for kabc +class KABCImapSubResCompletionItem : public CompletionItem +{ +public: + KABCImapSubResCompletionItem( ResourceABC* resource, const TQString& subResource ) + : mResource( resource ), mSubResource( subResource ), mWeight( completionWeight() ) {} + virtual TQString label() const { + return TQString( "%1 %2" ).tqarg( mResource->resourceName() ).tqarg( mResource->subresourceLabel( mSubResource ) ); + } + virtual int completionWeight() const { + return mResource->subresourceCompletionWeight( mSubResource ); + } + virtual void setCompletionWeight( int weight ) { + mWeight = weight; + } + virtual void save( CompletionOrderEditor* ) { + mResource->setSubresourceCompletionWeight( mSubResource, mWeight ); + } +private: + ResourceABC* mResource; + TQString mSubResource; + int mWeight; +}; + +///////// + +class CompletionViewItem : public TQListViewItem +{ +public: + CompletionViewItem( TQListView* lv, CompletionItem* item ) + : TQListViewItem( lv, lv->lastItem(), item->label() ), mItem( item ) {} + CompletionItem* item() const { return mItem; } + void setItem( CompletionItem* i ) { mItem = i; setText( 0, mItem->label() ); } + +private: + CompletionItem* mItem; +}; + +CompletionOrderEditor::CompletionOrderEditor( KPIM::LdapSearch* ldapSearch, + TQWidget* parent, const char* name ) + : KDialogBase( parent, name, true, i18n("Edit Completion Order"), Ok|Cancel, Ok, true ), + mConfig( "kpimcompletionorder" ), mDirty( false ) +{ + mItems.setAutoDelete( true ); + // The first step is to gather all the data, creating CompletionItem objects + TQValueList< LdapClient* > ldapClients = ldapSearch->clients(); + for( TQValueList<LdapClient*>::const_iterator it = ldapClients.begin(); it != ldapClients.end(); ++it ) { + //kdDebug(5300) << "LDAP: host " << (*it)->host() << " weight " << (*it)->completionWeight() << endl; + mItems.append( new LDAPCompletionItem( *it ) ); + } + KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true ); + TQPtrList<KABC::Resource> resources = addressBook->resources(); + for( TQPtrListIterator<KABC::Resource> resit( resources ); *resit; ++resit ) { + //kdDebug(5300) << "KABC Resource: " << (*resit)->className() << endl; + ResourceABC* res = dynamic_cast<ResourceABC *>( *resit ); + if ( res ) { // IMAP KABC resource + const TQStringList subresources = res->subresources(); + for( TQStringList::const_iterator it = subresources.begin(); it != subresources.end(); ++it ) { + mItems.append( new KABCImapSubResCompletionItem( res, *it ) ); + } + } else { // non-IMAP KABC resource + mItems.append( new SimpleCompletionItem( this, (*resit)->resourceName(), + (*resit)->identifier(), 60 ) ); + } + } + +#ifndef KDEPIM_NEW_DISTRLISTS // new distr lists are normal contact, so no separate item if using them + // Add an item for distribution lists + mItems.append( new SimpleCompletionItem( this, i18n( "Distribution Lists" ), "DistributionLists" ), 60 ); +#endif + + mItems.append( new SimpleCompletionItem( this, i18n( "Recent Addresses" ), "Recent Addresses", 10 ) ); + + // Now sort the items, then create the GUI + mItems.sort(); + + TQHBox* page = makeHBoxMainWidget(); + mListView = new KListView( page ); + mListView->setSorting( -1 ); + mListView->addColumn( TQString() ); + mListView->header()->hide(); + + for( TQPtrListIterator<CompletionItem> compit( mItems ); *compit; ++compit ) { + new CompletionViewItem( mListView, *compit ); + kdDebug(5300) << " " << (*compit)->label() << " " << (*compit)->completionWeight() << endl; + } + + TQVBox* upDownBox = new TQVBox( page ); + mUpButton = new KPushButton( upDownBox, "mUpButton" ); + mUpButton->setIconSet( BarIconSet( "up", KIcon::SizeSmall ) ); + mUpButton->setEnabled( false ); // b/c no item is selected yet + mUpButton->setFocusPolicy( TQ_StrongFocus ); + + mDownButton = new KPushButton( upDownBox, "mDownButton" ); + mDownButton->setIconSet( BarIconSet( "down", KIcon::SizeSmall ) ); + mDownButton->setEnabled( false ); // b/c no item is selected yet + mDownButton->setFocusPolicy( TQ_StrongFocus ); + + TQWidget* spacer = new TQWidget( upDownBox ); + upDownBox->setStretchFactor( spacer, 100 ); + + connect( mListView, TQT_SIGNAL( selectionChanged( TQListViewItem* ) ), + TQT_SLOT( slotSelectionChanged( TQListViewItem* ) ) ); + connect( mUpButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotMoveUp() ) ); + connect( mDownButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotMoveDown() ) ); +} + +CompletionOrderEditor::~CompletionOrderEditor() +{ +} + +void CompletionOrderEditor::slotSelectionChanged( TQListViewItem *item ) +{ + mDownButton->setEnabled( item && item->itemBelow() ); + mUpButton->setEnabled( item && item->itemAbove() ); +} + +static void swapItems( CompletionViewItem *one, CompletionViewItem *other ) +{ + CompletionItem* i = one->item(); + one->setItem( other->item() ); + other->setItem( i ); +} + +void CompletionOrderEditor::slotMoveUp() +{ + CompletionViewItem *item = static_cast<CompletionViewItem *>( mListView->selectedItem() ); + if ( !item ) return; + CompletionViewItem *above = static_cast<CompletionViewItem *>( item->itemAbove() ); + if ( !above ) return; + swapItems( item, above ); + mListView->setCurrentItem( above ); + mListView->setSelected( above, true ); + mDirty = true; +} + +void CompletionOrderEditor::slotMoveDown() +{ + CompletionViewItem *item = static_cast<CompletionViewItem *>( mListView->selectedItem() ); + if ( !item ) return; + CompletionViewItem *below = static_cast<CompletionViewItem *>( item->itemBelow() ); + if ( !below ) return; + swapItems( item, below ); + mListView->setCurrentItem( below ); + mListView->setSelected( below, true ); + mDirty = true; +} + +void CompletionOrderEditor::slotOk() +{ + if ( mDirty ) { + int w = 100; + for ( TQListViewItem* it = mListView->firstChild(); it; it = it->nextSibling() ) { + CompletionViewItem *item = static_cast<CompletionViewItem *>( it ); + item->item()->setCompletionWeight( w ); + item->item()->save( this ); + kdDebug(5300) << "slotOk: " << item->item()->label() << " " << w << endl; + --w; + } + + // Emit DCOP signal + // The emitter is always set to KPIM::IMAPCompletionOrder, so that the connect works + // This is why we can't use k_dcop_signals here, but need to use emitDCOPSignal + kapp->dcopClient()->emitDCOPSignal( "KPIM::IMAPCompletionOrder", "orderChanged()", TQByteArray() ); + } + KDialogBase::slotOk(); +} + +} // namespace KPIM + +#include "completionordereditor.moc" diff --git a/libtdepim/completionordereditor.h b/libtdepim/completionordereditor.h new file mode 100644 index 000000000..50cc3b3cb --- /dev/null +++ b/libtdepim/completionordereditor.h @@ -0,0 +1,93 @@ +/* -*- c++ -*- + * completionordereditor.h + * + * Copyright (c) 2004 David Faure <faure@kde.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of this program with any edition of + * the TQt library by Trolltech AS, Norway (or with modified versions + * of TQt that use the same license as TQt), and distribute linked + * combinations including the two. You must obey the GNU General + * Public License in all respects for all of the code used other than + * TQt. If you modify this file, you may extend this exception to + * your version of the file, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from + * your version. + */ + +#ifndef COMPLETIONORDEREDITOR_H +#define COMPLETIONORDEREDITOR_H + +#include <kdialogbase.h> +#include <kconfig.h> + +class KPushButton; +class KListView; +namespace KPIM { + +class LdapSearch; +class CompletionOrderEditor; + +// Base class for items in the list +class CompletionItem +{ +public: + virtual ~CompletionItem() {} + virtual TQString label() const = 0; + virtual int completionWeight() const = 0; + virtual void setCompletionWeight( int weight ) = 0; + virtual void save( CompletionOrderEditor* ) = 0; +}; + + +// I don't like TQPtrList much, but it has compareItems, which TQValueList doesn't +class CompletionItemList : public TQPtrList<CompletionItem> +{ +public: + CompletionItemList() {} + virtual int compareItems( TQPtrCollection::Item s1, TQPtrCollection::Item s2 ); +}; + +class CompletionOrderEditor : public KDialogBase { + Q_OBJECT + TQ_OBJECT + +public: + CompletionOrderEditor( KPIM::LdapSearch* ldapSearch, TQWidget* parent, const char* name = 0 ); + ~CompletionOrderEditor(); + + KConfig* configFile() { return &mConfig; } + +private slots: + void slotSelectionChanged( TQListViewItem* ); + void slotMoveUp(); + void slotMoveDown(); + virtual void slotOk(); + +private: + KConfig mConfig; + CompletionItemList mItems; + KListView* mListView; + KPushButton* mUpButton; + KPushButton* mDownButton; + + bool mDirty; +}; + +} // namespace + +#endif /* COMPLETIONORDEREDITOR_H */ + diff --git a/libtdepim/configmanager.cpp b/libtdepim/configmanager.cpp new file mode 100644 index 000000000..64b4562fc --- /dev/null +++ b/libtdepim/configmanager.cpp @@ -0,0 +1,35 @@ +/* + configmanager.cpp + + KMail, the KDE mail client. + Copyright (c) 2002 the KMail authors. + See file AUTHORS for details + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License, + version 2.0, as published by the Free Software Foundation. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "configmanager.h" + +using namespace KPIM; + +ConfigManager::ConfigManager( TQObject * parent, const char * name ) + : TQObject( parent, name ) +{ + +} + +ConfigManager::~ConfigManager() +{ + +} + +#include "configmanager.moc" diff --git a/libtdepim/configmanager.h b/libtdepim/configmanager.h new file mode 100644 index 000000000..832a0f70f --- /dev/null +++ b/libtdepim/configmanager.h @@ -0,0 +1,55 @@ +/* -*- c++ -*- + configmanager.h + + KMail, the KDE mail client. + Copyright (c) 2002 the KMail authors. + See file AUTHORS for details + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License, + version 2.0, as published by the Free Software Foundation. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US +*/ + + +#ifndef _KMAIL_CONFIGMANAGER_H_ +#define _KMAIL_CONFIGMANAGER_H_ + +#include <tqobject.h> + +#include <tdepimmacros.h> + +class KMKernel; + +namespace KPIM { + +/** + * @short Class for managing a set of config options. + * @author Marc Mutz <mutz@kde.org> + **/ +class KDE_EXPORT ConfigManager : public TQObject { + Q_OBJECT + TQ_OBJECT +public: + /** Commit changes to disk and emit changed() if necessary. */ + virtual void commit() = 0; + /** Re-read the config from disk and forget changes. */ + virtual void rollback() = 0; + + /** Check whether there are any unsaved changes. */ + virtual bool hasPendingChanges() const = 0; + +signals: + /** Emitted whenever a commit changes any configure option */ + void changed(); + +protected: + ConfigManager( TQObject * parent=0, const char * name=0 ); + virtual ~ConfigManager(); +}; + +} + +#endif // _KMAIL_CONFIGMANAGER_H_ diff --git a/libtdepim/configure.in.in b/libtdepim/configure.in.in new file mode 100644 index 000000000..c405f31c4 --- /dev/null +++ b/libtdepim/configure.in.in @@ -0,0 +1,14 @@ +AC_MSG_CHECKING([whether to use new-style distribution lists]) +AC_ARG_ENABLE(newdistrlists, +AC_HELP_STRING([--disable-newdistrlists], + [Disables the new distribution lists (which are saved as addressee in the address book as normal contacts, useful for Kolab)]), + [ enable_new_distrlists=$enableval], [enable_new_distrlists=yes])dnl +if test "$enable_new_distrlists" = "yes" ; then + AC_DEFINE_UNQUOTED(KDEPIM_NEW_DISTRLISTS, 1, [Define if you want to use the new distribution lists]) + AC_MSG_RESULT(yes) +else + AC_MSG_RESULT(no) +fi + +AM_CONDITIONAL(compile_newdistrlists, test "x$enable_new_distrlists" = "xyes") + diff --git a/libtdepim/csshelper.cpp b/libtdepim/csshelper.cpp new file mode 100644 index 000000000..05f20e390 --- /dev/null +++ b/libtdepim/csshelper.cpp @@ -0,0 +1,647 @@ +/* -*- mode: C++; c-file-style: "gnu" -*- + csshelper.cpp + + This file is part of KMail, the KDE mail client. + Copyright (c) 2003 Marc Mutz <mutz@kde.org> + + KMail is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License, version 2, as + published by the Free Software Foundation. + + KMail 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the TQt library by Trolltech AS, Norway (or with modified versions + of TQt that use the same license as TQt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + TQt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#include "csshelper.h" + +#include <kconfig.h> +#include <kglobalsettings.h> +#include <kdebug.h> +#include <kglobal.h> + +#include <tqstring.h> +#include <tqapplication.h> + +namespace KPIM { + + namespace { + // some TQColor manipulators that hide the ugly TQColor API w.r.t. HSV: + inline TQColor darker( const TQColor & c ) { + int h, s, v; + c.hsv( &h, &s, &v ); + return TQColor( h, s, v*4/5, TQColor::Hsv ); + } + + inline TQColor desaturate( const TQColor & c ) { + int h, s, v; + c.hsv( &h, &s, &v ); + return TQColor( h, s/8, v, TQColor::Hsv ); + } + + inline TQColor fixValue( const TQColor & c, int newV ) { + int h, s, v; + c.hsv( &h, &s, &v ); + return TQColor( h, s, newV, TQColor::Hsv ); + } + + inline int getValueOf( const TQColor & c ) { + int h, s, v; + c.hsv( &h, &s, &v ); + return v; + } + } + + CSSHelper::CSSHelper( const TQPaintDeviceMetrics &pdm ) : + mShrinkQuotes( false ), + mMetrics( pdm ) + { + // initialize with defaults - should match the corresponding application defaults + mForegroundColor = TQApplication::tqpalette().active().text(); + mLinkColor = KGlobalSettings::linkColor(); + mVisitedLinkColor = KGlobalSettings::visitedLinkColor(); + mBackgroundColor = TQApplication::tqpalette().active().base(); + cHtmlWarning = TQColor( 0xFF, 0x40, 0x40 ); // warning frame color: light red + + cPgpEncrH = TQColor( 0x00, 0x80, 0xFF ); // light blue + cPgpOk1H = TQColor( 0x40, 0xFF, 0x40 ); // light green + cPgpOk0H = TQColor( 0xFF, 0xFF, 0x40 ); // light yellow + cPgpWarnH = TQColor( 0xFF, 0xFF, 0x40 ); // light yellow + cPgpErrH = TQt::red; + + for ( int i = 0 ; i < 3 ; ++i ) + mQuoteColor[i] = TQColor( 0x00, 0x80 - i * 0x10, 0x00 ); // shades of green + mRecycleQuoteColors = false; + + TQFont defaultFont = KGlobalSettings::generalFont(); + TQFont defaultFixedFont = KGlobalSettings::fixedFont(); + mBodyFont = mPrintFont = defaultFont; + mFixedFont = mFixedPrintFont = defaultFixedFont; + defaultFont.setItalic( true ); + for ( int i = 0 ; i < 3 ; ++i ) + mQuoteFont[i] = defaultFont; + + mBackingPixmapOn = false; + + recalculatePGPColors(); + } + + void CSSHelper::recalculatePGPColors() { + // determine the frame and body color for PGP messages from the header color + // if the header color equals the background color then the other colors are + // also set to the background color (-> old style PGP message viewing) + // else + // the brightness of the frame is set to 4/5 of the brightness of the header + // and in case of a light background color + // the saturation of the body is set to 1/8 of the saturation of the header + // while in case of a dark background color + // the value of the body is set to the value of the background color + + // Check whether the user uses a light color scheme + const int vBG = getValueOf( mBackgroundColor ); + const bool lightBG = vBG >= 128; + if ( cPgpOk1H == mBackgroundColor ) { + cPgpOk1F = mBackgroundColor; + cPgpOk1B = mBackgroundColor; + } else { + cPgpOk1F= darker( cPgpOk1H ); + cPgpOk1B = lightBG ? desaturate( cPgpOk1H ) : fixValue( cPgpOk1H, vBG ); + } + if ( cPgpOk0H == mBackgroundColor ) { + cPgpOk0F = mBackgroundColor; + cPgpOk0B = mBackgroundColor; + } else { + cPgpOk0F = darker( cPgpOk0H ); + cPgpOk0B = lightBG ? desaturate( cPgpOk0H ) : fixValue( cPgpOk0H, vBG ); + } + if ( cPgpWarnH == mBackgroundColor ) { + cPgpWarnF = mBackgroundColor; + cPgpWarnB = mBackgroundColor; + } else { + cPgpWarnF = darker( cPgpWarnH ); + cPgpWarnB = lightBG ? desaturate( cPgpWarnH ) : fixValue( cPgpWarnH, vBG ); + } + if ( cPgpErrH == mBackgroundColor ) { + cPgpErrF = mBackgroundColor; + cPgpErrB = mBackgroundColor; + } else { + cPgpErrF = darker( cPgpErrH ); + cPgpErrB = lightBG ? desaturate( cPgpErrH ) : fixValue( cPgpErrH, vBG ); + } + if ( cPgpEncrH == mBackgroundColor ) { + cPgpEncrF = mBackgroundColor; + cPgpEncrB = mBackgroundColor; + } else { + cPgpEncrF = darker( cPgpEncrH ); + cPgpEncrB = lightBG ? desaturate( cPgpEncrH ) : fixValue( cPgpEncrH, vBG ); + } + } + + TQString CSSHelper::cssDefinitions( bool fixed ) const { + return + commonCssDefinitions() + + + "@media screen {\n\n" + + + screenCssDefinitions( this, fixed ) + + + "}\n" + "@media print {\n\n" + + + printCssDefinitions( fixed ) + + + "}\n"; + } + + TQString CSSHelper::htmlHead( bool /*fixed*/ ) const { + return + "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" + "<html><head><title></title></head>\n" + "<body>\n"; + } + + TQString CSSHelper::quoteFontTag( int level ) const { + if ( level < 0 ) + level = 0; + static const int numQuoteLevels = sizeof mQuoteFont / sizeof *mQuoteFont; + const int effectiveLevel = mRecycleQuoteColors + ? level % numQuoteLevels + 1 + : kMin( level + 1, numQuoteLevels ) ; + if ( level >= numQuoteLevels ) + return TQString( "<div class=\"deepquotelevel%1\">" ).tqarg( effectiveLevel ); + else + return TQString( "<div class=\"quotelevel%1\">" ).tqarg( effectiveLevel ); + } + + TQString CSSHelper::nonQuotedFontTag() const { + return "<div class=\"noquote\">"; + } + + TQFont CSSHelper::bodyFont( bool fixed, bool print ) const { + return fixed ? ( print ? mFixedPrintFont : mFixedFont ) + : ( print ? mPrintFont : mBodyFont ); + } + + int CSSHelper::fontSize( bool fixed, bool print ) const { + return bodyFont( fixed, print ).pointSize(); + } + + + namespace { + int pointsToPixel( const TQPaintDeviceMetrics & metrics, int pointSize ) { + return ( pointSize * metrics.logicalDpiY() + 36 ) / 72 ; + } + } + + static const char * const quoteFontSizes[] = { "85", "80", "75" }; + + TQString CSSHelper::printCssDefinitions( bool fixed ) const { + const TQString headerFont = TQString( " font-family: \"%1\" ! important;\n" + " font-size: %2pt ! important;\n" ) + .tqarg( mPrintFont.family() ) + .tqarg( mPrintFont.pointSize() ); + const TQColorGroup & cg = TQApplication::tqpalette().active(); + + const TQFont printFont = bodyFont( fixed, true /* print */ ); + TQString quoteCSS; + if ( printFont.italic() ) + quoteCSS += " font-style: italic ! important;\n"; + if ( printFont.bold() ) + quoteCSS += " font-weight: bold ! important;\n"; + if ( !quoteCSS.isEmpty() ) + quoteCSS = "div.noquote {\n" + quoteCSS + "}\n\n"; + + return + TQString( "body {\n" + " font-family: \"%1\" ! important;\n" + " font-size: %2pt ! important;\n" + " color: #000000 ! important;\n" + " background-color: #ffffff ! important\n" + "}\n\n" ) + .tqarg( printFont.family(), + TQString::number( printFont.pointSize() ) ) + + + TQString( "tr.textAtmH,\n" + "tr.signInProgressH,\n" + "tr.rfc822H,\n" + "tr.encrH,\n" + "tr.signOkKeyOkH,\n" + "tr.signOkKeyBadH,\n" + "tr.signWarnH,\n" + "tr.signErrH,\n" + "div.header {\n" + "%1" + "}\n\n" + + "div.fancy.header > div {\n" + " background-color: %2 ! important;\n" + " color: %3 ! important;\n" + " padding: 4px ! important;\n" + " border: solid %3 1px ! important;\n" + "}\n\n" + + "div.fancy.header > div a[href] { color: %3 ! important; }\n\n" + + "div.fancy.header > table.outer{\n" + " background-color: %2 ! important;\n" + " color: %3 ! important;\n" + " border-bottom: solid %3 1px ! important;\n" + " border-left: solid %3 1px ! important;\n" + " border-right: solid %3 1px ! important;\n" + "}\n\n" + + "div.spamheader {\n" + " display:none ! important;\n" + "}\n\n" + + "div.htmlWarn {\n" + " border: 2px solid #ffffff ! important;\n" + "}\n\n" + + "div.senderpic{\n" + " font-size:0.8em ! important;\n" + " border:1px solid black ! important;\n" + " background-color:%2 ! important;\n" + "}\n\n" + + "div.senderstatus{\n" + " text-align:center ! important;\n" + "}\n\n" + + "div.noprint {\n" + " display:none ! important;\n" + "}\n\n" + ) + .tqarg( headerFont, + cg.background().name(), + cg.foreground().name() ) + + quoteCSS; + } + + TQString CSSHelper::screenCssDefinitions( const CSSHelper * helper, bool fixed ) const { + const TQString fgColor = mForegroundColor.name(); + const TQString bgColor = mBackgroundColor.name(); + const TQString linkColor = mLinkColor.name(); + const TQString headerFont = TQString(" font-family: \"%1\" ! important;\n" + " font-size: %2px ! important;\n") + .tqarg( mBodyFont.family() ) + .tqarg( pointsToPixel( helper->mMetrics, mBodyFont.pointSize() ) ); + const TQString background = ( mBackingPixmapOn + ? TQString( " background-image:url(file://%1) ! important;\n" ) + .tqarg( mBackingPixmapStr ) + : TQString( " background-color: %1 ! important;\n" ) + .tqarg( bgColor ) ); + const TQString bodyFontSize = TQString::number( pointsToPixel( helper->mMetrics, fontSize( fixed ) ) ) + "px" ; + const TQColorGroup & cg = TQApplication::tqpalette().active(); + + TQString quoteCSS; + if ( bodyFont( fixed ).italic() ) + quoteCSS += " font-style: italic ! important;\n"; + if ( bodyFont( fixed ).bold() ) + quoteCSS += " font-weight: bold ! important;\n"; + if ( !quoteCSS.isEmpty() ) + quoteCSS = "div.noquote {\n" + quoteCSS + "}\n\n"; + + // CSS definitions for quote levels 1-3 + for ( int i = 0 ; i < 3 ; ++i ) { + quoteCSS += TQString( "div.quotelevel%1 {\n" + " color: %2 ! important;\n" ) + .tqarg( TQString::number(i+1), mQuoteColor[i].name() ); + if ( mQuoteFont[i].italic() ) + quoteCSS += " font-style: italic ! important;\n"; + if ( mQuoteFont[i].bold() ) + quoteCSS += " font-weight: bold ! important;\n"; + if ( mShrinkQuotes ) + quoteCSS += " font-size: " + TQString::tqfromLatin1( quoteFontSizes[i] ) + + "% ! important;\n"; + quoteCSS += "}\n\n"; + } + + // CSS definitions for quote levels 4+ + for ( int i = 0 ; i < 3 ; ++i ) { + quoteCSS += TQString( "div.deepquotelevel%1 {\n" + " color: %2 ! important;\n" ) + .tqarg( TQString::number(i+1), mQuoteColor[i].name() ); + if ( mQuoteFont[i].italic() ) + quoteCSS += " font-style: italic ! important;\n"; + if ( mQuoteFont[i].bold() ) + quoteCSS += " font-weight: bold ! important;\n"; + if ( mShrinkQuotes ) + quoteCSS += " font-size: 70% ! important;\n"; + quoteCSS += "}\n\n"; + } + + return + TQString( "body {\n" + " font-family: \"%1\" ! important;\n" + " font-size: %2 ! important;\n" + " color: %3 ! important;\n" + "%4" + "}\n\n" ) + .tqarg( bodyFont( fixed ).family(), + bodyFontSize, + fgColor, + background ) + + + TQString( "a {\n" + " color: %1 ! important;\n" + " text-decoration: none ! important;\n" + "}\n\n" + + "a.white {\n" + " color: white ! important;\n" + "}\n\n" + + "a.black {\n" + " color: black ! important;\n" + "}\n\n" + + "table.textAtm { background-color: %2 ! important; }\n\n" + + "tr.textAtmH {\n" + " background-color: %3 ! important;\n" + "%4" + "}\n\n" + + "tr.textAtmB {\n" + " background-color: %3 ! important;\n" + "}\n\n" + + "table.signInProgress,\n" + "table.rfc822 {\n" + " background-color: %3 ! important;\n" + "}\n\n" + + "tr.signInProgressH,\n" + "tr.rfc822H {\n" + "%4" + "}\n\n" ) + .tqarg( linkColor, fgColor, bgColor, headerFont ) + + + TQString( "table.encr {\n" + " background-color: %1 ! important;\n" + "}\n\n" + + "tr.encrH {\n" + " background-color: %2 ! important;\n" + "%3" + "}\n\n" + + "tr.encrB { background-color: %4 ! important; }\n\n" ) + .tqarg( cPgpEncrF.name(), + cPgpEncrH.name(), + headerFont, + cPgpEncrB.name() ) + + + TQString( "table.signOkKeyOk {\n" + " background-color: %1 ! important;\n" + "}\n\n" + + "tr.signOkKeyOkH {\n" + " background-color: %2 ! important;\n" + "%3" + "}\n\n" + + "tr.signOkKeyOkB { background-color: %4 ! important; }\n\n" ) + .tqarg( cPgpOk1F.name(), + cPgpOk1H.name(), + headerFont, + cPgpOk1B.name() ) + + + TQString( "table.signOkKeyBad {\n" + " background-color: %1 ! important;\n" + "}\n\n" + + "tr.signOkKeyBadH {\n" + " background-color: %2 ! important;\n" + "%3" + "}\n\n" + + "tr.signOkKeyBadB { background-color: %4 ! important; }\n\n" ) + .tqarg( cPgpOk0F.name(), + cPgpOk0H.name(), + headerFont, + cPgpOk0B.name() ) + + + TQString( "table.signWarn {\n" + " background-color: %1 ! important;\n" + "}\n\n" + + "tr.signWarnH {\n" + " background-color: %2 ! important;\n" + "%3" + "}\n\n" + + "tr.signWarnB { background-color: %4 ! important; }\n\n" ) + .tqarg( cPgpWarnF.name(), + cPgpWarnH.name(), + headerFont, + cPgpWarnB.name() ) + + + TQString( "table.signErr {\n" + " background-color: %1 ! important;\n" + "}\n\n" + + "tr.signErrH {\n" + " background-color: %2 ! important;\n" + "%3" + "}\n\n" + + "tr.signErrB { background-color: %4 ! important; }\n\n" ) + .tqarg( cPgpErrF.name(), + cPgpErrH.name(), + headerFont, + cPgpErrB.name() ) + + + TQString( "div.htmlWarn {\n" + " border: 2px solid %1 ! important;\n" + "}\n\n" ) + .tqarg( cHtmlWarning.name() ) + + + TQString( "div.header {\n" + "%1" + "}\n\n" + + "div.fancy.header > div {\n" + " background-color: %2 ! important;\n" + " color: %3 ! important;\n" + " border: solid %4 1px ! important;\n" + "}\n\n" + + "div.fancy.header > div a[href] { color: %3 ! important; }\n\n" + + "div.fancy.header > div a[href]:hover { text-decoration: underline ! important; }\n\n" + + "div.fancy.header > div.spamheader {\n" + " background-color: #cdcdcd ! important;\n" + " border-top: 0px ! important;\n" + " padding: 3px ! important;\n" + " color: black ! important;\n" + " font-weight: bold ! important;\n" + " font-size: smaller ! important;\n" + "}\n\n" + + "div.fancy.header > table.outer {\n" + " background-color: %5 ! important;\n" + " color: %4 ! important;\n" + " border-bottom: solid %4 1px ! important;\n" + " border-left: solid %4 1px ! important;\n" + " border-right: solid %4 1px ! important;\n" + "}\n\n" + + "div.senderpic{\n" + " padding: 0px ! important;\n" + " font-size:0.8em ! important;\n" + " border:1px solid %6 ! important;\n" + // FIXME: InfoBackground crashes KHTML + //" background-color:InfoBackground ! important;\n" + " background-color:%5 ! important;\n" + "}\n\n" + + "div.senderstatus{\n" + " text-align:center ! important;\n" + "}\n\n" + ) + + .tqarg( headerFont ) + .tqarg( cg.highlight().name(), + cg.highlightedText().name(), + cg.foreground().name(), + cg.background().name() ) + .tqarg( cg.mid().name() ) + + quoteCSS; + } + + TQString CSSHelper::commonCssDefinitions() const { + return + "div.header {\n" + " margin-bottom: 10pt ! important;\n" + "}\n\n" + + "table.textAtm {\n" + " margin-top: 10pt ! important;\n" + " margin-bottom: 10pt ! important;\n" + "}\n\n" + + "tr.textAtmH,\n" + "tr.textAtmB,\n" + "tr.rfc822B {\n" + " font-weight: normal ! important;\n" + "}\n\n" + + "tr.signInProgressH,\n" + "tr.rfc822H,\n" + "tr.encrH,\n" + "tr.signOkKeyOkH,\n" + "tr.signOkKeyBadH,\n" + "tr.signWarnH,\n" + "tr.signErrH {\n" + " font-weight: bold ! important;\n" + "}\n\n" + + "tr.textAtmH td,\n" + "tr.textAtmB td {\n" + " padding: 3px ! important;\n" + "}\n\n" + + "table.rfc822 {\n" + " width: 100% ! important;\n" + " border: solid 1px black ! important;\n" + " margin-top: 10pt ! important;\n" + " margin-bottom: 10pt ! important;\n" + "}\n\n" + + "table.textAtm,\n" + "table.encr,\n" + "table.signWarn,\n" + "table.signErr,\n" + "table.signOkKeyBad,\n" + "table.signOkKeyOk,\n" + "table.signInProgress,\n" + "div.fancy.header table {\n" + " width: 100% ! important;\n" + " border-width: 0px ! important;\n" + "}\n\n" + + "div.htmlWarn {\n" + " margin: 0px 5% ! important;\n" + " padding: 10px ! important;\n" + " text-align: left ! important;\n" + "}\n\n" + + "div.fancy.header > div {\n" + " font-weight: bold ! important;\n" + " padding: 4px ! important;\n" + "}\n\n" + + "div.fancy.header table {\n" + " padding: 2px ! important;\n" // ### khtml bug: this is ignored + " text-align: left ! important\n" + "}\n\n" + + "div.fancy.header table th {\n" + " padding: 0px ! important;\n" + " white-space: nowrap ! important;\n" + " border-spacing: 0px ! important;\n" + " text-align: left ! important;\n" + " vertical-align: top ! important;\n" + "}\n\n" + + "div.fancy.header table td {\n" + " padding: 0px ! important;\n" + " border-spacing: 0px ! important;\n" + " text-align: left ! important;\n" + " vertical-align: top ! important;\n" + " width: 100% ! important;\n" + "}\n\n" + + "span.pimsmileytext {\n" + " position: absolute;\n" + " top: 0px;\n" + " left: 0px;\n" + " visibility: hidden;\n" + "}\n\n" + + "img.pimsmileyimg {\n" + "}\n\n" + + "div.quotelevelmark {\n" + " position: absolute;\n" + " margin-left:-10px;\n" + "}\n\n" + ; + } + + + void CSSHelper::setBodyFont( const TQFont& font ) + { + mBodyFont = font; + } + + void CSSHelper::setPrintFont( const TQFont& font ) + { + mPrintFont = font; + } + + TQColor CSSHelper::pgpWarnColor() const + { + return cPgpWarnH; + } + +} // namespace KPIM diff --git a/libtdepim/csshelper.h b/libtdepim/csshelper.h new file mode 100644 index 000000000..ab9190cf8 --- /dev/null +++ b/libtdepim/csshelper.h @@ -0,0 +1,111 @@ +/* -*- c++ -*- + csshelper.h + + This file is part of KMail, the KDE mail client. + Copyright (c) 2003 Marc Mutz <mutz@kde.org> + + KMail is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License, version 2, as + published by the Free Software Foundation. + + KMail 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the TQt library by Trolltech AS, Norway (or with modified versions + of TQt that use the same license as TQt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + TQt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#ifndef KPIM_CSSHELPER_H +#define KPIM_CSSHELPER_H + +#include <tqcolor.h> +#include <tqfont.h> +#include <tqpaintdevicemetrics.h> + +class TQString; + +namespace KPIM { + +class CSSHelper { + public: + /** Construct a CSSHelper object and set its font and color settings to + default values. + Sub-Classes should put their config loading here. + */ + CSSHelper( const TQPaintDeviceMetrics &pdm ); + + /** @return HTML head including style sheet definitions and the + >body< tag */ + TQString htmlHead( bool fixedFont = false ) const; + + /** @return The collected CSS definitions as a string */ + TQString cssDefinitions( bool fixedFont = false ) const; + + /** @return a <div> start tag with embedded style + information suitable for quoted text with quote level @p level */ + TQString quoteFontTag( int level ) const; + /** @return a <div> start tag with embedded style + information suitable for non-quoted text */ + TQString nonQuotedFontTag() const; + + TQFont bodyFont( bool fixedFont = false, bool printing = false ) const; + + void setBodyFont( const TQFont& font ); + void setPrintFont( const TQFont& font ); + + TQColor pgpWarnColor() const; + + protected: + /** Recalculate PGP frame and body colors (should be called after changing + color settings) */ + void recalculatePGPColors(); + + protected: + TQFont mBodyFont, mPrintFont, mFixedFont, mFixedPrintFont; + TQFont mQuoteFont[3]; + TQColor mQuoteColor[3]; + bool mRecycleQuoteColors; + bool mBackingPixmapOn; + bool mShrinkQuotes; + TQString mBackingPixmapStr; + TQColor mForegroundColor, mLinkColor, mVisitedLinkColor, mBackgroundColor; + // colors for PGP (Frame, Header, Body) + TQColor cPgpOk1F, cPgpOk1H, cPgpOk1B, + cPgpOk0F, cPgpOk0H, cPgpOk0B, + cPgpWarnF, cPgpWarnH, cPgpWarnB, + cPgpErrF, cPgpErrH, cPgpErrB, + cPgpEncrF, cPgpEncrH, cPgpEncrB; + // color of frame of warning preceding the source of HTML messages + TQColor cHtmlWarning; + + private: + int fontSize( bool fixed, bool print = false ) const; + // returns CSS rules specific to the print media type + TQString printCssDefinitions( bool fixed ) const; + // returns CSS rules specific to the screen media type + TQString screenCssDefinitions( const CSSHelper * helper, bool fixed ) const; + // returns CSS rules common to both screen and print media types + TQString commonCssDefinitions() const; + + private: + const TQPaintDeviceMetrics mMetrics; + +}; + +} // namespace KPIM + +#endif // KPIM_CSSHELPER_H diff --git a/libtdepim/designerfields.cpp b/libtdepim/designerfields.cpp new file mode 100644 index 000000000..1a07655f7 --- /dev/null +++ b/libtdepim/designerfields.cpp @@ -0,0 +1,251 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@kde.org> + Copyright (c) 2004 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <tqcheckbox.h> +#include <tqcombobox.h> +#include <tqdatetimeedit.h> +#include <tqlayout.h> +#include <tqobjectlist.h> +#include <tqspinbox.h> +#include <tqregexp.h> +#include <tqtextedit.h> +#include <tqwidgetfactory.h> + +#include <kdatepicker.h> +#include <kdatetimewidget.h> +#include <kdialog.h> +#include <klineedit.h> +#include <kstandarddirs.h> +#include <kdebug.h> + +#include "designerfields.h" + +using namespace KPIM; + +DesignerFields::DesignerFields( const TQString &uiFile, TQWidget *parent, + const char *name ) + : TQWidget( parent, name ) +{ + initGUI( uiFile ); +} + +void DesignerFields::initGUI( const TQString &uiFile ) +{ + TQVBoxLayout *tqlayout = new TQVBoxLayout( this ); + + TQWidget *wdg = TQWidgetFactory::create( uiFile, 0, this ); + if ( !wdg ) { + kdError() << "No ui file found" << endl; + return; + } + + mTitle = wdg->caption(); + mIdentifier = wdg->name(); + + tqlayout->addWidget( wdg ); + + TQObjectList *list = wdg->queryList( TQWIDGET_OBJECT_NAME_STRING ); + TQObjectListIt it( *list ); + + TQStringList allowedTypes; + allowedTypes << TQLINEEDIT_OBJECT_NAME_STRING + << TQTEXTEDIT_OBJECT_NAME_STRING + << TQSPINBOX_OBJECT_NAME_STRING + << TQCHECKBOX_OBJECT_NAME_STRING + << TQCOMBOBOX_OBJECT_NAME_STRING + << TQDATETIMEEDIT_OBJECT_NAME_STRING + << "KLineEdit" + << "KDateTimeWidget" + << "KDatePicker"; + + while ( it.current() ) { + if ( allowedTypes.contains( it.current()->className() ) ) { + TQString name = it.current()->name(); + if ( name.startsWith( "X_" ) ) { + name = name.mid( 2 ); + + TQWidget *widget = TQT_TQWIDGET( it.current() ); + if ( !name.isEmpty() ) + mWidgets.insert( name, widget ); + + if ( it.current()->inherits( TQLINEEDIT_OBJECT_NAME_STRING ) ) + connect( it.current(), TQT_SIGNAL( textChanged( const TQString& ) ), + TQT_SIGNAL( modified() ) ); + else if ( it.current()->inherits( TQSPINBOX_OBJECT_NAME_STRING ) ) + connect( it.current(), TQT_SIGNAL( valueChanged( int ) ), + TQT_SIGNAL( modified() ) ); + else if ( it.current()->inherits( TQCHECKBOX_OBJECT_NAME_STRING ) ) + connect( it.current(), TQT_SIGNAL( toggled( bool ) ), + TQT_SIGNAL( modified() ) ); + else if ( it.current()->inherits( TQCOMBOBOX_OBJECT_NAME_STRING ) ) + connect( it.current(), TQT_SIGNAL( activated( const TQString& ) ), + TQT_SIGNAL( modified() ) ); + else if ( it.current()->inherits( TQDATETIMEEDIT_OBJECT_NAME_STRING ) ) + connect( it.current(), TQT_SIGNAL( valueChanged( const TQDateTime& ) ), + TQT_SIGNAL( modified() ) ); + else if ( it.current()->inherits( "KDateTimeWidget" ) ) + connect( it.current(), TQT_SIGNAL( valueChanged( const TQDateTime& ) ), + TQT_SIGNAL( modified() ) ); + else if ( it.current()->inherits( "KDatePicker" ) ) + connect( it.current(), TQT_SIGNAL( dateChanged( TQDate ) ), + TQT_SIGNAL( modified() ) ); + else if ( it.current()->inherits( TQTEXTEDIT_OBJECT_NAME_STRING ) ) + connect( it.current(), TQT_SIGNAL( textChanged() ), + TQT_SIGNAL( modified() ) ); + + if ( !widget->isEnabled() ) + mDisabledWidgets.append( widget ); + } + } + + ++it; + } + + delete list; +} + +TQString DesignerFields::identifier() const +{ + return mIdentifier; +} + +TQString DesignerFields::title() const +{ + return mTitle; +} + +void DesignerFields::load( DesignerFields::Storage *storage ) +{ + TQStringList keys = storage->keys(); + + // clear all custom page widgets + // we can't do this in the following loop, as it works on the + // custom fields of the vcard, which may not be set. + TQMap<TQString, TQWidget *>::ConstIterator widIt; + for ( widIt = mWidgets.begin(); widIt != mWidgets.end(); ++widIt ) { + TQString value; + if ( widIt.data()->inherits( TQLINEEDIT_OBJECT_NAME_STRING ) ) { + TQLineEdit *wdg = static_cast<TQLineEdit*>( widIt.data() ); + wdg->setText( TQString() ); + } else if ( widIt.data()->inherits( TQSPINBOX_OBJECT_NAME_STRING ) ) { + TQSpinBox *wdg = static_cast<TQSpinBox*>( widIt.data() ); + wdg->setValue( wdg->minValue() ); + } else if ( widIt.data()->inherits( TQCHECKBOX_OBJECT_NAME_STRING ) ) { + TQCheckBox *wdg = static_cast<TQCheckBox*>( widIt.data() ); + wdg->setChecked( false ); + } else if ( widIt.data()->inherits( TQDATETIMEEDIT_OBJECT_NAME_STRING ) ) { + TQDateTimeEdit *wdg = static_cast<TQDateTimeEdit*>( widIt.data() ); + wdg->setDateTime( TQDateTime::tqcurrentDateTime() ); + } else if ( widIt.data()->inherits( "KDateTimeWidget" ) ) { + KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( widIt.data() ); + wdg->setDateTime( TQDateTime::tqcurrentDateTime() ); + } else if ( widIt.data()->inherits( "KDatePicker" ) ) { + KDatePicker *wdg = static_cast<KDatePicker*>( widIt.data() ); + wdg->setDate( TQDate::tqcurrentDate() ); + } else if ( widIt.data()->inherits( TQCOMBOBOX_OBJECT_NAME_STRING ) ) { + TQComboBox *wdg = static_cast<TQComboBox*>( widIt.data() ); + wdg->setCurrentItem( 0 ); + } else if ( widIt.data()->inherits( TQTEXTEDIT_OBJECT_NAME_STRING ) ) { + TQTextEdit *wdg = static_cast<TQTextEdit*>( widIt.data() ); + wdg->setText( TQString() ); + } + } + + TQStringList::ConstIterator it2; + for ( it2 = keys.begin(); it2 != keys.end(); ++it2 ) { + TQString value = storage->read( *it2 ); + + TQMap<TQString, TQWidget *>::ConstIterator it = mWidgets.find( *it2 ); + if ( it != mWidgets.end() ) { + if ( it.data()->inherits( TQLINEEDIT_OBJECT_NAME_STRING ) ) { + TQLineEdit *wdg = static_cast<TQLineEdit*>( it.data() ); + wdg->setText( value ); + } else if ( it.data()->inherits( TQSPINBOX_OBJECT_NAME_STRING ) ) { + TQSpinBox *wdg = static_cast<TQSpinBox*>( it.data() ); + wdg->setValue( value.toInt() ); + } else if ( it.data()->inherits( TQCHECKBOX_OBJECT_NAME_STRING ) ) { + TQCheckBox *wdg = static_cast<TQCheckBox*>( it.data() ); + wdg->setChecked( value == "true" || value == "1" ); + } else if ( it.data()->inherits( TQDATETIMEEDIT_OBJECT_NAME_STRING ) ) { + TQDateTimeEdit *wdg = static_cast<TQDateTimeEdit*>( it.data() ); + wdg->setDateTime( TQDateTime::fromString( value, Qt::ISODate ) ); + } else if ( it.data()->inherits( "KDateTimeWidget" ) ) { + KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( it.data() ); + wdg->setDateTime( TQDateTime::fromString( value, Qt::ISODate ) ); + } else if ( it.data()->inherits( "KDatePicker" ) ) { + KDatePicker *wdg = static_cast<KDatePicker*>( it.data() ); + wdg->setDate( TQDate::fromString( value, Qt::ISODate ) ); + } else if ( it.data()->inherits( TQCOMBOBOX_OBJECT_NAME_STRING ) ) { + TQComboBox *wdg = static_cast<TQComboBox*>( it.data() ); + wdg->setCurrentText( value ); + } else if ( it.data()->inherits( TQTEXTEDIT_OBJECT_NAME_STRING ) ) { + TQTextEdit *wdg = static_cast<TQTextEdit*>( it.data() ); + wdg->setText( value ); + } + } + } +} + +void DesignerFields::save( DesignerFields::Storage *storage ) +{ + TQMap<TQString, TQWidget*>::Iterator it; + for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) { + TQString value; + if ( it.data()->inherits( TQLINEEDIT_OBJECT_NAME_STRING ) ) { + TQLineEdit *wdg = static_cast<TQLineEdit*>( it.data() ); + value = wdg->text(); + } else if ( it.data()->inherits( TQSPINBOX_OBJECT_NAME_STRING ) ) { + TQSpinBox *wdg = static_cast<TQSpinBox*>( it.data() ); + value = TQString::number( wdg->value() ); + } else if ( it.data()->inherits( TQCHECKBOX_OBJECT_NAME_STRING ) ) { + TQCheckBox *wdg = static_cast<TQCheckBox*>( it.data() ); + value = ( wdg->isChecked() ? "true" : "false" ); + } else if ( it.data()->inherits( TQDATETIMEEDIT_OBJECT_NAME_STRING ) ) { + TQDateTimeEdit *wdg = static_cast<TQDateTimeEdit*>( it.data() ); + value = wdg->dateTime().toString( Qt::ISODate ); + } else if ( it.data()->inherits( "KDateTimeWidget" ) ) { + KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( it.data() ); + value = wdg->dateTime().toString( Qt::ISODate ); + } else if ( it.data()->inherits( "KDatePicker" ) ) { + KDatePicker *wdg = static_cast<KDatePicker*>( it.data() ); + value = wdg->date().toString( Qt::ISODate ); + } else if ( it.data()->inherits( TQCOMBOBOX_OBJECT_NAME_STRING ) ) { + TQComboBox *wdg = static_cast<TQComboBox*>( it.data() ); + value = wdg->currentText(); + } else if ( it.data()->inherits( TQTEXTEDIT_OBJECT_NAME_STRING ) ) { + TQTextEdit *wdg = static_cast<TQTextEdit*>( it.data() ); + value = wdg->text(); + } + + storage->write( it.key(), value ); + } +} + +void DesignerFields::setReadOnly( bool readOnly ) +{ + TQMap<TQString, TQWidget*>::Iterator it; + for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) + if ( mDisabledWidgets.find( it.data() ) == mDisabledWidgets.end() ) + it.data()->setEnabled( !readOnly ); +} + +#include "designerfields.moc" diff --git a/libtdepim/designerfields.h b/libtdepim/designerfields.h new file mode 100644 index 000000000..c7c2d22ca --- /dev/null +++ b/libtdepim/designerfields.h @@ -0,0 +1,75 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@kde.org> + Copyright (c) 2004 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef KPIM_DESIGNERFIELDS_H +#define KPIM_DESIGNERFIELDS_H + +#include <klocale.h> + +#include <tqmap.h> +#include <tqpair.h> +#include <tqstringlist.h> + +#include <tdepimmacros.h> + +namespace KPIM { + +class KDE_EXPORT DesignerFields : public TQWidget +{ + Q_OBJECT + TQ_OBJECT + public: + DesignerFields( const TQString &uiFile, TQWidget *parent, + const char *name = 0 ); + + class Storage + { + public: + virtual ~Storage() {} + + virtual TQStringList keys() = 0; + virtual TQString read( const TQString &key ) = 0; + virtual void write( const TQString &key, const TQString &value ) = 0; + }; + + void load( Storage * ); + void save( Storage * ); + + void setReadOnly( bool readOnly ); + + TQString identifier() const; + TQString title() const; + + signals: + void modified(); + + private: + void initGUI( const TQString& ); + + TQMap<TQString, TQWidget *> mWidgets; + TQValueList<TQWidget *> mDisabledWidgets; + TQString mTitle; + TQString mIdentifier; +}; + +} + +#endif diff --git a/libtdepim/diffalgo.cpp b/libtdepim/diffalgo.cpp new file mode 100644 index 000000000..b2cfc40eb --- /dev/null +++ b/libtdepim/diffalgo.cpp @@ -0,0 +1,85 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <libtdepim/diffalgo.h> + +using namespace KPIM; + +void DiffAlgo::begin() +{ + TQValueList<DiffAlgoDisplay*>::Iterator it; + for ( it = mDisplays.begin(); it != mDisplays.end(); ++it ) + (*it)->begin(); +} + +void DiffAlgo::end() +{ + TQValueList<DiffAlgoDisplay*>::Iterator it; + for ( it = mDisplays.begin(); it != mDisplays.end(); ++it ) + (*it)->end(); +} + +void DiffAlgo::setLeftSourceTitle( const TQString &title ) +{ + TQValueList<DiffAlgoDisplay*>::Iterator it; + for ( it = mDisplays.begin(); it != mDisplays.end(); ++it ) + (*it)->setLeftSourceTitle( title ); +} + +void DiffAlgo::setRightSourceTitle( const TQString &title ) +{ + TQValueList<DiffAlgoDisplay*>::Iterator it; + for ( it = mDisplays.begin(); it != mDisplays.end(); ++it ) + (*it)->setRightSourceTitle( title ); +} + +void DiffAlgo::additionalLeftField( const TQString &id, const TQString &value ) +{ + TQValueList<DiffAlgoDisplay*>::Iterator it; + for ( it = mDisplays.begin(); it != mDisplays.end(); ++it ) + (*it)->additionalLeftField( id, value ); +} + +void DiffAlgo::additionalRightField( const TQString &id, const TQString &value ) +{ + TQValueList<DiffAlgoDisplay*>::Iterator it; + for ( it = mDisplays.begin(); it != mDisplays.end(); ++it ) + (*it)->additionalRightField( id, value ); +} + +void DiffAlgo::conflictField( const TQString &id, const TQString &leftValue, + const TQString &rightValue ) +{ + TQValueList<DiffAlgoDisplay*>::Iterator it; + for ( it = mDisplays.begin(); it != mDisplays.end(); ++it ) + (*it)->conflictField( id, leftValue, rightValue ); +} + +void DiffAlgo::addDisplay( DiffAlgoDisplay *display ) +{ + if ( mDisplays.find( display ) == mDisplays.end() ) + mDisplays.append( display ); +} + +void DiffAlgo::removeDisplay( DiffAlgoDisplay *display ) +{ + mDisplays.remove( display ); +} diff --git a/libtdepim/diffalgo.h b/libtdepim/diffalgo.h new file mode 100644 index 000000000..513db7e33 --- /dev/null +++ b/libtdepim/diffalgo.h @@ -0,0 +1,138 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef DIFFALGO_H +#define DIFFALGO_H + +#include <tqvaluelist.h> +#include <tdepimmacros.h> + +namespace KPIM { + +/** + DiffAlgo and DiffAlgoDisplay work together for displaying differences between + two PIM objects like contacts, events or todos. + DiffAlgo is the bas class for the diffing algorithm and DiffAlgoDisplay is + responsible for representation. The separation makes it possible to use one + display for all diffing algorithm and vice versa. + */ +class DiffAlgoDisplay +{ + public: + + /** + Is called on the start of the diff. + */ + virtual void begin() = 0; + + /** + Is called on the end of the diff. + */ + virtual void end() = 0; + + /** + Sets the title of the left data source. + */ + virtual void setLeftSourceTitle( const TQString &title ) = 0; + + /** + Sets the title of the right data source. + */ + virtual void setRightSourceTitle( const TQString &title ) = 0; + + /** + Adds a field which is only available in the left data source. + */ + virtual void additionalLeftField( const TQString &id, const TQString &value ) = 0; + + /** + Adds a field which is only available in the right data source. + */ + virtual void additionalRightField( const TQString &id, const TQString &value ) = 0; + + /** + Adds a conflict between two fields. + */ + virtual void conflictField( const TQString &id, const TQString &leftValue, + const TQString &rightValue ) = 0; +}; + + +class KDE_EXPORT DiffAlgo +{ + public: + /** + Destructor. + */ + virtual ~DiffAlgo() {} + + /** + Starts the diffing algorithm. + */ + virtual void run() = 0; + + /** + Must be called on the start of the diff. + */ + void begin(); + + /** + Must be called on the end of the diff. + */ + void end(); + + /** + Sets the title of the left data source. + */ + void setLeftSourceTitle( const TQString &title ); + + /** + Sets the title of the right data source. + */ + void setRightSourceTitle( const TQString &title ); + + /** + Adds a field which is only available in the left data source. + */ + void additionalLeftField( const TQString &id, const TQString &value ); + + /** + Adds a field which is only available in the right data source. + */ + void additionalRightField( const TQString &id, const TQString &value ); + + /** + Adds a conflict between two fields. + */ + void conflictField( const TQString &id, const TQString &leftValue, + const TQString &rightValue ); + + void addDisplay( DiffAlgoDisplay *display ); + void removeDisplay( DiffAlgoDisplay *display ); + + + private: + TQValueList<DiffAlgoDisplay*> mDisplays; +}; + +} + +#endif diff --git a/libtdepim/distributionlist.cpp b/libtdepim/distributionlist.cpp new file mode 100644 index 000000000..4cf079c76 --- /dev/null +++ b/libtdepim/distributionlist.cpp @@ -0,0 +1,223 @@ +#include "distributionlist.h" +#include <kabc/addressbook.h> + +static const char* s_customFieldName = "DistributionList"; + +KPIM::DistributionList::DistributionList() + : KABC::Addressee() +{ + // can't insert the custom entry here, we need to remain a null addressee +} + +KPIM::DistributionList::DistributionList( const KABC::Addressee& addr ) + : KABC::Addressee( addr ) +{ +} + +void KPIM::DistributionList::setName( const TQString &name ) +{ + // We can't use Addressee::setName, the name isn't saved/loaded in the vcard (fixed in 3.4) + Addressee::setFormattedName( name ); + // Also set family name, just in case this entry appears in the normal contacts list (e.g. old kaddressbook) + Addressee::setFamilyName( name ); + // We're not an empty addressee anymore + // Set the custom field to non-empty, so that isDistributionList works + if ( custom( "KADDRESSBOOK", s_customFieldName ).isEmpty() ) + insertCustom( "KADDRESSBOOK", s_customFieldName, ";" ); +} + +// Helper function, to parse the contents of the custom field +// Returns a list of { uid, email } +typedef TQValueList<TQPair<TQString, TQString> > ParseList; +static ParseList parseCustom( const TQString& str ) +{ + ParseList res; + const TQStringList lst = TQStringList::split( ';', str ); + for( TQStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it ) { + if ( (*it).isEmpty() ) + continue; + // parse "uid,email" + TQStringList helpList = TQStringList::split( ',', (*it), true ); + Q_ASSERT( !helpList.isEmpty() ); + if ( helpList.isEmpty() ) + continue; + Q_ASSERT( helpList.count() < 3 ); // 1 or 2 items, but not more + const TQString uid = helpList.first(); + const TQString email = helpList.last(); + res.append( tqMakePair( uid, email ) ); + } + return res; +} + +void KPIM::DistributionList::insertEntry( const Addressee& addr, const TQString& email ) +{ + // insertEntry will removeEntry(uid), but not with formattedName + removeEntry( addr.formattedName(), email ); + insertEntry( addr.uid(), email ); +} + +void KPIM::DistributionList::insertEntry( const TQString& uid, const TQString& email ) +{ + Q_ASSERT( !email.isEmpty() || email.isNull() ); // hopefully never called with "", would lead to confusion + removeEntry( uid, email ); // avoid duplicates + TQString str = custom( "KADDRESSBOOK", s_customFieldName ); + // Assumption: UIDs don't contain ; nor , + str += ";" + uid + "," + email; + insertCustom( "KADDRESSBOOK", s_customFieldName, str ); // replace old value +} + +void KPIM::DistributionList::removeEntry( const Addressee& addr, const TQString& email ) +{ + removeEntry( addr.uid(), email ); + // Also remove entries with the full name as uid (for the kolab thing) + removeEntry( addr.formattedName(), email ); +} + +void KPIM::DistributionList::removeEntry( const TQString& uid, const TQString& email ) +{ + Q_ASSERT( !email.isEmpty() || email.isNull() ); // hopefully never called with "", would lead to confusion + ParseList parseList = parseCustom( custom( "KADDRESSBOOK", s_customFieldName ) ); + TQString str; + for( ParseList::ConstIterator it = parseList.begin(); it != parseList.end(); ++it ) { + const TQString thisUid = (*it).first; + const TQString thisEmail = (*it).second; + if ( thisUid == uid && thisEmail == email ) { + continue; // remove that one + } + str += ";" + thisUid + "," + thisEmail; + } + if ( str.isEmpty() ) + str = ";"; // keep something, for isDistributionList to work + insertCustom( "KADDRESSBOOK", s_customFieldName, str ); // replace old value +} + +bool KPIM::DistributionList::isDistributionList( const KABC::Addressee& addr ) +{ + const TQString str = addr.custom( "KADDRESSBOOK", s_customFieldName ); + return !str.isEmpty(); +} + +// ###### KDE4: add findByFormattedName to KABC::AddressBook +static KABC::Addressee::List findByFormattedName( KABC::AddressBook* book, + const TQString& name, + bool caseSensitive = true ) +{ + KABC::Addressee::List res; + KABC::AddressBook::Iterator abIt; + for ( abIt = book->begin(); abIt != book->end(); ++abIt ) + { + if ( caseSensitive && (*abIt).formattedName() == name ) + res.append( *abIt ); + if ( !caseSensitive && (*abIt).formattedName().lower() == name.lower() ) + res.append( *abIt ); + } + return res; +} + +KPIM::DistributionList KPIM::DistributionList::findByName( KABC::AddressBook* book, + const TQString& name, + bool caseSensitive ) +{ + KABC::AddressBook::Iterator abIt; + for ( abIt = book->begin(); abIt != book->end(); ++abIt ) + { + if ( isDistributionList( *abIt ) ) { + if ( caseSensitive && (*abIt).formattedName() == name ) + return *abIt; + if ( !caseSensitive && (*abIt).formattedName().lower() == name.lower() ) + return *abIt; + } + } + return DistributionList(); +} + +static KABC::Addressee findByUidOrName( KABC::AddressBook* book, const TQString& uidOrName, const TQString& email ) +{ + KABC::Addressee a = book->findByUid( uidOrName ); + if ( a.isEmpty() ) { + // UID not found, maybe it is a name instead. + // If we have an email, let's use that for the lookup. + // [This is used by e.g. the Kolab resource] + if ( !email.isEmpty() ) { + KABC::Addressee::List lst = book->findByEmail( email ); + KABC::Addressee::List::ConstIterator listit = lst.begin(); + for ( ; listit != lst.end(); ++listit ) + if ( (*listit).formattedName() == uidOrName ) { + a = *listit; + break; + } + if ( !lst.isEmpty() && a.isEmpty() ) { // found that email, but no match on the fullname + a = lst.first(); // probably the last name changed + } + } + // If we don't have an email, or if we didn't find any match for it, look up by full name + if ( a.isEmpty() ) { + // (But this has to be done here, since when loading we might not have the entries yet) + KABC::Addressee::List lst = findByFormattedName( book, uidOrName ); + if ( !lst.isEmpty() ) + a = lst.first(); + } + } + return a; +} + +KPIM::DistributionList::Entry::List KPIM::DistributionList::entries( KABC::AddressBook* book ) const +{ + Entry::List res; + const TQString str = custom( "KADDRESSBOOK", s_customFieldName ); + const ParseList parseList = parseCustom( str ); + for( ParseList::ConstIterator it = parseList.begin(); it != parseList.end(); ++it ) { + const TQString uid = (*it).first; + const TQString email = (*it).second; + // look up contact + KABC::Addressee a = findByUidOrName( book, uid, email ); + if ( a.isEmpty() ) { + // ## The old DistributionListManager had a "missing entries" list... + kdWarning() << "Addressee not found: " << uid << endl; + } else { + res.append( Entry( a, email ) ); + } + } + return res; +} + +TQStringList KPIM::DistributionList::emails( KABC::AddressBook* book ) const +{ + TQStringList emails; + + const TQString str = custom( "KADDRESSBOOK", s_customFieldName ); + ParseList parseList = parseCustom( str ); + for( ParseList::ConstIterator it = parseList.begin(); it != parseList.end(); ++it ) { + const TQString thisUid = (*it).first; + const TQString thisEmail = (*it).second; + + // look up contact + KABC::Addressee a = findByUidOrName( book, thisUid, thisEmail ); + if ( a.isEmpty() ) { + // ## The old DistributionListManager had a "missing entries" list... + continue; + } + + const TQString email = thisEmail.isEmpty() ? a.fullEmail() : + a.fullEmail( thisEmail ); + if ( !email.isEmpty() ) { + emails.append( email ); + } + } + + return emails; +} + +TQValueList<KPIM::DistributionList> + KPIM::DistributionList::allDistributionLists( KABC::AddressBook* book ) +{ + TQValueList<KPIM::DistributionList> lst; + KABC::AddressBook::Iterator abIt; + for ( abIt = book->begin(); abIt != book->end(); ++abIt ) + { + if ( isDistributionList( *abIt ) ) { + lst.append( KPIM::DistributionList( *abIt ) ); + } + } + return lst; +} diff --git a/libtdepim/distributionlist.h b/libtdepim/distributionlist.h new file mode 100644 index 000000000..99391fca3 --- /dev/null +++ b/libtdepim/distributionlist.h @@ -0,0 +1,140 @@ +/* + This file is part of libtdepim. + Copyright (c) 2004-2005 David Faure <faure@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef DISTRIBUTIONLIST_H +#define DISTRIBUTIONLIST_H + +#include <kabc/addressee.h> + +namespace KABC { +class AddressBook; +} + +namespace KPIM { + +/** + * @short Distribution list of email addresses + * + * This class represents a list of email addresses. Each email address is + * associated with an address book entry. If the address book entry changes, the + * entry in the distribution list is automatically updated. + * + * This should go into kdelibs in KDE4. + * + * @author David Faure <faure@kde.org> + */ +class DistributionList : public KABC::Addressee +{ + public: + /** + * @short Distribution List Entry + * + * This class represents an entry of a distribution list. It consists of an + * addressee and an email address. If the email address is null, the + * preferred email address of the addressee is used. + */ + struct Entry + { + typedef TQValueList<Entry> List; + + Entry() {} + Entry( const Addressee &_addressee, const TQString &_email ) : + addressee( _addressee ), email( _email ) {} + + Addressee addressee; + TQString email; + }; + + typedef TQValueList<DistributionList> List; + + /** + * Create a distribution list. + */ + DistributionList(); + /** + * Create a distribution list from an addressee object + * (this is a kind of down-cast) + */ + DistributionList( const KABC::Addressee& addr ); + + /** + * Destructor. + */ + ~DistributionList() {} + + /// HACK: reimplemented from Addressee, but it's NOT virtual there + void setName( const TQString &name ); + + /// HACK: reimplemented from Addressee, but it's NOT virtual there + TQString name() const { return formattedName(); } + + /** + Insert an entry into this distribution list. If the entry already exists + nothing happens. + */ + void insertEntry( const Addressee &, const TQString &email=TQString() ); + + /** + Remove an entry from this distribution list. If the entry doesn't exist + nothing happens. + */ + void removeEntry( const Addressee &, const TQString &email=TQString() ); + + /// Overload, used by resources to avoid looking up the addressee + void insertEntry( const TQString& uid, const TQString& email=TQString() ); + /// Overload, used by resources to avoid looking up the addressee + void removeEntry( const TQString& uid, const TQString& email=TQString() ); + + + /** + Return list of email addresses, which belong to this distributon list. + These addresses can be directly used by e.g. a mail client. + @param book necessary to look up entries + */ + TQStringList emails( KABC::AddressBook* book ) const; + + /** + Return list of entries belonging to this distribution list. This function + is mainly useful for a distribution list editor. + @param book necessary to look up entries + */ + Entry::List entries( KABC::AddressBook* book ) const; + + // KDE4: should be a method of Addressee + static bool isDistributionList( const KABC::Addressee& addr ); + + // KDE4: should be a method of AddressBook + static DistributionList findByName( KABC::AddressBook* book, + const TQString& name, + bool caseSensitive = true ); + // KDE4: should be a method of AddressBook + // A bit slow (but no more than findByName). + // From KAddressbook, use Core::distributionLists() instead. + static TQValueList<DistributionList> allDistributionLists( KABC::AddressBook* book ); + + + private: + // can't have any data here, use Addressee's methods instead +}; + +} + +#endif /* DISTRIBUTIONLIST_H */ + diff --git a/libtdepim/embeddedurlpage.cpp b/libtdepim/embeddedurlpage.cpp new file mode 100644 index 000000000..3971bf367 --- /dev/null +++ b/libtdepim/embeddedurlpage.cpp @@ -0,0 +1,71 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2005 Reinhold Kainhofer <reinhold@kainhofer.com> + Part of loadContents() copied from the kpartsdesignerplugin: + Copyright (C) 2005, David Faure <faure@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "embeddedurlpage.h" +#include <kparts/componentfactory.h> +#include <kparts/browserextension.h> +#include <kparts/part.h> +#include <kmimetype.h> +#include <klocale.h> +#include <tqlayout.h> +#include <tqlabel.h> + +using namespace KPIM; + +EmbeddedURLPage::EmbeddedURLPage( const TQString &url, const TQString &mimetype, + TQWidget *parent, const char *name ) + : TQWidget( parent, name ), mUri(url), mMimeType( mimetype ), mPart( 0 ) +{ + initGUI( url, mimetype ); +} + +void EmbeddedURLPage::initGUI( const TQString &url, const TQString &/*mimetype*/ ) +{ + TQVBoxLayout *tqlayout = new TQVBoxLayout( this ); + tqlayout->setAutoAdd( true ); + new TQLabel( i18n("Showing URL %1").tqarg( url ), this ); +} + +void EmbeddedURLPage::loadContents() +{ + if ( !mPart ) { + if ( mMimeType.isEmpty() || mUri.isEmpty() ) + return; + TQString mimetype = mMimeType; + if ( mimetype == "auto" ) + mimetype == KMimeType::findByURL( mUri )->name(); + // "this" is both the parent widget and the parent object + mPart = KParts::ComponentFactory::createPartInstanceFromQuery<KParts::ReadOnlyPart>( mimetype, TQString(), TQT_TQWIDGET(this), 0, TQT_TQOBJECT(this), 0 ); + if ( mPart ) { + mPart->openURL( mUri ); + mPart->widget()->show(); + } +//void KParts::BrowserExtension::openURLRequestDelayed( const KURL &url, const KParts::URLArgs &args = KParts::URLArgs() ) + KParts::BrowserExtension* be = KParts::BrowserExtension::childObject( mPart ); + connect( be, TQT_SIGNAL( openURLRequestDelayed( const KURL &, const KParts::URLArgs & ) ), +// mPart, TQT_SLOT( openURL( const KURL & ) ) ); + this, TQT_SIGNAL( openURL( const KURL & ) ) ); + } +} + +#include "embeddedurlpage.moc" diff --git a/libtdepim/embeddedurlpage.h b/libtdepim/embeddedurlpage.h new file mode 100644 index 000000000..c306466cd --- /dev/null +++ b/libtdepim/embeddedurlpage.h @@ -0,0 +1,54 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2005 Reinhold Kainhofer <reinhold@kainhofer.com> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef KPIM_EMBEDDEDURLPAGE_H +#define KPIM_EMBEDDEDURLPAGE_H + +#include <tqwidget.h> +#include <tdepimmacros.h> +#include <kurl.h> + +namespace KParts { class ReadOnlyPart; } + +namespace KPIM { + +class KDE_EXPORT EmbeddedURLPage : public TQWidget +{ + Q_OBJECT + TQ_OBJECT + public: + EmbeddedURLPage( const TQString &url, const TQString &mimetype, + TQWidget *parent, const char *name = 0 ); + + public slots: + void loadContents(); + signals: + void openURL( const KURL &url ); + private: + void initGUI( const TQString &url, const TQString &mimetype ); + + TQString mUri; + TQString mMimeType; + KParts::ReadOnlyPart* mPart; +}; + +} + +#endif diff --git a/libtdepim/groupwarejob.cpp b/libtdepim/groupwarejob.cpp new file mode 100644 index 000000000..bd413e12b --- /dev/null +++ b/libtdepim/groupwarejob.cpp @@ -0,0 +1,47 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "groupwarejob.h" + +#include <kio/job.h> +#include <kdebug.h> + +using namespace KIO; + +KIO::TransferJob *GroupwareJob::getCalendar( const KURL &u ) +{ + KURL url = u; + url.setPath( "/calendar/" ); + + kdDebug() << "GroupwareJob::getCalendar(): URL: " << url << endl; + + return KIO::get( url, false, false ); +} + +KIO::TransferJob *GroupwareJob::getAddressBook( const KURL &u ) +{ + KURL url = u; + url.setPath( "/addressbook/" ); + + kdDebug() << "GroupwareJob::getAddressBook(): URL: " << url << endl; + + return KIO::get( url, false, false ); +} diff --git a/libtdepim/groupwarejob.h b/libtdepim/groupwarejob.h new file mode 100644 index 000000000..bf5a33f6e --- /dev/null +++ b/libtdepim/groupwarejob.h @@ -0,0 +1,37 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef KIO_GROUPWAREJOB_H +#define KIO_GROUPWAREJOB_H + +#include <kio/jobclasses.h> + +namespace KIO { + +class GroupwareJob +{ + public: + static KIO::TransferJob *getCalendar( const KURL & ); + static KIO::TransferJob *getAddressBook( const KURL & ); +}; + +} + +#endif diff --git a/libtdepim/htmldiffalgodisplay.cpp b/libtdepim/htmldiffalgodisplay.cpp new file mode 100644 index 000000000..726854e4e --- /dev/null +++ b/libtdepim/htmldiffalgodisplay.cpp @@ -0,0 +1,97 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <kglobalsettings.h> + +#include <libtdepim/htmldiffalgodisplay.h> + +using namespace KPIM; + +static TQString textToHTML( const TQString &text ) +{ + return TQStyleSheet::convertFromPlainText( text ); +} + +HTMLDiffAlgoDisplay::HTMLDiffAlgoDisplay( TQWidget *parent ) + : KTextBrowser( parent ) +{ + setWrapPolicy( TQTextEdit::AtWordBoundary ); + setVScrollBarMode( TQScrollView::AlwaysOff ); + setHScrollBarMode( TQScrollView::AlwaysOff ); +} + +void HTMLDiffAlgoDisplay::begin() +{ + clear(); + mText = ""; + + mText.append( "<html>" ); + mText.append( TQString( "<body text=\"%1\" bgcolor=\"%2\">" ) + .tqarg( KGlobalSettings::textColor().name() ) + .tqarg( KGlobalSettings::baseColor().name() ) ); + + mText.append( "<center><table>" ); + mText.append( TQString( "<tr><th></th><th align=\"center\">%1</th><td> </td><th align=\"center\">%2</th></tr>" ) + .tqarg( mLeftTitle ) + .tqarg( mRightTitle ) ); +} + +void HTMLDiffAlgoDisplay::end() +{ + mText.append( "</table></center>" + "</body>" + "</html>" ); + + setText( mText ); +} + +void HTMLDiffAlgoDisplay::setLeftSourceTitle( const TQString &title ) +{ + mLeftTitle = title; +} + +void HTMLDiffAlgoDisplay::setRightSourceTitle( const TQString &title ) +{ + mRightTitle = title; +} + +void HTMLDiffAlgoDisplay::additionalLeftField( const TQString &id, const TQString &value ) +{ + mText.append( TQString( "<tr><td align=\"right\"><b>%1:</b></td><td bgcolor=\"#9cff83\">%2</td><td></td><td></td></tr>" ) + .tqarg( id ) + .tqarg( textToHTML( value ) ) ); +} + +void HTMLDiffAlgoDisplay::additionalRightField( const TQString &id, const TQString &value ) +{ + mText.append( TQString( "<tr><td align=\"right\"><b>%1:</b></td><td></td><td></td><td bgcolor=\"#9cff83\">%2</td></tr>" ) + .tqarg( id ) + .tqarg( textToHTML( value ) ) ); +} + +void HTMLDiffAlgoDisplay::conflictField( const TQString &id, const TQString &leftValue, + const TQString &rightValue ) +{ + mText.append( TQString( "<tr><td align=\"right\"><b>%1:</b></td><td bgcolor=\"#ff8686\">%2</td><td></td><td bgcolor=\"#ff8686\">%3</td></tr>" ) + .tqarg( id ) + .tqarg( textToHTML( leftValue ) ) + .tqarg( textToHTML( rightValue ) ) ); +} diff --git a/libtdepim/htmldiffalgodisplay.h b/libtdepim/htmldiffalgodisplay.h new file mode 100644 index 000000000..35d95f5c8 --- /dev/null +++ b/libtdepim/htmldiffalgodisplay.h @@ -0,0 +1,54 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef KPIM_HTMLDIFFALGODISPLAY_H +#define KPIM_HTMLDIFFALGODISPLAY_H + +#include "diffalgo.h" + +#include <ktextbrowser.h> +#include <tdepimmacros.h> + +namespace KPIM { + +class KDE_EXPORT HTMLDiffAlgoDisplay : virtual public DiffAlgoDisplay, public KTextBrowser +{ + public: + HTMLDiffAlgoDisplay( TQWidget *parent ); + + void begin(); + void end(); + void setLeftSourceTitle( const TQString &title ); + void setRightSourceTitle( const TQString &title ); + void additionalLeftField( const TQString &id, const TQString &value ); + void additionalRightField( const TQString &id, const TQString &value ); + void conflictField( const TQString &id, const TQString &leftValue, + const TQString &rightValue ); + + private: + TQString mLeftTitle; + TQString mRightTitle; + TQString mText; +}; + +} + +#endif diff --git a/libtdepim/icons/CMakeLists.txt b/libtdepim/icons/CMakeLists.txt new file mode 100644 index 000000000..189a3a200 --- /dev/null +++ b/libtdepim/icons/CMakeLists.txt @@ -0,0 +1,12 @@ +################################################# +# +# (C) 2010-2011 Serghei Amelian +# serghei (DOT) amelian (AT) gmail.com +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +tde_install_icons() diff --git a/libtdepim/icons/Makefile.am b/libtdepim/icons/Makefile.am new file mode 100644 index 000000000..2e31aecbf --- /dev/null +++ b/libtdepim/icons/Makefile.am @@ -0,0 +1,2 @@ +KDE_ICON=AUTO + diff --git a/libtdepim/icons/cr22-action-button_fewer.png b/libtdepim/icons/cr22-action-button_fewer.png Binary files differnew file mode 100644 index 000000000..96919575a --- /dev/null +++ b/libtdepim/icons/cr22-action-button_fewer.png diff --git a/libtdepim/icons/cr22-action-button_more.png b/libtdepim/icons/cr22-action-button_more.png Binary files differnew file mode 100644 index 000000000..31c064ba9 --- /dev/null +++ b/libtdepim/icons/cr22-action-button_more.png diff --git a/libtdepim/infoextension.cpp b/libtdepim/infoextension.cpp new file mode 100644 index 000000000..25f7e8ba7 --- /dev/null +++ b/libtdepim/infoextension.cpp @@ -0,0 +1,38 @@ +/* + This file is part of libtdepim. + + Copyright (C) 2003 Sven Lüppken <sven@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "infoextension.h" + +#include <tqwidget.h> +#include <kparts/part.h> + +using namespace KParts; + +InfoExtension::InfoExtension( KParts::ReadOnlyPart *parent, const char* name) +: TQObject(parent, name), d(0) +{ +} + +InfoExtension::~InfoExtension() +{ +} + +#include "infoextension.moc" diff --git a/libtdepim/infoextension.h b/libtdepim/infoextension.h new file mode 100644 index 000000000..b857dc4a5 --- /dev/null +++ b/libtdepim/infoextension.h @@ -0,0 +1,62 @@ +/* + This file is part of libtdepim. + + Copyright (C) 2003 Sven Lüppken <sven@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + */ + +#ifndef INFOEXTENSION_H +#define INFOEXTENSION_H + +#include <tqobject.h> +#include <tdepimmacros.h> + +class TQWidget; + +namespace KParts +{ + + class ReadOnlyPart; + + /** + * Provides a way to get information out of a PIM-Part + **/ + class KDE_EXPORT InfoExtension : public TQObject + { + Q_OBJECT + TQ_OBJECT + + public: + /** + * Constucts an InfoExtension. + * + * @param parent The parent widget. + * @param name The name of the class. + **/ + InfoExtension( KParts::ReadOnlyPart *parent, const char* name); + ~InfoExtension(); + + private: + class InfoExtensionPrivate; + InfoExtensionPrivate *d; + + signals: + void textChanged( const TQString& ); + void iconChanged( const TQPixmap& ); + }; +} +#endif // INFOEXTENSION_H diff --git a/libtdepim/interfaces/AddressBookServiceIface.h b/libtdepim/interfaces/AddressBookServiceIface.h new file mode 100644 index 000000000..e07ac3e76 --- /dev/null +++ b/libtdepim/interfaces/AddressBookServiceIface.h @@ -0,0 +1,59 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2003 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef ADDRESSBOOKSERVICEIFACE_H +#define ADDRESSBOOKSERVICEIFACE_H + +#include <dcopobject.h> +#include <dcopref.h> +#include <kurl.h> +#include <tqstring.h> +#include <tqcstring.h> +#include <tdepimmacros.h> + +namespace KPIM { + +#define AddressBookServiceIface KDE_EXPORT AddressBookServiceIface + class AddressBookServiceIface : virtual public DCOPObject +#undef AddressBookServiceIface + { + K_DCOP + + k_dcop: + /** + This method will add a vcard to the address book. + + @param vCard The vCard in string representation. + */ + virtual void importVCardFromData( const TQString& vCard ) = 0; + + /** + This method will add a vcard to the address book. + + @param url The url where the vcard is located. + */ + virtual void importVCard( const KURL& url ) = 0; + }; + +} + +#endif + diff --git a/libtdepim/interfaces/MailTransportServiceIface.h b/libtdepim/interfaces/MailTransportServiceIface.h new file mode 100644 index 000000000..d256775a3 --- /dev/null +++ b/libtdepim/interfaces/MailTransportServiceIface.h @@ -0,0 +1,93 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2003 Daniel Molkentin <molkentin@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef MAILTRANSPORTSERVICEIFACE_H +#define MAILTRANSPORTSERVICEIFACE_H + +#include <dcopobject.h> +#include <dcopref.h> +#include <kurl.h> +#include <tqstring.h> +#include <tqcstring.h> + +#include <tdepimmacros.h> + +namespace KPim { + +#define MailTransportServiceIface KDE_EXPORT MailTransportServiceIface + class MailTransportServiceIface : virtual public DCOPObject +#undef MailTransportServiceIface + { + K_DCOP + + k_dcop: + /** + * This method will compose a message and send it using the mailers + * preferred transport. The mimetype of the attachments passed is + * determined using mime magic. + * + * @return true when the message was send successfully, false on failure. + **/ + virtual bool sendMessage( const TQString& from, const TQString& to, + const TQString& cc, const TQString& bcc, + const TQString& subject, const TQString& body, + const KURL::List& attachments ) = 0; + + /** + * This method basically behaves like the one above, but takes only one + * attachment as TQByteArray. This is useful if you want to attach simple + * text files (e.g. a vCalendar). The mimetype is determined using + * mime magic. + * + * @return true when the message was send successfully, false on failure. + **/ + virtual bool sendMessage( const TQString& from, const TQString& to, + const TQString& cc, const TQString& bcc, + const TQString& subject, const TQString& body, + const TQByteArray& attachment ) = 0; + + k_dcop_hidden: + /** + * This method is deprecated. Use the corresponding method with the + * additional parameter from instead. + **/ + // FIXME KDE 4.0: Remove this. + virtual bool sendMessage( const TQString& to, + const TQString& cc, const TQString& bcc, + const TQString& subject, const TQString& body, + const KURL::List& attachments ) = 0; + + /** + * This method is deprecated. Use the corresponding method with the + * additional parameter from instead. + **/ + // FIXME KDE 4.0: Remove this. + virtual bool sendMessage( const TQString& to, + const TQString& cc, const TQString& bcc, + const TQString& subject, const TQString& body, + const TQByteArray& attachment ) = 0; + + }; + +} + +#endif // MAILTRANSPORTSERVICEIFACE_H + diff --git a/libtdepim/interfaces/Makefile.am b/libtdepim/interfaces/Makefile.am new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/libtdepim/interfaces/Makefile.am diff --git a/libtdepim/kabcresourcecached.cpp b/libtdepim/kabcresourcecached.cpp new file mode 100644 index 000000000..eece76351 --- /dev/null +++ b/libtdepim/kabcresourcecached.cpp @@ -0,0 +1,417 @@ +/* + This file is part of libtdepim. + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <tqfile.h> + +#include <kabc/vcardconverter.h> +#include <kdebug.h> +#include <klocale.h> +#include <kstandarddirs.h> + +#include "kabcresourcecached.h" + +using namespace KABC; + +ResourceCached::ResourceCached( const KConfig *config ) + : KABC::Resource( config ), mIdMapper( "kabc/uidmaps/" ), + mReloadPolicy( ReloadInterval ), mReloadInterval( 10 ), + mKABCReloadTimer( 0, "mKABCReloadTimer" ), mReloaded( false ), + mSavePolicy( SaveDelayed ), mSaveInterval( 10 ), + mKABCSaveTimer( 0, "mKABCSaveTimer" ) +{ + connect( &mKABCReloadTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotKABCReload() ) ); + connect( &mKABCSaveTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotKABCSave() ) ); + + if (config) + this->readConfig(const_cast<KConfig *>(config)); +} + +ResourceCached::~ResourceCached() +{ +} + +void ResourceCached::setReloadPolicy( int i ) +{ + mReloadPolicy = i; + + setupReloadTimer(); +} + +int ResourceCached::reloadPolicy() const +{ + return mReloadPolicy; +} + +void ResourceCached::setReloadInterval( int minutes ) +{ + mReloadInterval = minutes; +} + +int ResourceCached::reloadInterval() const +{ + return mReloadInterval; +} + +void ResourceCached::setSavePolicy( int i ) +{ + mSavePolicy = i; + + setupSaveTimer(); +} + +int ResourceCached::savePolicy() const +{ + return mSavePolicy; +} + +void ResourceCached::setSaveInterval( int minutes ) +{ + mSaveInterval = minutes; +} + +int ResourceCached::saveInterval() const +{ + return mSaveInterval; +} + +void ResourceCached::writeConfig( KConfig *config ) +{ + config->writeEntry( "ReloadPolicy", mReloadPolicy ); + config->writeEntry( "ReloadInterval", mReloadInterval ); + + config->writeEntry( "SavePolicy", mSavePolicy ); + config->writeEntry( "SaveInterval", mSaveInterval ); + + config->writeEntry( "LastLoad", mLastLoad ); + config->writeEntry( "LastSave", mLastSave ); + + KABC::Resource::writeConfig( config ); +} + +void ResourceCached::readConfig( KConfig *config ) +{ + mReloadPolicy = config->readNumEntry( "ReloadPolicy", ReloadNever ); + mReloadInterval = config->readNumEntry( "ReloadInterval", 10 ); + + mSaveInterval = config->readNumEntry( "SaveInterval", 10 ); + mSavePolicy = config->readNumEntry( "SavePolicy", SaveNever ); + + mLastLoad = config->readDateTimeEntry( "LastLoad" ); + mLastSave = config->readDateTimeEntry( "LastSave" ); + + setupSaveTimer(); + setupReloadTimer(); +} + +void ResourceCached::setupSaveTimer() +{ + if ( mSavePolicy == SaveInterval ) { + kdDebug(5800) << "ResourceCached::setSavePolicy(): start save timer (interval " + << mSaveInterval << " minutes)." << endl; + mKABCSaveTimer.start( mSaveInterval * 60 * 1000 ); // n minutes + } else { + mKABCSaveTimer.stop(); + } +} + +void ResourceCached::setupReloadTimer() +{ + if ( mReloadPolicy == ReloadInterval ) { + kdDebug(5800) << "ResourceCached::setSavePolicy(): start reload timer " + "(interval " << mReloadInterval << " minutes)" << endl; + mKABCReloadTimer.start( mReloadInterval * 60 * 1000 ); // n minutes + } else { + mKABCReloadTimer.stop(); + } +} + +void ResourceCached::slotKABCReload() +{ + if ( !isActive() ) return; + + kdDebug(5800) << "ResourceCached::slotKABCReload()" << endl; + + load(); +} + +void ResourceCached::slotKABCSave() +{ + if ( !isActive() ) return; + + kdDebug(5800) << "ResourceCached::slotKABCSave()" << endl; + + KABC::Ticket *ticket = requestSaveTicket(); + if ( ticket ) { + save( ticket ); + } +} + +void ResourceCached::insertAddressee( const Addressee &addr ) +{ + if ( !mAddrMap.contains( addr.uid() ) ) { // new contact + if ( mDeletedAddressees.contains( addr.uid() ) ) { + // it was first removed, then added, so it's an update... + mDeletedAddressees.remove( addr.uid() ); + + mAddrMap.insert( addr.uid(), addr ); + mChangedAddressees.insert( addr.uid(), addr ); + return; + } + + mAddrMap.insert( addr.uid(), addr ); + mAddedAddressees.insert( addr.uid(), addr ); + } else { + KABC::Addressee oldAddressee = mAddrMap.find( addr.uid() ).data(); + if ( oldAddressee != addr ) { + mAddrMap.remove( addr.uid() ); + mAddrMap.insert( addr.uid(), addr ); + mChangedAddressees.insert( addr.uid(), addr ); + } + } +} + +void ResourceCached::removeAddressee( const Addressee &addr ) +{ + if ( mAddedAddressees.contains( addr.uid() ) ) { + mAddedAddressees.remove( addr.uid() ); + return; + } + + if ( mDeletedAddressees.find( addr.uid() ) == mDeletedAddressees.end() ) + mDeletedAddressees.insert( addr.uid(), addr ); + + mAddrMap.remove( addr.uid() ); +} + +void ResourceCached::loadCache() +{ + mAddrMap.clear(); + + setIdMapperIdentifier(); + mIdMapper.load(); + + // load cache + TQFile file( cacheFile() ); + if ( !file.open( IO_ReadOnly ) ) + return; + + + KABC::VCardConverter converter; +#if defined(KABC_VCARD_ENCODING_FIX) + KABC::Addressee::List list = converter.parseVCardsRaw( file.readAll().data() ); +#else + KABC::Addressee::List list = converter.parseVCards( TQString::fromUtf8( file.readAll() ) ); +#endif + KABC::Addressee::List::Iterator it; + + for ( it = list.begin(); it != list.end(); ++it ) { + (*it).setResource( this ); + (*it).setChanged( false ); + mAddrMap.insert( (*it).uid(), *it ); + } + + file.close(); +} + +void ResourceCached::saveCache() +{ + setIdMapperIdentifier(); + mIdMapper.save(); + + // save cache + TQFile file( cacheFile() ); + if ( !file.open( IO_WriteOnly ) ) + return; + + KABC::Addressee::List list = mAddrMap.values(); + + KABC::VCardConverter converter; +#if defined(KABC_VCARD_ENCODING_FIX) + TQCString vCard = converter.createVCardsRaw( list ); + file.writeBlock( vCard, vCard.length() ); +#else + TQString vCard = converter.createVCards( list ); + file.writeBlock( vCard.utf8(), vCard.utf8().length() ); +#endif + file.close(); +} + +void ResourceCached::clearCache() +{ + // TEST ME + mAddrMap.clear(); +} + +void ResourceCached::cleanUpCache( const KABC::Addressee::List &addrList ) +{ + // load cache + TQFile file( cacheFile() ); + if ( !file.open( IO_ReadOnly ) ) + return; + + + KABC::VCardConverter converter; +#if defined(KABC_VCARD_ENCODING_FIX) + KABC::Addressee::List list = converter.parseVCardsRaw( file.readAll().data() ); +#else + KABC::Addressee::List list = converter.parseVCards( TQString::fromUtf8( file.readAll() ) ); +#endif + KABC::Addressee::List::Iterator cacheIt; + KABC::Addressee::List::ConstIterator it; + + for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) { + bool found = false; + for ( it = addrList.begin(); it != addrList.end(); ++it ) { + if ( (*it).uid() == (*cacheIt).uid() ) + found = true; + } + + if ( !found ) { + mIdMapper.removeRemoteId( mIdMapper.remoteId( (*cacheIt).uid() ) ); + mAddrMap.remove( (*cacheIt).uid() ); + } + } + + file.close(); +} + +KPIM::IdMapper& ResourceCached::idMapper() +{ + return mIdMapper; +} + +bool ResourceCached::hasChanges() const +{ + return !( mAddedAddressees.isEmpty() && + mChangedAddressees.isEmpty() && + mDeletedAddressees.isEmpty() ); +} + +void ResourceCached::clearChanges() +{ + mAddedAddressees.clear(); + mChangedAddressees.clear(); + mDeletedAddressees.clear(); +} + +void ResourceCached::clearChange( const KABC::Addressee &addr ) +{ + mAddedAddressees.remove( addr.uid() ); + mChangedAddressees.remove( addr.uid() ); + mDeletedAddressees.remove( addr.uid() ); +} + +void ResourceCached::clearChange( const TQString &uid ) +{ + mAddedAddressees.remove( uid ); + mChangedAddressees.remove( uid ); + mDeletedAddressees.remove( uid ); +} + +KABC::Addressee::List ResourceCached::addedAddressees() const +{ + return mAddedAddressees.values(); +} + +KABC::Addressee::List ResourceCached::changedAddressees() const +{ + return mChangedAddressees.values(); +} + +KABC::Addressee::List ResourceCached::deletedAddressees() const +{ + return mDeletedAddressees.values(); +} + +TQString ResourceCached::cacheFile() const +{ + return locateLocal( "cache", "kabc/kresources/" + identifier() ); +} + +TQString ResourceCached::changesCacheFile( const TQString &type ) const +{ + return locateLocal( "cache", "kabc/changescache/" + identifier() + "_" + type ); +} + +void ResourceCached::saveChangesCache( const TQMap<TQString, KABC::Addressee> &map, const TQString &type ) +{ + TQFile file( changesCacheFile( type ) ); + + const KABC::Addressee::List list = map.values(); + if ( list.isEmpty() ) { + file.remove(); + } else { + if ( !file.open( IO_WriteOnly ) ) { + kdError() << "Can't open changes cache file '" << file.name() << "' for saving." << endl; + return; + } + + KABC::VCardConverter converter; +#if defined(KABC_VCARD_ENCODING_FIX) + const TQCString vCards = converter.createVCardsRaw( list ); + file.writeBlock( vCards, vCards.length() ); +#else + const TQString vCards = converter.createVCards( list ); + TQCString content = vCards.utf8(); + file.writeBlock( content, content.length() ); +#endif + } +} + +void ResourceCached::saveChangesCache() +{ + saveChangesCache( mAddedAddressees, "added" ); + saveChangesCache( mDeletedAddressees, "deleted" ); + saveChangesCache( mChangedAddressees, "changed" ); +} + +void ResourceCached::loadChangesCache( TQMap<TQString, KABC::Addressee> &map, const TQString &type ) +{ + TQFile file( changesCacheFile( type ) ); + if ( !file.open( IO_ReadOnly ) ) + return; + + KABC::VCardConverter converter; + +#if defined(KABC_VCARD_ENCODING_FIX) + const KABC::Addressee::List list = converter.parseVCardsRaw( file.readAll().data() ); +#else + const KABC::Addressee::List list = converter.parseVCards( TQString::fromUtf8( file.readAll() ) ); +#endif + KABC::Addressee::List::ConstIterator it; + for ( it = list.begin(); it != list.end(); ++it ) + map.insert( (*it).uid(), *it ); + + file.close(); +} + +void ResourceCached::loadChangesCache() +{ + loadChangesCache( mAddedAddressees, "added" ); + loadChangesCache( mDeletedAddressees, "deleted" ); + loadChangesCache( mChangedAddressees, "changed" ); +} + +void ResourceCached::setIdMapperIdentifier() +{ + mIdMapper.setIdentifier( type() + "_" + identifier() ); +} + +#include "kabcresourcecached.moc" diff --git a/libtdepim/kabcresourcecached.h b/libtdepim/kabcresourcecached.h new file mode 100644 index 000000000..2cf0352ef --- /dev/null +++ b/libtdepim/kabcresourcecached.h @@ -0,0 +1,195 @@ +/* + This file is part of libtdepim. + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef KABC_RESOURCECACHED_H +#define KABC_RESOURCECACHED_H + +#include <kabc/resource.h> +#include <tdepimmacros.h> + +#include <tqdatetime.h> +#include <tqtimer.h> + +#include "libemailfunctions/idmapper.h" + +namespace KABC { + +class KDE_EXPORT ResourceCached : public Resource +{ + Q_OBJECT + TQ_OBJECT + + public: + /** + Reload policy. + + @see setReloadPolicy(), reloadPolicy() + */ + enum { ReloadNever, ReloadOnStartup, ReloadInterval }; + /** + Save policy. + + @see setSavePolicy(), savePolicy() + */ + enum { SaveNever, SaveOnExit, SaveInterval, SaveDelayed, SaveAlways }; + + ResourceCached( const KConfig* ); + ~ResourceCached(); + + /** + Set reload policy. This controls when the cache is refreshed. + + ReloadNever never reload + ReloadOnStartup reload when resource is started + ReloadInterval reload regularly after given interval + */ + void setReloadPolicy( int policy ); + /** + Return reload policy. + + @see setReloadPolicy() + */ + int reloadPolicy() const; + + /** + Set reload interval in minutes which is used when reload policy is + ReloadInterval. + */ + void setReloadInterval( int minutes ); + + /** + Return reload interval in minutes. + */ + int reloadInterval() const; + + /** + Set save policy. This controls when the cache is refreshed. + + SaveNever never save + SaveOnExit save when resource is exited + SaveInterval save regularly after given interval + SaveDelayed save after small delay + SaveAlways save on every change + */ + void setSavePolicy( int policy ); + /** + Return save policy. + + @see setsavePolicy() + */ + int savePolicy() const; + + /** + Set save interval in minutes which is used when save policy is + SaveInterval. + */ + void setSaveInterval( int minutes ); + + /** + Return save interval in minutes. + */ + int saveInterval() const; + + void setupSaveTimer(); + void setupReloadTimer(); + + /** + Reads the resource specific config from disk. + */ + virtual void readConfig( KConfig *config ); + + /** + Writes the resource specific config to disk. + */ + virtual void writeConfig( KConfig *config ); + + /** + Insert an addressee into the resource. + */ + virtual void insertAddressee( const Addressee& ); + + /** + Removes an addressee from resource. + */ + virtual void removeAddressee( const Addressee& addr ); + + void loadCache(); + void saveCache(); + void clearCache(); + void cleanUpCache( const KABC::Addressee::List &list ); + + /** + Returns a reference to the id mapper. + */ + KPIM::IdMapper& idMapper(); + + bool hasChanges() const; + void clearChanges(); + void clearChange( const KABC::Addressee& ); + void clearChange( const TQString& ); + + KABC::Addressee::List addedAddressees() const; + KABC::Addressee::List changedAddressees() const; + KABC::Addressee::List deletedAddressees() const; + + protected: + virtual TQString cacheFile() const; + + /** + Functions for keeping the changes persistent. + */ + virtual TQString changesCacheFile( const TQString& ) const; + void loadChangesCache( TQMap<TQString, KABC::Addressee>&, const TQString& ); + void loadChangesCache(); + void saveChangesCache( const TQMap<TQString, KABC::Addressee>&, const TQString& ); + void saveChangesCache(); + + void setIdMapperIdentifier(); + + private: + TQMap<TQString, KABC::Addressee> mAddedAddressees; + TQMap<TQString, KABC::Addressee> mChangedAddressees; + TQMap<TQString, KABC::Addressee> mDeletedAddressees; + + KPIM::IdMapper mIdMapper; + + class ResourceCachedPrivate; + ResourceCachedPrivate *d; + + int mReloadPolicy; + int mReloadInterval; + TQTimer mKABCReloadTimer; + bool mReloaded; + + int mSavePolicy; + int mSaveInterval; + TQTimer mKABCSaveTimer; + + TQDateTime mLastLoad; + TQDateTime mLastSave; + + protected slots: + void slotKABCReload(); + void slotKABCSave(); +}; + +} + +#endif diff --git a/libtdepim/kabcresourcenull.h b/libtdepim/kabcresourcenull.h new file mode 100644 index 000000000..7d77b89b7 --- /dev/null +++ b/libtdepim/kabcresourcenull.h @@ -0,0 +1,46 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef KABC_RESOURCENULL_H +#define KABC_RESOURCENULL_H + +#include <kabc/resource.h> + +namespace KABC { + +/** + This resource does nothing. +*/ +class ResourceNull : public Resource +{ + public: + ResourceNull( const KConfig *cfg ) : Resource( cfg ) {} + ResourceNull() : Resource( 0 ) {} + virtual ~ResourceNull() {} + + Ticket *requestSaveTicket() { return 0; } + void releaseSaveTicket( Ticket * ) {} + bool load() { return false; } + bool save( Ticket * ) { return false; } +}; + +} + +#endif diff --git a/libtdepim/kaccount.cpp b/libtdepim/kaccount.cpp new file mode 100644 index 000000000..392fea265 --- /dev/null +++ b/libtdepim/kaccount.cpp @@ -0,0 +1,62 @@ +/* -*- c++ -*- + kaccount.cpp + + This file is part of KMail, the KDE mail client. + + Copyright (C) 2002 Carsten Burghardt <burghardt@kde.org> + + KMail is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License, version 2, as + published by the Free Software Foundation. + + KMail 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the TQt library by Trolltech AS, Norway (or with modified versions + of TQt that use the same license as TQt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + TQt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#include "kaccount.h" + +#include <kconfig.h> + +KAccount::KAccount( const uint id, const TQString &name, const Type type ) + : mId( id ), mName( name ), mType( type ) +{ +} + +void KAccount::writeConfig( KConfig &config, const TQString &group ) +{ + TQString oldGroup = config.group(); + if (!group.isEmpty()) + config.setGroup(group); + config.writeEntry("Id", mId); + config.writeEntry("Name", mName); + if (!group.isEmpty()) // restore + config.setGroup(oldGroup); +} + +void KAccount::readConfig( KConfig &config, const TQString &group ) +{ + TQString oldGroup = config.group(); + if (!group.isEmpty()) + config.setGroup(group); + mId = config.readUnsignedNumEntry("Id", 0); + mName = config.readEntry("Name"); + if (!group.isEmpty()) // restore + config.setGroup(oldGroup); +} diff --git a/libtdepim/kaccount.h b/libtdepim/kaccount.h new file mode 100644 index 000000000..d0a1b7f21 --- /dev/null +++ b/libtdepim/kaccount.h @@ -0,0 +1,92 @@ +/* -*- c++ -*- + kaccount.h + + This file is part of KMail, the KDE mail client. + Copyright (C) 2002 Carsten Burghardt <burghardt@kde.org> + + KMail is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License, version 2, as + published by the Free Software Foundation. + + KMail 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the TQt library by Trolltech AS, Norway (or with modified versions + of TQt that use the same license as TQt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + TQt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#ifndef __KACCOUNT +#define __KACCOUNT + +#include <tqstring.h> +#include <tdepimmacros.h> + +class KConfig; + +class KDE_EXPORT KAccount +{ + public: + /** Type information */ + enum Type { + Imap, + MBox, + Maildir, + News, + DImap, + Other + }; + + KAccount( const uint id = 0, const TQString &name = TQString(), + const Type type = Other ); + + /** + * Get/Set name + */ + TQString name() const { return mName; } + void setName( const TQString& name ) { mName = name; } + + /** + * Get/Set id + */ + uint id() const { return mId; } + void setId( const uint id ) { mId = id; } + + /** + * Get/Set type + */ + Type type() const { return mType; } + void setType( const Type type ) { mType = type; } + + /** + * Save the settings + * If the group is empty it must be preset in the KConfig + */ + void writeConfig( KConfig &config, const TQString &group = TQString() ); + + /** + * Read the settings + * If the group is empty it must be preset in the KConfig + */ + void readConfig( KConfig &config, const TQString &group = TQString() ); + + protected: + uint mId; + TQString mName; + Type mType; +}; + +#endif diff --git a/libtdepim/kaddrbook.cpp b/libtdepim/kaddrbook.cpp new file mode 100644 index 000000000..1036a263b --- /dev/null +++ b/libtdepim/kaddrbook.cpp @@ -0,0 +1,285 @@ +// -*- mode: C++; c-file-style: "gnu" -*- +// kaddrbook.cpp +// Author: Stefan Taferner <taferner@kde.org> +// This code is under GPL + +#include <config.h> + +#include "kaddrbook.h" + +#ifdef KDEPIM_NEW_DISTRLISTS +#include "distributionlist.h" +#else +#include <kabc/distributionlist.h> +#endif + +#include <kapplication.h> +#include <kdebug.h> +#include <klocale.h> +#include <kmessagebox.h> +#include <kdeversion.h> +#include <kabc/resource.h> +#include <kabc/stdaddressbook.h> +#include <kabc/vcardconverter.h> +#include <kabc/errorhandler.h> +#include <kresources/selectdialog.h> +#include <dcopref.h> +#include <dcopclient.h> + +#include <tqeventloop.h> +#include <tqregexp.h> + +#include <unistd.h> + +//----------------------------------------------------------------------------- +void KAddrBookExternal::openEmail( const TQString &addr, TQWidget *parent ) { + TQString email; + TQString name; + + KABC::Addressee::parseEmailAddress( addr, name, email ); + + KABC::AddressBook *ab = KABC::StdAddressBook::self( true ); + + // force a reload of the address book file so that changes that were made + // by other programs are loaded + ab->asyncLoad(); + + // if we have to reload the address book then we should also wait until + // it's completely reloaded +#if KDE_IS_VERSION(3,4,89) + // This ugly hack will be removed in 4.0 + while ( !ab->loadingHasFinished() ) { + TQApplication::eventLoop()->processEvents( TQEventLoop::ExcludeUserInput ); + + // use sleep here to reduce cpu usage + usleep( 100 ); + } +#endif + + KABC::Addressee::List addressees = ab->findByEmail( email ); + + if ( addressees.count() > 0 ) { + if ( kapp->dcopClient()->isApplicationRegistered( "kaddressbook" ) ){ + //make sure kaddressbook is loaded, otherwise showContactEditor + //won't work as desired, see bug #87233 + DCOPRef call ( "kaddressbook", "kaddressbook" ); + call.send( "newInstance()" ); + } else { + kapp->startServiceByDesktopName( "kaddressbook" ); + } + + DCOPRef call( "kaddressbook", "KAddressBookIface" ); + call.send( "showContactEditor(TQString)", addressees.first().uid() ); + } else { + //TODO: Enable the better message at the next string unfreeze +#if 0 + TQString text = i18n("<qt>The email address <b>%1</b> cannot be " + "found in your addressbook.</qt>").tqarg( email ); +#else + TQString text = email + " " + i18n( "is not in address book" ); +#endif + KMessageBox::information( parent, text, TQString(), "notInAddressBook" ); + } +} + +//----------------------------------------------------------------------------- +void KAddrBookExternal::addEmail( const TQString& addr, TQWidget *parent) { + TQString email; + TQString name; + + KABC::Addressee::parseEmailAddress( addr, name, email ); + + KABC::AddressBook *ab = KABC::StdAddressBook::self( true ); + + ab->setErrorHandler( new KABC::GuiErrorHandler( parent ) ); + + // force a reload of the address book file so that changes that were made + // by other programs are loaded + ab->asyncLoad(); + + // if we have to reload the address book then we should also wait until + // it's completely reloaded +#if KDE_IS_VERSION(3,4,89) + // This ugly hack will be removed in 4.0 + while ( !ab->loadingHasFinished() ) { + TQApplication::eventLoop()->processEvents( TQEventLoop::ExcludeUserInput ); + + // use sleep here to reduce cpu usage + usleep( 100 ); + } +#endif + + KABC::Addressee::List addressees = ab->findByEmail( email ); + + if ( addressees.isEmpty() ) { + KABC::Addressee a; + a.setNameFromString( name ); + a.insertEmail( email, true ); + + { + KConfig config( "kaddressbookrc" ); + config.setGroup( "General" ); + int type = config.readNumEntry( "FormattedNameType", 1 ); + + TQString name; + switch ( type ) { + case 1: + name = a.givenName() + " " + a.familyName(); + break; + case 2: + name = a.assembledName(); + break; + case 3: + name = a.familyName() + ", " + a.givenName(); + break; + case 4: + name = a.familyName() + " " + a.givenName(); + break; + case 5: + name = a.organization(); + break; + default: + name = ""; + break; + } + name.simplifyWhiteSpace(); + + a.setFormattedName( name ); + } + + if ( KAddrBookExternal::addAddressee( a ) ) { + TQString text = i18n("<qt>The email address <b>%1</b> was added to your " + "addressbook; you can add more information to this " + "entry by opening the addressbook.</qt>").tqarg( addr ); + KMessageBox::information( parent, text, TQString(), "addedtokabc" ); + } + } else { + TQString text = i18n("<qt>The email address <b>%1</b> is already in your " + "addressbook.</qt>").tqarg( addr ); + KMessageBox::information( parent, text, TQString(), + "alreadyInAddressBook" ); + } + ab->setErrorHandler( 0 ); +} + +void KAddrBookExternal::openAddressBook(TQWidget *) { + kapp->startServiceByDesktopName( "kaddressbook" ); +} + +void KAddrBookExternal::addNewAddressee( TQWidget* ) +{ + kapp->startServiceByDesktopName("kaddressbook"); + DCOPRef call("kaddressbook", "KAddressBookIface"); + call.send("newContact()"); +} + +bool KAddrBookExternal::addVCard( const KABC::Addressee& addressee, TQWidget *parent ) +{ + KABC::AddressBook *ab = KABC::StdAddressBook::self( true ); + bool inserted = false; + + ab->setErrorHandler( new KABC::GuiErrorHandler( parent ) ); + + KABC::Addressee::List addressees = + ab->findByEmail( addressee.preferredEmail() ); + + if ( addressees.isEmpty() ) { + if ( KAddrBookExternal::addAddressee( addressee ) ) { + TQString text = i18n("The VCard was added to your addressbook; " + "you can add more information to this " + "entry by opening the addressbook."); + KMessageBox::information( parent, text, TQString(), "addedtokabc" ); + inserted = true; + } + } else { + TQString text = i18n("The VCard's primary email address is already in " + "your addressbook; however, you may save the VCard " + "into a file and import it into the addressbook " + "manually."); + KMessageBox::information( parent, text ); + inserted = true; + } + + ab->setErrorHandler( 0 ); + return inserted; +} + +bool KAddrBookExternal::addAddressee( const KABC::Addressee& addr ) +{ + KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true ); + KABC::Resource *kabcResource = selectResourceForSaving( addressBook ); + if( !kabcResource ) + return false; + KABC::Ticket *ticket = addressBook->requestSaveTicket( kabcResource ); + bool saved = false; + if ( ticket ) { + KABC::Addressee addressee( addr ); + addressee.setResource( kabcResource ); + addressBook->insertAddressee( addressee ); + saved = addressBook->save( ticket ); + if ( !saved ) + addressBook->releaseSaveTicket( ticket ); + } + + addressBook->emitAddressBookChanged(); + + return saved; +} + +TQString KAddrBookExternal::expandDistributionList( const TQString& listName ) +{ + if ( listName.isEmpty() ) + return TQString(); + + const TQString lowerListName = listName.lower(); + KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true ); +#ifdef KDEPIM_NEW_DISTRLISTS + KPIM::DistributionList distrList = KPIM::DistributionList::findByName( addressBook, lowerListName, false ); + if ( !distrList.isEmpty() ) { + return distrList.emails( addressBook ).join( ", " ); + } +#else + KABC::DistributionListManager manager( addressBook ); + manager.load(); + const TQStringList listNames = manager.listNames(); + + for ( TQStringList::ConstIterator it = listNames.begin(); + it != listNames.end(); ++it) { + if ( (*it).lower() == lowerListName ) { + const TQStringList addressList = manager.list( *it )->emails(); + return addressList.join( ", " ); + } + } +#endif + return TQString(); +} + +KABC::Resource* KAddrBookExternal::selectResourceForSaving( KABC::AddressBook *addressBook ) +{ +#if KDE_IS_VERSION(3,4,89) + // This ugly hack will be removed in 4.0 + while ( !addressBook->loadingHasFinished() ) { + TQApplication::eventLoop()->processEvents( TQEventLoop::ExcludeUserInput ); + + // use sleep here to reduce cpu usage + usleep( 100 ); + } +#endif + + // Select a resource + TQPtrList<KABC::Resource> kabcResources = addressBook->resources(); + + TQPtrList<KRES::Resource> kresResources; + TQPtrListIterator<KABC::Resource> resIt( kabcResources ); + KABC::Resource *kabcResource; + while ( ( kabcResource = resIt.current() ) != 0 ) { + ++resIt; + if ( !kabcResource->readOnly() ) { + KRES::Resource *res = static_cast<KRES::Resource*>( kabcResource ); + if ( res ) + kresResources.append( res ); + } + } + + return static_cast<KABC::Resource*>( KRES::SelectDialog::getResource( kresResources, 0 ) ); +} diff --git a/libtdepim/kaddrbook.h b/libtdepim/kaddrbook.h new file mode 100644 index 000000000..f2bab1003 --- /dev/null +++ b/libtdepim/kaddrbook.h @@ -0,0 +1,45 @@ +/* Simple Addressbook for KMail + * Author: Stefan Taferner <taferner@kde.org> + * This code is under GPL + */ +#ifndef KAddrBook_h +#define KAddrBook_h + +#include <tqstringlist.h> + +#include <kdeversion.h> +#include <kabc/addressee.h> +#include <tdepimmacros.h> + +namespace KABC { + class AddressBook; +} + +class TQWidget; + +class KDE_EXPORT KAddrBookExternal { +public: + static void addEmail( const TQString &addr, TQWidget *parent ); + static void addNewAddressee( TQWidget* ); + static void openEmail( const TQString &addr, TQWidget *parent ); + static void openAddressBook( TQWidget *parent ); + + static bool addVCard( const KABC::Addressee& addressee, TQWidget *parent ); + + static TQString expandDistributionList( const TQString& listName ); + + /** + * Pops up a dialog to ask the user to select a resource for saving something, and + * returns the selected resource or 0 on failure or if the user cancelled. + * + * The addressbook used to get the resource list from. If the addressbook was loaded + * async and loading is not yet finished, this method will run an eventloop until the + * addressbook is loaded. + */ + static KABC::Resource* selectResourceForSaving( KABC::AddressBook *addressBook ); + +private: + static bool addAddressee( const KABC::Addressee& addressee ); +}; + +#endif /*KAddrBook_h*/ diff --git a/libtdepim/kcmdesignerfields.cpp b/libtdepim/kcmdesignerfields.cpp new file mode 100644 index 000000000..870d89ce2 --- /dev/null +++ b/libtdepim/kcmdesignerfields.cpp @@ -0,0 +1,430 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@kde.org> + Copyright (c) 2004 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <unistd.h> + +#include <tqimage.h> +#include <tqlabel.h> +#include <tqlayout.h> +#include <tqobjectlist.h> +#include <tqpixmap.h> +#include <tqpushbutton.h> +#include <tqwhatsthis.h> +#include <tqgroupbox.h> +#include <tqwidgetfactory.h> +#include <tqregexp.h> +#include <tqtimer.h> + +#include <kaboutdata.h> +#include <kdebug.h> +#include <kdialog.h> +#include <kglobal.h> +#include <klistview.h> +#include <klocale.h> +#include <krun.h> +#include <kstandarddirs.h> +#include <kactivelabel.h> +#include <kdirwatch.h> +#include <kfiledialog.h> +#include <kmessagebox.h> +#include <kprocess.h> +#include <kio/netaccess.h> + +#include "kcmdesignerfields.h" + +using namespace KPIM; + +class PageItem : public TQCheckListItem +{ + public: + PageItem( TQListView *parent, const TQString &path ) + : TQCheckListItem( parent, "", TQCheckListItem::CheckBox ), + mPath( path ), mIsActive( false ) + { + mName = path.mid( path.findRev( '/' ) + 1 ); + + TQWidget *wdg = TQWidgetFactory::create( mPath, 0, 0 ); + if ( wdg ) { + setText( 0, wdg->caption() ); + + TQPixmap pm = TQPixmap::grabWidget( wdg ); + TQImage img = pm.convertToImage().smoothScale( 300, 300, TQ_ScaleMin ); + mPreview = img; + + TQObjectList *list = wdg->queryList( TQWIDGET_OBJECT_NAME_STRING ); + TQObjectListIt it( *list ); + + TQMap<TQString, TQString> allowedTypes; + allowedTypes.insert( TQLINEEDIT_OBJECT_NAME_STRING, i18n( "Text" ) ); + allowedTypes.insert( TQTEXTEDIT_OBJECT_NAME_STRING, i18n( "Text" ) ); + allowedTypes.insert( TQSPINBOX_OBJECT_NAME_STRING, i18n( "Numeric Value" ) ); + allowedTypes.insert( TQCHECKBOX_OBJECT_NAME_STRING, i18n( "Boolean" ) ); + allowedTypes.insert( TQCOMBOBOX_OBJECT_NAME_STRING, i18n( "Selection" ) ); + allowedTypes.insert( TQDATETIMEEDIT_OBJECT_NAME_STRING, i18n( "Date & Time" ) ); + allowedTypes.insert( "KLineEdit", i18n( "Text" ) ); + allowedTypes.insert( "KDateTimeWidget", i18n( "Date & Time" ) ); + allowedTypes.insert( "KDatePicker", i18n( "Date" ) ); + + while ( it.current() ) { + if ( allowedTypes.find( it.current()->className() ) != allowedTypes.end() ) { + TQString name = it.current()->name(); + if ( name.startsWith( "X_" ) ) { + new TQListViewItem( this, name, + allowedTypes[ it.current()->className() ], + it.current()->className(), + TQWhatsThis::textFor( TQT_TQWIDGET( it.current() ) ) ); + } + } + + ++it; + } + + delete list; + } + } + + TQString name() const { return mName; } + TQString path() const { return mPath; } + + TQPixmap preview() + { + return mPreview; + } + + void setIsActive( bool isActive ) { mIsActive = isActive; } + bool isActive() const { return mIsActive; } + + protected: + void paintBranches( TQPainter *p, const TQColorGroup & cg, int w, int y, int h ) + { + TQListViewItem::paintBranches( p, cg, w, y, h ); + } + + private: + TQString mName; + TQString mPath; + TQPixmap mPreview; + bool mIsActive; +}; + +KCMDesignerFields::KCMDesignerFields( TQWidget *parent, const char *name ) + : KCModule( parent, name ) +{ + TQTimer::singleShot( 0, this, TQT_SLOT( delayedInit() ) ); + + KAboutData *about = new KAboutData( I18N_NOOP( "KCMDesignerfields" ), + I18N_NOOP( "TQt Designer Fields Dialog" ), + 0, 0, KAboutData::License_LGPL, + I18N_NOOP( "(c), 2004 Tobias Koenig" ) ); + + about->addAuthor( "Tobias Koenig", 0, "tokoe@kde.org" ); + about->addAuthor( "Cornelius Schumacher", 0, "schumacher@kde.org" ); + setAboutData( about ); +} + +void KCMDesignerFields::delayedInit() +{ + kdDebug() << "KCMDesignerFields::delayedInit()" << endl; + + initGUI(); + + connect( mPageView, TQT_SIGNAL( selectionChanged( TQListViewItem* ) ), + this, TQT_SLOT( updatePreview( TQListViewItem* ) ) ); + connect( mPageView, TQT_SIGNAL( clicked( TQListViewItem* ) ), + this, TQT_SLOT( itemClicked( TQListViewItem* ) ) ); + + connect( mDeleteButton, TQT_SIGNAL( clicked() ), + this, TQT_SLOT( deleteFile() ) ); + connect( mImportButton, TQT_SIGNAL( clicked() ), + this, TQT_SLOT( importFile() ) ); + connect( mDesignerButton, TQT_SIGNAL( clicked() ), + this, TQT_SLOT( startDesigner() ) ); + + load(); + + // Install a dirwatcher that will detect newly created or removed designer files + KDirWatch *dw = new KDirWatch( TQT_TQOBJECT(this) ); + KStandardDirs::makeDir(localUiDir()); + dw->addDir( localUiDir(), true ); + connect( dw, TQT_SIGNAL( created(const TQString&) ), TQT_SLOT( rebuildList() ) ); + connect( dw, TQT_SIGNAL( deleted(const TQString&) ), TQT_SLOT( rebuildList() ) ); + connect( dw, TQT_SIGNAL( dirty(const TQString&) ), TQT_SLOT( rebuildList() ) ); +} + +void KCMDesignerFields::deleteFile() +{ + TQListViewItem *item = mPageView->selectedItem(); + if ( item ) { + PageItem *pageItem = static_cast<PageItem*>( item->parent() ? item->parent() : item ); + if (KMessageBox::warningContinueCancel(this, + i18n( "<qt>Do you really want to delete '<b>%1</b>'?</qt>").tqarg( pageItem->text(0) ), "", KStdGuiItem::del() ) + == KMessageBox::Continue) + KIO::NetAccess::del( pageItem->path(), 0 ); + } + // The actual view refresh will be done automagically by the slots connected to kdirwatch +} + +void KCMDesignerFields::importFile() +{ + KURL src = KFileDialog::getOpenFileName( TQDir::homeDirPath(), i18n("*.ui|Designer Files"), + this, i18n("Import Page") ); + KURL dest = localUiDir(); + dest.setFileName(src.fileName()); + KIO::NetAccess::file_copy( src, dest, -1, true, false, this ); + // The actual view refresh will be done automagically by the slots connected to kdirwatch +} + + +void KCMDesignerFields::loadUiFiles() +{ + TQStringList list = KGlobal::dirs()->findAllResources( "data", uiPath() + "/*.ui", true, true ); + for ( TQStringList::iterator it = list.begin(); it != list.end(); ++it ) { + new PageItem( mPageView, *it ); + } +} + +void KCMDesignerFields::rebuildList() +{ + TQStringList ai = saveActivePages(); + updatePreview( 0 ); + mPageView->clear(); + loadUiFiles(); + loadActivePages(ai); +} + +void KCMDesignerFields::loadActivePages(const TQStringList& ai) +{ + TQListViewItemIterator it( mPageView ); + while ( it.current() ) { + if ( it.current()->parent() == 0 ) { + PageItem *item = static_cast<PageItem*>( it.current() ); + if ( ai.find( item->name() ) != ai.end() ) { + item->setOn( true ); + item->setIsActive( true ); + } + } + + ++it; + } +} + +void KCMDesignerFields::load() +{ + loadActivePages( readActivePages() ); +} + +TQStringList KCMDesignerFields::saveActivePages() +{ + TQListViewItemIterator it( mPageView, TQListViewItemIterator::Checked | + TQListViewItemIterator::Selectable ); + + TQStringList activePages; + while ( it.current() ) { + if ( it.current()->parent() == 0 ) { + PageItem *item = static_cast<PageItem*>( it.current() ); + activePages.append( item->name() ); + } + + ++it; + } + + return activePages; +} + +void KCMDesignerFields::save() +{ + writeActivePages( saveActivePages() ); +} + +void KCMDesignerFields::defaults() +{ +} + +void KCMDesignerFields::initGUI() +{ + TQVBoxLayout *tqlayout = new TQVBoxLayout( this, KDialog::marginHint(), + KDialog::spacingHint() ); + + bool noDesigner = KStandardDirs::findExe("designer").isEmpty(); + + if ( noDesigner ) + { + TQString txt = + i18n("<qt><b>Warning:</b> TQt Designer could not be found. It is probably not " + "installed. You will only be able to import existing designer files.</qt>"); + TQLabel *lbl = new TQLabel( txt, this ); + tqlayout->addWidget( lbl ); + } + + TQHBoxLayout *hbox = new TQHBoxLayout( tqlayout, KDialog::spacingHint() ); + + mPageView = new KListView( this ); + mPageView->addColumn( i18n( "Available Pages" ) ); + mPageView->setRootIsDecorated( true ); + mPageView->setAllColumnsShowFocus( true ); + mPageView->setFullWidth( true ); + hbox->addWidget( mPageView ); + + TQGroupBox *box = new TQGroupBox(1, Qt::Horizontal, i18n("Preview of Selected Page"), this ); + + mPagePreview = new TQLabel( box ); + mPagePreview->setMinimumWidth( 300 ); + + mPageDetails = new TQLabel( box ); + + hbox->addWidget( box ); + + loadUiFiles(); + + hbox = new TQHBoxLayout( tqlayout, KDialog::spacingHint() ); + + TQString cwHowto = i18n("<qt><p>This section allows you to add your own GUI" + " Elements ('<i>Widgets</i>') to store your own values" + " into %1. Proceed as described below:</p>" + "<ol>" + "<li>Click on '<i>Edit with TQt Designer</i>'" + "<li>In the dialog, select '<i>Widget</i>', then click <i>OK</i>" + "<li>Add your widgets to the form" + "<li>Save the file in the directory proposed by TQt Designer" + "<li>Close TQt Designer" + "</ol>" + "<p>In case you already have a designer file (*.ui) located" + " somewhere on your hard disk, simply choose '<i>Import Page</i>'</p>" + "<p><b>Important:</b> The name of each input widget you place within" + " the form must start with '<i>X_</i>'; so if you want the widget to" + " correspond to your custom entry '<i>X-Foo</i>', set the widget's" + " <i>name</i> property to '<i>X_Foo</i>'.</p>" + "<p><b>Important:</b> The widget will edit custom fields with an" + " application name of %2. To change the application name" + " to be edited, set the widget name in TQt Designer.</p></qt>" ) + .tqarg( applicationName(), applicationName() ); + + KActiveLabel *activeLabel = new KActiveLabel( + i18n( "<a href=\"whatsthis:%1\">How does this work?</a>" ).tqarg(cwHowto), this ); + hbox->addWidget( activeLabel ); + + // ### why is this needed? Looks like a KActiveLabel bug... + activeLabel->tqsetSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Maximum ); + + hbox->addStretch( 1 ); + + mDeleteButton = new TQPushButton( i18n( "Delete Page" ), this); + mDeleteButton->setEnabled( false ); + hbox->addWidget( mDeleteButton ); + mImportButton = new TQPushButton( i18n( "Import Page..." ), this); + hbox->addWidget( mImportButton ); + mDesignerButton = new TQPushButton( i18n( "Edit with TQt Designer..." ), this ); + hbox->addWidget( mDesignerButton ); + + if ( noDesigner ) + mDesignerButton->setEnabled( false ); + + // FIXME: Why do I have to call show() for all widgets? A this->show() doesn't + // seem to work. + mPageView->show(); + box->show(); + activeLabel->show(); + mDeleteButton->show(); + mImportButton->show(); + mDesignerButton->show(); +} + +void KCMDesignerFields::updatePreview( TQListViewItem *item ) +{ + bool widgetItemSelected = false; + + if ( item ) { + if ( item->parent() ) { + TQString details = TQString( "<qt><table>" + "<tr><td align=\"right\"><b>%1</b></td><td>%2</td></tr>" + "<tr><td align=\"right\"><b>%3</b></td><td>%4</td></tr>" + "<tr><td align=\"right\"><b>%5</b></td><td>%6</td></tr>" + "<tr><td align=\"right\"><b>%7</b></td><td>%8</td></tr>" + "</table></qt>" ) + .tqarg( i18n( "Key:" ) ) + .tqarg( item->text( 0 ).replace("X_","X-") ) + .tqarg( i18n( "Type:" ) ) + .tqarg( item->text( 1 ) ) + .tqarg( i18n( "Classname:" ) ) + .tqarg( item->text( 2 ) ) + .tqarg( i18n( "Description:" ) ) + .tqarg( item->text( 3 ) ); + + mPageDetails->setText( details ); + + PageItem *pageItem = static_cast<PageItem*>( item->parent() ); + mPagePreview->setPixmap( pageItem->preview() ); + } else { + mPageDetails->setText( TQString() ); + + PageItem *pageItem = static_cast<PageItem*>( item ); + mPagePreview->setPixmap( pageItem->preview() ); + + widgetItemSelected = true; + } + + mPagePreview->setFrameStyle( TQFrame::Panel | TQFrame::Sunken ); + } else { + mPagePreview->setPixmap( TQPixmap() ); + mPagePreview->setFrameStyle( 0 ); + mPageDetails->setText( TQString() ); + } + + mDeleteButton->setEnabled( widgetItemSelected ); +} + +void KCMDesignerFields::itemClicked( TQListViewItem *item ) +{ + if ( !item || item->parent() != 0 ) + return; + + PageItem *pageItem = static_cast<PageItem*>( item ); + + if ( pageItem->isOn() != pageItem->isActive() ) { + emit changed( true ); + pageItem->setIsActive( pageItem->isOn() ); + } +} + +void KCMDesignerFields::startDesigner() +{ + TQString cmdLine = "designer"; + + // check if path exists and create one if not. + TQString cepPath = localUiDir(); + if( !KGlobal::dirs()->exists(cepPath) ) { + KIO::NetAccess::mkdir( cepPath, this ); + } + + // finally jump there + chdir(cepPath.local8Bit()); + + TQListViewItem *item = mPageView->selectedItem(); + if ( item ) { + PageItem *pageItem = static_cast<PageItem*>( item->parent() ? item->parent() : item ); + cmdLine += " " + KProcess::quote( pageItem->path() ); + } + + KRun::runCommand( cmdLine ); +} + +#include "kcmdesignerfields.moc" diff --git a/libtdepim/kcmdesignerfields.h b/libtdepim/kcmdesignerfields.h new file mode 100644 index 000000000..820506f93 --- /dev/null +++ b/libtdepim/kcmdesignerfields.h @@ -0,0 +1,81 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@kde.org> + Copyright (c) 2004 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef KPIM_KCMDESIGNERFIELDS_H +#define KPIM_KCMDESIGNERFIELDS_H + +#include <kcmodule.h> +#include <tdepimmacros.h> + +class KListView; + +class TQLabel; +class TQListViewItem; +class TQPushButton; + +namespace KPIM { + +class KDE_EXPORT KCMDesignerFields : public KCModule +{ + Q_OBJECT + TQ_OBJECT + + public: + KCMDesignerFields( TQWidget *parent = 0, const char *name = 0 ); + + virtual void load(); + virtual void save(); + virtual void defaults(); + + protected: + void loadUiFiles(); + void loadActivePages(const TQStringList&); + TQStringList saveActivePages(); + + virtual TQString localUiDir() = 0; + virtual TQString uiPath() = 0; + virtual void writeActivePages( const TQStringList & ) = 0; + virtual TQStringList readActivePages() = 0; + virtual TQString applicationName() = 0; + + private slots: + void updatePreview( TQListViewItem* ); + void itemClicked( TQListViewItem* ); + void startDesigner(); + void rebuildList(); + void deleteFile(); + void importFile(); + void delayedInit(); + + private: + void initGUI(); + + KListView *mPageView; + TQLabel *mPagePreview; + TQLabel *mPageDetails; + TQPushButton *mDeleteButton; + TQPushButton *mImportButton; + TQPushButton *mDesignerButton; +}; + +} + +#endif diff --git a/libtdepim/kconfigpropagator.cpp b/libtdepim/kconfigpropagator.cpp new file mode 100644 index 000000000..d22805db9 --- /dev/null +++ b/libtdepim/kconfigpropagator.cpp @@ -0,0 +1,289 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2003 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "kconfigpropagator.h" + +#include <kdebug.h> +#include <kconfig.h> +#include <kconfigskeleton.h> +#include <kstandarddirs.h> +#include <kstringhandler.h> +#include <klocale.h> + +#include <tqfile.h> +#include <tqstringlist.h> + +KConfigPropagator::Change::~Change() +{ +} + +KConfigPropagator::ChangeConfig::ChangeConfig() + : KConfigPropagator::Change( i18n("Change Config Value") ), + hideValue( false ) +{ +} + +TQString KConfigPropagator::ChangeConfig::arg1() const +{ + return file + "/" + group + "/" + name; +} + +TQString KConfigPropagator::ChangeConfig::arg2() const +{ + if ( hideValue ) return "*"; + else return value; +} + +void KConfigPropagator::ChangeConfig::apply() +{ + KConfig cfg( file ); + cfg.setGroup( group ); + cfg.writeEntry( name, value ); + + cfg.sync(); +} + +KConfigPropagator::KConfigPropagator() + : mSkeleton( 0 ) +{ + init(); +} + +KConfigPropagator::KConfigPropagator( KConfigSkeleton *skeleton, + const TQString &kcfgFile ) + : mSkeleton( skeleton ), mKcfgFile( kcfgFile ) +{ + init(); + + readKcfgFile(); +} + +void KConfigPropagator::init() +{ + mChanges.setAutoDelete( true ); +} + +void KConfigPropagator::readKcfgFile() +{ + TQString filename = locate( "kcfg", mKcfgFile ); + if ( filename.isEmpty() ) { + kdError() << "Unable to find kcfg file '" << mKcfgFile << "'" << endl; + return; + } + + TQFile input( filename ); + TQDomDocument doc; + TQString errorMsg; + int errorRow; + int errorCol; + if ( !doc.setContent( &input, &errorMsg, &errorRow, &errorCol ) ) { + kdError() << "Parse error in " << mKcfgFile << ", line " << errorRow << ", col " << errorCol << ": " << errorMsg << endl; + return; + } + + TQDomElement cfgElement = doc.documentElement(); + + if ( cfgElement.isNull() ) { + kdError() << "No document in kcfg file" << endl; + return; + } + + mRules.clear(); + + TQDomNode n; + for ( n = cfgElement.firstChild(); !n.isNull(); n = n.nextSibling() ) { + TQDomElement e = n.toElement(); + + TQString tag = e.tagName(); + + if ( tag == "propagation" ) { + Rule rule = parsePropagation( e ); + mRules.append( rule ); + } else if ( tag == "condition" ) { + Condition condition = parseCondition( e ); + TQDomNode n2; + for( n2 = e.firstChild(); !n2.isNull(); n2 = n2.nextSibling() ) { + TQDomElement e2 = n2.toElement(); + if ( e2.tagName() == "propagation" ) { + Rule rule = parsePropagation( e2 ); + rule.condition = condition; + mRules.append( rule ); + } else { + kdError() << "Unknow tag: " << e2.tagName() << endl; + } + } + } + } +} + +KConfigPropagator::Rule KConfigPropagator::parsePropagation( const TQDomElement &e ) +{ + Rule r; + + TQString source = e.attribute( "source" ); + parseConfigEntryPath( source, r.sourceFile, r.sourceGroup, r.sourceEntry ); + + TQString target = e.attribute( "target" ); + parseConfigEntryPath( target, r.targetFile, r.targetGroup, r.targetEntry ); + + r.hideValue = e.hasAttribute( "hidevalue" ) && + e.attribute( "hidevalue" ) == "true"; + + return r; +} + +void KConfigPropagator::parseConfigEntryPath( const TQString &path, + TQString &file, + TQString &group, + TQString &entry ) +{ + TQStringList p = TQStringList::split( "/", path ); + + if ( p.count() != 3 ) { + kdError() << "Path has to be of form file/group/entry" << endl; + file = TQString(); + group = TQString(); + entry = TQString(); + return; + } + + file = p[ 0 ]; + group = p[ 1 ]; + entry = p[ 2 ]; + + return; +} + +KConfigPropagator::Condition KConfigPropagator::parseCondition( const TQDomElement &e ) +{ + Condition c; + + TQString key = e.attribute( "key" ); + + parseConfigEntryPath( key, c.file, c.group, c.key ); + + c.value = e.attribute( "value" ); + + c.isValid = true; + + return c; +} + +void KConfigPropagator::commit() +{ + updateChanges(); + + Change *c; + for( c = mChanges.first(); c; c = mChanges.next() ) { + c->apply(); + } +} + +KConfigSkeletonItem *KConfigPropagator::findItem( const TQString &group, + const TQString &name ) +{ +// kdDebug() << "KConfigPropagator::findItem()" << endl; + + if ( !mSkeleton ) return 0; + + KConfigSkeletonItem::List items = mSkeleton->items(); + KConfigSkeletonItem::List::ConstIterator it; + for( it = items.begin(); it != items.end(); ++it ) { +// kdDebug() << " Item: " << (*it)->name() << " Type: " +// << (*it)->property().typeName() << endl; + if ( (*it)->group() == group && (*it)->name() == name ) { + break; + } + } + if ( it == items.end() ) return 0; + else return *it; +} + +TQString KConfigPropagator::itemValueAsString( KConfigSkeletonItem *item ) +{ + TQVariant p = item->property(); + + if ( p.type() == TQVariant::Bool ) { + if ( p.toBool() ) return "true"; + else return "false"; + } + + return p.toString(); +} + +void KConfigPropagator::updateChanges() +{ + mChanges.clear(); + + Rule::List::ConstIterator it; + for( it = mRules.begin(); it != mRules.end(); ++it ) { + Rule r = *it; + Condition c = r.condition; + if ( c.isValid ) { + KConfigSkeletonItem *item = findItem( c.group, c.key ); + kdDebug() << "Item " << c.group << "/" << c.key << ":" << endl; + if ( !item ) { + kdError() << " Item not found." << endl; + } else { + TQString value = itemValueAsString( item ); + kdDebug() << " Value: " << value << endl; + if ( value != c.value ) { + continue; + } + } + } + + KConfigSkeletonItem *item = findItem( r.sourceGroup, r.sourceEntry ); + if ( !item ) { + kdError() << "Item " << r.sourceGroup << "/" << r.sourceEntry + << " not found." << endl; + continue; + } + TQString value = itemValueAsString( item ); + + KConfig target( r.targetFile ); + target.setGroup( r.targetGroup ); + TQString targetValue = target.readEntry( r.targetEntry ); + if ( r.hideValue ) targetValue = KStringHandler::obscure( targetValue ); + if ( targetValue != value ) { + ChangeConfig *change = new ChangeConfig(); + change->file = r.targetFile; + change->group = r.targetGroup; + change->name = r.targetEntry; + if ( r.hideValue ) value = KStringHandler::obscure( value ); + change->value = value; + change->hideValue = r.hideValue; + mChanges.append( change ); + } + } + + addCustomChanges( mChanges ); +} + +KConfigPropagator::Change::List KConfigPropagator::changes() +{ + return mChanges; +} + +KConfigPropagator::Rule::List KConfigPropagator::rules() +{ + return mRules; +} diff --git a/libtdepim/kconfigpropagator.h b/libtdepim/kconfigpropagator.h new file mode 100644 index 000000000..3e52eb7c4 --- /dev/null +++ b/libtdepim/kconfigpropagator.h @@ -0,0 +1,165 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2003 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef KCONFIGPROPAGATOR_H +#define KCONFIGPROPAGATOR_H + +#include <tqstring.h> +#include <tqvaluelist.h> +#include <tqdom.h> +#include <tqptrlist.h> + +#include <tdepimmacros.h> + +class KConfigSkeleton; +class KConfigSkeletonItem; + +class KDE_EXPORT KConfigPropagator +{ + public: + + /** + Create KConfigPropagator object without associated source configuration. + */ + KConfigPropagator(); + /** + Create KConfigPropagator object. + + @param skeleton KConfigSkeleton object used as source for the propagation + @param kcfgFile file name of kcfg file containing the propagation rules + */ + KConfigPropagator( KConfigSkeleton *skeleton, const TQString &kcfgFile ); + virtual ~KConfigPropagator() {} + + KConfigSkeleton *skeleton() { return mSkeleton; } + + /* + Commit changes according to propagation rules. + */ + void commit(); + + class KDE_EXPORT Condition + { + public: + Condition() : isValid( false ) {} + + TQString file; + TQString group; + TQString key; + TQString value; + + bool isValid; + }; + + class KDE_EXPORT Rule + { + public: + typedef TQValueList<Rule> List; + + Rule() : hideValue( false ) {} + + TQString sourceFile; + TQString sourceGroup; + TQString sourceEntry; + + TQString targetFile; + TQString targetGroup; + TQString targetEntry; + + Condition condition; + + bool hideValue; + }; + + class KDE_EXPORT Change + { + public: + typedef TQPtrList<Change> List; + + Change( const TQString &title ) : mTitle( title ) {} + virtual ~Change(); + + void setTitle( const TQString &title ) { mTitle = title; } + TQString title() const { return mTitle; } + + virtual TQString arg1() const { return TQString(); } + virtual TQString arg2() const { return TQString(); } + + virtual void apply() = 0; + + private: + TQString mTitle; + }; + + class KDE_EXPORT ChangeConfig : public Change + { + public: + ChangeConfig(); + ~ChangeConfig() {} + + TQString arg1() const; + TQString arg2() const; + + void apply(); + + TQString file; + TQString group; + TQString name; + TQString label; + TQString value; + bool hideValue; + }; + + void updateChanges(); + + Change::List changes(); + + Rule::List rules(); + + protected: + void init(); + + /** + Implement this function in a subclass if you want to add changes which + can't be expressed as propagations in the kcfg file. + */ + virtual void addCustomChanges( Change::List & ) {} + + KConfigSkeletonItem *findItem( const TQString &group, const TQString &name ); + + TQString itemValueAsString( KConfigSkeletonItem * ); + + void readKcfgFile(); + + Rule parsePropagation( const TQDomElement &e ); + Condition parseCondition( const TQDomElement &e ); + + void parseConfigEntryPath( const TQString &path, TQString &file, + TQString &group, TQString &entry ); + + private: + KConfigSkeleton *mSkeleton; + TQString mKcfgFile; + + Rule::List mRules; + Change::List mChanges; +}; + +#endif diff --git a/libtdepim/kconfigwizard.cpp b/libtdepim/kconfigwizard.cpp new file mode 100644 index 000000000..bceacd656 --- /dev/null +++ b/libtdepim/kconfigwizard.cpp @@ -0,0 +1,197 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2003 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "kconfigwizard.h" + +#include <klocale.h> +#include <kdebug.h> +#include <kconfigskeleton.h> +#include <kmessagebox.h> +#include <kapplication.h> + +#include <tqlistview.h> +#include <tqlayout.h> +#include <tqtimer.h> + +KConfigWizard::KConfigWizard( TQWidget *parent, + char *name, bool modal ) + : KDialogBase( TreeList, i18n("Configuration Wizard"), Ok|Cancel, Ok, parent, + name, modal ), + mPropagator( 0 ), mChangesPage( 0 ) +{ + init(); +} + +KConfigWizard::KConfigWizard( KConfigPropagator *propagator, TQWidget *parent, + char *name, bool modal ) + : KDialogBase( TreeList, i18n("Configuration Wizard"), Ok|Cancel, Ok, parent, + name, modal ), + mPropagator( propagator ), mChangesPage( 0 ) +{ + init(); +} + +KConfigWizard::~KConfigWizard() +{ + delete mPropagator; +} + +void KConfigWizard::init() +{ + connect( this, TQT_SIGNAL( aboutToShowPage( TQWidget * ) ), + TQT_SLOT( slotAboutToShowPage( TQWidget * ) ) ); + + TQTimer::singleShot( 0, this, TQT_SLOT( readConfig() ) ); +} + +void KConfigWizard::setPropagator( KConfigPropagator *p ) +{ + mPropagator = p; +} + +void KConfigWizard::slotAboutToShowPage( TQWidget *page ) +{ + if ( page == mChangesPage ) { + updateChanges(); + } +} + +TQFrame *KConfigWizard::createWizardPage( const TQString &title ) +{ + return addPage( title ); +} + +void KConfigWizard::setupRulesPage() +{ + TQFrame *topFrame = addPage( i18n("Rules") ); + TQVBoxLayout *topLayout = new TQVBoxLayout( topFrame ); + + mRuleView = new TQListView( topFrame ); + topLayout->addWidget( mRuleView ); + + mRuleView->addColumn( i18n("Source") ); + mRuleView->addColumn( i18n("Target") ); + mRuleView->addColumn( i18n("Condition") ); + + updateRules(); +} + +void KConfigWizard::updateRules() +{ + if ( !mPropagator ) { + kdError() << "KConfigWizard: No KConfigPropagator set." << endl; + return; + } + + mRuleView->clear(); + + KConfigPropagator::Rule::List rules = mPropagator->rules(); + KConfigPropagator::Rule::List::ConstIterator it; + for( it = rules.begin(); it != rules.end(); ++it ) { + KConfigPropagator::Rule r = *it; + TQString source = r.sourceFile + "/" + r.sourceGroup + "/" + + r.sourceEntry; + TQString target = r.targetFile + "/" + r.targetGroup + "/" + + r.targetEntry; + TQString condition; + KConfigPropagator::Condition c = r.condition; + if ( c.isValid ) { + condition = c.file + "/" + c.group + "/" + c.key + " = " + c.value; + } + new TQListViewItem( mRuleView, source, target, condition ); + } +} + +void KConfigWizard::setupChangesPage() +{ + TQFrame *topFrame = addPage( i18n("Changes") ); + TQVBoxLayout *topLayout = new TQVBoxLayout( topFrame ); + + mChangeView = new TQListView( topFrame ); + topLayout->addWidget( mChangeView ); + + mChangeView->addColumn( i18n("Action") ); + mChangeView->addColumn( i18n("Option") ); + mChangeView->addColumn( i18n("Value") ); + mChangeView->setSorting( -1 ); + + mChangesPage = topFrame; +} + +void KConfigWizard::updateChanges() +{ + kdDebug() << "KConfigWizard::updateChanges()" << endl; + + if ( !mPropagator ) { + kdError() << "KConfigWizard: No KConfigPropagator set." << endl; + return; + } + + usrWriteConfig(); + + mPropagator->updateChanges(); + + mChangeView->clear(); + + KConfigPropagator::Change::List changes = mPropagator->changes(); + KConfigPropagator::Change *c; + for( c = changes.first(); c; c = changes.next() ) { + new TQListViewItem( mChangeView, mChangeView->lastItem(), c->title(), c->arg1(), c->arg2() ); + } +} + +void KConfigWizard::readConfig() +{ + kdDebug() << "KConfigWizard::readConfig()" << endl; + + int result = KMessageBox::warningContinueCancel( this, + i18n("Please make sure that the programs which are " + "configured by the wizard do not run in parallel to the wizard; " + "otherwise, changes done by the wizard could be lost."), + i18n("Warning"), i18n("Run Wizard Now"), "warning_running_instances" ); + if ( result != KMessageBox::Continue ) kapp->quit(); + + usrReadConfig(); +} + +void KConfigWizard::slotOk() +{ + TQString error = validate(); + if ( error.isNull() ) { + usrWriteConfig(); + + if ( !mPropagator ) { + kdError() << "KConfigWizard: No KConfigPropagator set." << endl; + return; + } else { + if ( mPropagator->skeleton() ) { + mPropagator->skeleton()->writeConfig(); + } + mPropagator->commit(); + } + + accept(); + } else { + KMessageBox::sorry( this, error ); + } +} + +#include "kconfigwizard.moc" diff --git a/libtdepim/kconfigwizard.h b/libtdepim/kconfigwizard.h new file mode 100644 index 000000000..71455277c --- /dev/null +++ b/libtdepim/kconfigwizard.h @@ -0,0 +1,110 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2003 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef KCONFIGWIZARD_H +#define KCONFIGWIZARD_H + +#include <kconfigpropagator.h> +#include <tdepimmacros.h> +#include <kdialogbase.h> + +class TQListView; + +/** + @short Configuration wizard base class +*/ +class KDE_EXPORT KConfigWizard : public KDialogBase +{ + Q_OBJECT + TQ_OBJECT + public: + /** + Create wizard. You have to set a propgator with setPropagator() later. + */ + KConfigWizard( TQWidget *parent = 0, char *name = 0, bool modal = false ); + /** + Create wizard for given KConfigPropagator. The wizard takes ownership of + the propagator. + */ + KConfigWizard( KConfigPropagator *propagator, TQWidget *parent = 0, + char *name = 0, bool modal = false ); + /** + Destructor. + */ + virtual ~KConfigWizard(); + + /** + Set propagator the wizard operates on. + */ + void setPropagator( KConfigPropagator * ); + /** + Return propagator the wizard operates on. + */ + KConfigPropagator *propagator() { return mPropagator; } + + /** + Create wizard page with given title. + */ + TQFrame *createWizardPage( const TQString &title ); + + /** + Use this function to read the configuration from the KConfigSkeleton + object to the GUI. + */ + virtual void usrReadConfig() = 0; + + /** + This function is called when the wizard is finished. You have to save all + settings from the GUI to the KConfigSkeleton object here, so that the + KConfigPropagator can take them up from there. + */ + virtual void usrWriteConfig() = 0; + + /** + Validates the supplied data. Returns a appropiate error when some data + is invalid. Return TQString() if all data is valid. + */ + virtual TQString validate() { return TQString(); } + + protected slots: + void readConfig(); + + void slotOk(); + + void slotAboutToShowPage( TQWidget *page ); + + protected: + void init(); + + void setupRulesPage(); + void updateRules(); + void setupChangesPage(); + void updateChanges(); + + private: + KConfigPropagator *mPropagator; + + TQListView *mRuleView; + TQListView *mChangeView; + + TQWidget *mChangesPage; +}; + +#endif diff --git a/libtdepim/kdateedit.cpp b/libtdepim/kdateedit.cpp new file mode 100644 index 000000000..44c4e696a --- /dev/null +++ b/libtdepim/kdateedit.cpp @@ -0,0 +1,364 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org> + Copyright (c) 2002 David Jarvie <software@astrojar.org.uk> + Copyright (c) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com> + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <tqapplication.h> +#include <tqlineedit.h> +#include <tqlistbox.h> +#include <tqvalidator.h> + +#include <kcalendarsystem.h> +#include <kglobal.h> +#include <kglobalsettings.h> +#include <klocale.h> + +#include "kdateedit.h" + +class DateValidator : public TQValidator +{ + public: + DateValidator( const TQStringList &keywords, TQWidget* parent, const char* name = 0 ) + : TQValidator( TQT_TQOBJECT(parent), name ), mKeywords( keywords ) + {} + + virtual State validate( TQString &str, int& ) const + { + int length = str.length(); + + // empty string is intermediate so one can clear the edit line and start from scratch + if ( length <= 0 ) + return Intermediate; + + if ( mKeywords.contains( str.lower() ) ) + return Acceptable; + + bool ok = false; + KGlobal::locale()->readDate( str, &ok ); + if ( ok ) + return Acceptable; + else + return Intermediate; + } + + private: + TQStringList mKeywords; +}; + +KDateEdit::KDateEdit( TQWidget *parent, const char *name ) + : TQComboBox( true, parent, name ), + mReadOnly( false ), + mDiscardNextMousePress( false ) +{ + // need at least one entry for popup to work + setMaxCount( 1 ); + + mDate = TQDate::tqcurrentDate(); + TQString today = KGlobal::locale()->formatDate( mDate, true ); + + insertItem( today ); + setCurrentItem( 0 ); + changeItem( today, 0 ); + setMinimumSize( tqsizeHint() ); + + connect( lineEdit(), TQT_SIGNAL( returnPressed() ), + this, TQT_SLOT( lineEnterPressed() ) ); + connect( this, TQT_SIGNAL( textChanged( const TQString& ) ), + TQT_SLOT( slotTextChanged( const TQString& ) ) ); + + mPopup = new KDatePickerPopup( KDatePickerPopup::DatePicker | KDatePickerPopup::Words ); + mPopup->hide(); + mPopup->installEventFilter( this ); + + connect( mPopup, TQT_SIGNAL( dateChanged( TQDate ) ), + TQT_SLOT( dateSelected( TQDate ) ) ); + + // handle keyword entry + setupKeywords(); + lineEdit()->installEventFilter( this ); + + setValidator( new DateValidator( mKeywordMap.keys(), this ) ); + + mTextChanged = false; +} + +KDateEdit::~KDateEdit() +{ + delete mPopup; + mPopup = 0; +} + +void KDateEdit::setDate( const TQDate& date ) +{ + assignDate( date ); + updateView(); +} + +TQDate KDateEdit::date() const +{ + return mDate; +} + +void KDateEdit::setReadOnly( bool readOnly ) +{ + mReadOnly = readOnly; + lineEdit()->setReadOnly( readOnly ); +} + +bool KDateEdit::isReadOnly() const +{ + return mReadOnly; +} + +void KDateEdit::popup() +{ + if ( mReadOnly ) + return; + + TQRect desk = KGlobalSettings::desktopGeometry( this ); + + TQPoint popupPoint = mapToGlobal( TQPoint( 0,0 ) ); + + int dateFrameHeight = mPopup->tqsizeHint().height(); + if ( popupPoint.y() + height() + dateFrameHeight > desk.bottom() ) + popupPoint.setY( popupPoint.y() - dateFrameHeight ); + else + popupPoint.setY( popupPoint.y() + height() ); + + int dateFrameWidth = mPopup->tqsizeHint().width(); + if ( popupPoint.x() + dateFrameWidth > desk.right() ) + popupPoint.setX( desk.right() - dateFrameWidth ); + + if ( popupPoint.x() < desk.left() ) + popupPoint.setX( desk.left() ); + + if ( popupPoint.y() < desk.top() ) + popupPoint.setY( desk.top() ); + + if ( mDate.isValid() ) + mPopup->setDate( mDate ); + else + mPopup->setDate( TQDate::tqcurrentDate() ); + + mPopup->popup( popupPoint ); + + // The combo box is now shown pressed. Make it show not pressed again + // by causing its (invisible) list box to emit a 'selected' signal. + // First, ensure that the list box contains the date currently displayed. + TQDate date = parseDate(); + assignDate( date ); + updateView(); + // Now, simulate an Enter to unpress it + TQListBox *lb = listBox(); + if (lb) { + lb->setCurrentItem(0); + TQKeyEvent* keyEvent = new TQKeyEvent(TQEvent::KeyPress, TQt::Key_Enter, 0, 0); + TQApplication::postEvent(lb, keyEvent); + } +} + +void KDateEdit::dateSelected( TQDate date ) +{ + if (assignDate( date ) ) { + updateView(); + emit dateChanged( date ); + emit dateEntered( date ); + + if ( date.isValid() ) { + mPopup->hide(); + } + } +} + +void KDateEdit::lineEnterPressed() +{ + bool replaced = false; + + TQDate date = parseDate( &replaced ); + + if (assignDate( date ) ) { + if ( replaced ) + updateView(); + + emit dateChanged( date ); + emit dateEntered( date ); + } +} + +TQDate KDateEdit::parseDate( bool *replaced ) const +{ + TQString text = currentText(); + TQDate result; + + if ( replaced ) + (*replaced) = false; + + if ( text.isEmpty() ) + result = TQDate(); + else if ( mKeywordMap.contains( text.lower() ) ) { + TQDate today = TQDate::tqcurrentDate(); + int i = mKeywordMap[ text.lower() ]; + if ( i >= 100 ) { + /* A day name has been entered. Convert to offset from today. + * This uses some math tricks to figure out the offset in days + * to the next date the given day of the week occurs. There + * are two cases, that the new day is >= the current day, which means + * the new day has not occurred yet or that the new day < the current day, + * which means the new day is already passed (so we need to find the + * day in the next week). + */ + i -= 100; + int currentDay = today.dayOfWeek(); + if ( i >= currentDay ) + i -= currentDay; + else + i += 7 - currentDay; + } + + result = today.addDays( i ); + if ( replaced ) + (*replaced) = true; + } else { + result = KGlobal::locale()->readDate( text ); + } + + return result; +} + +bool KDateEdit::eventFilter( TQObject *object, TQEvent *event ) +{ + if ( TQT_BASE_OBJECT(object) == TQT_BASE_OBJECT(lineEdit()) ) { + // We only process the focus out event if the text has changed + // since we got focus + if ( (event->type() == TQEvent::FocusOut) && mTextChanged ) { + lineEnterPressed(); + mTextChanged = false; + } else if ( event->type() == TQEvent::KeyPress ) { + // Up and down arrow keys step the date + TQKeyEvent* keyEvent = (TQKeyEvent*)event; + + if ( keyEvent->key() == TQt::Key_Return ) { + lineEnterPressed(); + return true; + } + + int step = 0; + if ( keyEvent->key() == TQt::Key_Up ) + step = 1; + else if ( keyEvent->key() == TQt::Key_Down ) + step = -1; + // TODO: If it's not an input key, but something like Return, Enter, Tab, etc..., don't eat the keypress, but handle it through to the default eventfilter! + if ( step && !mReadOnly ) { + TQDate date = parseDate(); + if ( date.isValid() ) { + date = date.addDays( step ); + if ( assignDate( date ) ) { + updateView(); + emit dateChanged( date ); + emit dateEntered( date ); + return true; + } + } + } + } + } else { + // It's a date picker event + switch ( event->type() ) { + case TQEvent::MouseButtonDblClick: + case TQEvent::MouseButtonPress: { + TQMouseEvent *mouseEvent = (TQMouseEvent*)event; + if ( !TQT_TQRECT_OBJECT(mPopup->rect()).contains( mouseEvent->pos() ) ) { + TQPoint globalPos = mPopup->mapToGlobal( mouseEvent->pos() ); + if ( TQApplication::widgetAt( globalPos, true ) == this ) { + // The date picker is being closed by a click on the + // KDateEdit widget. Avoid popping it up again immediately. + mDiscardNextMousePress = true; + } + } + + break; + } + default: + break; + } + } + + return false; +} + +void KDateEdit::mousePressEvent( TQMouseEvent *event ) +{ + if ( event->button() == Qt::LeftButton && mDiscardNextMousePress ) { + mDiscardNextMousePress = false; + return; + } + + TQComboBox::mousePressEvent( event ); +} + +void KDateEdit::slotTextChanged( const TQString& ) +{ + TQDate date = parseDate(); + + if ( assignDate( date ) ) + emit dateChanged( date ); + + mTextChanged = true; +} + +void KDateEdit::setupKeywords() +{ + // Create the keyword list. This will be used to match against when the user + // enters information. + mKeywordMap.insert( i18n( "tomorrow" ), 1 ); + mKeywordMap.insert( i18n( "today" ), 0 ); + mKeywordMap.insert( i18n( "yesterday" ), -1 ); + + TQString dayName; + for ( int i = 1; i <= 7; ++i ) { + dayName = KGlobal::locale()->calendar()->weekDayName( i ).lower(); + mKeywordMap.insert( dayName, i + 100 ); + } +} + +bool KDateEdit::assignDate( const TQDate& date ) +{ + mDate = date; + mTextChanged = false; + return true; +} + +void KDateEdit::updateView() +{ + TQString dateString; + if ( mDate.isValid() ) + dateString = KGlobal::locale()->formatDate( mDate, true ); + + // We do not want to generate a signal here, + // since we explicitly setting the date + bool blocked = signalsBlocked(); + blockSignals( true ); + changeItem( dateString, 0 ); + blockSignals( blocked ); +} + +#include "kdateedit.moc" diff --git a/libtdepim/kdateedit.h b/libtdepim/kdateedit.h new file mode 100644 index 000000000..02a7701cc --- /dev/null +++ b/libtdepim/kdateedit.h @@ -0,0 +1,150 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org> + Copyright (c) 2002 David Jarvie <software@astrojar.org.uk> + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef KDATEEDIT_H +#define KDATEEDIT_H + +#include <tqcombobox.h> +#include <tqdatetime.h> +#include <tqmap.h> + +#include <tdepimmacros.h> + +#include "kdatepickerpopup.h" + +class TQEvent; + +/** + A date editing widget that consists of an editable combo box. + The combo box contains the date in text form, and clicking the combo + box arrow will display a 'popup' style date picker. + + This widget also supports advanced features like allowing the user + to type in the day name to get the date. The following keywords + are supported (in the native language): tomorrow, yesturday, today, + monday, tuesday, wednesday, thursday, friday, saturday, sunday. + + @image html kdateedit.png "This is how it looks" + + @author Cornelius Schumacher <schumacher@kde.org> + @author Mike Pilone <mpilone@slac.com> + @author David Jarvie <software@astrojar.org.uk> + @author Tobias Koenig <tokoe@kde.org> +*/ +class KDE_EXPORT KDateEdit : public TQComboBox +{ + Q_OBJECT + TQ_OBJECT + + public: + KDateEdit( TQWidget *parent = 0, const char *name = 0 ); + virtual ~KDateEdit(); + + /** + @return The date entered. This date could be invalid, + you have to check validity yourself. + */ + TQDate date() const; + + /** + Sets whether the widget is read-only for the user. If read-only, + the date picker pop-up is inactive, and the displayed date cannot be edited. + + @param readOnly True to set the widget read-only, false to set it read-write. + */ + void setReadOnly( bool readOnly ); + + /** + @return True if the widget is read-only, false if read-write. + */ + bool isReadOnly() const; + + virtual void popup(); + + signals: + /** + This signal is emitted whenever the user has entered a new date. + When the user changes the date by editing the line edit field, + the signal is not emitted until focus leaves the line edit field. + The passed date can be invalid. + */ + void dateEntered( const TQDate &date ); + + /** + This signal is emitted whenever the user modifies the date. + The passed date can be invalid. + */ + void dateChanged( const TQDate &date ); + + public slots: + /** + Sets the date. + + @param date The new date to display. This date must be valid or + it will not be set + */ + void setDate( const TQDate &date ); + + protected slots: + void lineEnterPressed(); + void slotTextChanged( const TQString& ); + void dateSelected( TQDate ); + + protected: + virtual bool eventFilter( TQObject*, TQEvent* ); + virtual void mousePressEvent( TQMouseEvent* ); + + /** + Sets the date, without altering the display. + This method is used internally to set the widget's date value. + As a virtual method, it allows derived classes to perform additional validation + on the date value before it is set. Derived classes should return true if + TQDate::isValid(@p date) returns false. + + @param date The new date to set. + @return True if the date was set, false if it was considered invalid and + remains unchanged. + */ + virtual bool assignDate( const TQDate &date ); + + /** + Fills the keyword map. Reimplement it if you want additional + keywords. + */ + void setupKeywords(); + + private: + TQDate parseDate( bool* = 0 ) const; + void updateView(); + + KDatePickerPopup *mPopup; + + TQDate mDate; + bool mReadOnly; + bool mTextChanged; + bool mDiscardNextMousePress; + + TQMap<TQString, int> mKeywordMap; +}; + +#endif diff --git a/libtdepim/kdatepickerpopup.cpp b/libtdepim/kdatepickerpopup.cpp new file mode 100644 index 000000000..998c44e4a --- /dev/null +++ b/libtdepim/kdatepickerpopup.cpp @@ -0,0 +1,123 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Bram Schoenmakers <bramschoenmakers@kde.nl> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <tqdatetime.h> +#include <tqpopupmenu.h> + +#include <klocale.h> + +#include "kdatepickerpopup.h" + +KDatePickerPopup::KDatePickerPopup( int items, const TQDate &date, TQWidget *parent, + const char *name ) + : TQPopupMenu( parent, name ) +{ + mItems = items; + + mDatePicker = new KDatePicker( this ); + mDatePicker->setCloseButton( false ); + + connect( mDatePicker, TQT_SIGNAL( dateEntered( TQDate ) ), + TQT_SLOT( slotDateChanged( TQDate ) ) ); + connect( mDatePicker, TQT_SIGNAL( dateSelected( TQDate ) ), + TQT_SLOT( slotDateChanged( TQDate ) ) ); + + mDatePicker->setDate( date ); + + buildMenu(); +} + +void KDatePickerPopup::buildMenu() +{ + if ( isVisible() ) return; + clear(); + + if ( mItems & DatePicker ) { + insertItem( mDatePicker ); + + if ( ( mItems & NoDate ) || ( mItems & Words ) ) + insertSeparator(); + } + + if ( mItems & Words ) { + insertItem( i18n("&Today"), this, TQT_SLOT( slotToday() ) ); + insertItem( i18n("To&morrow"), this, TQT_SLOT( slotTomorrow() ) ); + insertItem( i18n("Next &Week"), this, TQT_SLOT( slotNextWeek() ) ); + insertItem( i18n("Next M&onth"), this, TQT_SLOT( slotNextMonth() ) ); + + if ( mItems & NoDate ) + insertSeparator(); + } + + if ( mItems & NoDate ) + insertItem( i18n("No Date"), this, TQT_SLOT( slotNoDate() ) ); +} + +KDatePicker *KDatePickerPopup::datePicker() const +{ + return mDatePicker; +} + +void KDatePickerPopup::setDate( const TQDate &date ) +{ + mDatePicker->setDate( date ); +} + +#if 0 +void KDatePickerPopup::setItems( int items ) +{ + mItems = items; + buildMenu(); +} +#endif + +void KDatePickerPopup::slotDateChanged( TQDate date ) +{ + emit dateChanged( date ); + hide(); +} + +void KDatePickerPopup::slotToday() +{ + emit dateChanged( TQDate::tqcurrentDate() ); +} + +void KDatePickerPopup::slotTomorrow() +{ + emit dateChanged( TQDate::tqcurrentDate().addDays( 1 ) ); +} + +void KDatePickerPopup::slotNoDate() +{ + emit dateChanged( TQDate() ); +} + +void KDatePickerPopup::slotNextWeek() +{ + emit dateChanged( TQDate::tqcurrentDate().addDays( 7 ) ); +} + +void KDatePickerPopup::slotNextMonth() +{ + emit dateChanged( TQDate::tqcurrentDate().addMonths( 1 ) ); +} + +#include "kdatepickerpopup.moc" diff --git a/libtdepim/kdatepickerpopup.h b/libtdepim/kdatepickerpopup.h new file mode 100644 index 000000000..44dc7c668 --- /dev/null +++ b/libtdepim/kdatepickerpopup.h @@ -0,0 +1,104 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Bram Schoenmakers <bramschoenmakers@kde.nl> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef KDATEPICKERPOPUP_H +#define KDATEPICKERPOPUP_H + +#include <tqdatetime.h> +#include <tqpopupmenu.h> + +#include <tdepimmacros.h> +#include <kdatepicker.h> + +/** + @short This menu helps the user to select a date quickly. + + This menu helps the user to select a date quicly. It offers various ways of selecting, e.g. with a KDatePicker or with words like "Tomorrow". + + The available items are: + + @li NoDate: A menu-item with "No Date". If choosen, the datepicker will emit a null TQDate. + @li DatePicker: Show a KDatePicker-widget. + @li Words: Show items like "Today", "Tomorrow" or "Next Week". + + When supplying multiple items, separate each item with a bitwise OR. + + @author Bram Schoenmakers <bram_s@softhome.net> +*/ +class KDE_EXPORT KDatePickerPopup: public TQPopupMenu +{ + Q_OBJECT + TQ_OBJECT + public: + enum { NoDate = 1, DatePicker = 2, Words = 4 }; + + /** + A constructor for the KDatePickerPopup. + + @param items List of all desirable items, separated with a bitwise OR. + @param date Initial date of datepicker-widget. + @param parent The object's parent. + @param name The object's name. + */ + KDatePickerPopup( int items = DatePicker, const TQDate &date = TQDate::tqcurrentDate(), + TQWidget *parent = 0, const char *name = 0 ); + + /** + @return A pointer to the private variable mDatePicker, an instance of + KDatePicker. + */ + KDatePicker *datePicker() const; + + void setDate( const TQDate &date ); + +#if 0 + /** Set items which should be shown and rebuilds the menu afterwards. Only if the menu is not visible. + @param items List of all desirable items, separated with a bitwise OR. + */ + void setItems( int items = 1 ); +#endif + /** @return Returns the bitwise result of the active items in the popup. */ + int items() const { return mItems; } + + signals: + + /** + This signal emits the new date (selected with datepicker or other + menu-items). + */ + void dateChanged ( TQDate ); + + protected slots: + void slotDateChanged ( TQDate ); + + void slotToday(); + void slotTomorrow(); + void slotNextWeek(); + void slotNextMonth(); + void slotNoDate(); + + private: + void buildMenu(); + + KDatePicker *mDatePicker; + int mItems; +}; + +#endif diff --git a/libtdepim/kfileio.cpp b/libtdepim/kfileio.cpp new file mode 100644 index 000000000..e167662bb --- /dev/null +++ b/libtdepim/kfileio.cpp @@ -0,0 +1,390 @@ +// kfileio.cpp +// Author: Stefan Taferner <taferner@kde.org> +// License: GPL + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <kmessagebox.h> +#include <kdebug.h> + +#include <assert.h> +#include <tqdir.h> + +#include <klocale.h> +#include <kstdguiitem.h> + +#include <tqwidget.h> +#include <tqfile.h> +#include <tqfileinfo.h> +#include <sys/stat.h> +#include <sys/types.h> + +#include <tdepimmacros.h> + +namespace KPIM { + +//----------------------------------------------------------------------------- +static void msgDialog(const TQString &msg) +{ + KMessageBox::sorry(0, msg, i18n("File I/O Error")); +} + + +//----------------------------------------------------------------------------- +KDE_EXPORT TQCString kFileToString(const TQString &aFileName, bool aEnsureNL, bool aVerbose) +{ + TQCString result; + TQFileInfo info(aFileName); + unsigned int readLen; + unsigned int len = info.size(); + TQFile file(aFileName); + + //assert(aFileName!=0); + if( aFileName.isEmpty() ) + return ""; + + if (!info.exists()) + { + if (aVerbose) + msgDialog(i18n("The specified file does not exist:\n%1").tqarg(aFileName)); + return TQCString(); + } + if (info.isDir()) + { + if (aVerbose) + msgDialog(i18n("This is a folder and not a file:\n%1").tqarg(aFileName)); + return TQCString(); + } + if (!info.isReadable()) + { + if (aVerbose) + msgDialog(i18n("You do not have read permissions " + "to the file:\n%1").tqarg(aFileName)); + return TQCString(); + } + if (len <= 0) return TQCString(); + + if (!file.open(IO_Raw|IO_ReadOnly)) + { + if (aVerbose) switch(file.status()) + { + case IO_ReadError: + msgDialog(i18n("Could not read file:\n%1").tqarg(aFileName)); + break; + case IO_OpenError: + msgDialog(i18n("Could not open file:\n%1").tqarg(aFileName)); + break; + default: + msgDialog(i18n("Error while reading file:\n%1").tqarg(aFileName)); + } + return TQCString(); + } + + result.resize(len + (int)aEnsureNL + 1); + readLen = file.readBlock(result.data(), len); + if (aEnsureNL && result[len-1]!='\n') + { + result[len++] = '\n'; + readLen++; + } + result[len] = '\0'; + + if (readLen < len) + { + TQString msg = i18n("Could only read %1 bytes of %2.") + .tqarg(readLen).tqarg(len); + msgDialog(msg); + return TQCString(); + } + + return result; +} + +//----------------------------------------------------------------------------- +#if 0 // unused +TQByteArray kFileToBytes(const TQString &aFileName, bool aVerbose) +{ + TQByteArray result; + TQFileInfo info(aFileName); + unsigned int readLen; + unsigned int len = info.size(); + TQFile file(aFileName); + + //assert(aFileName!=0); + if( aFileName.isEmpty() ) + return result; + + if (!info.exists()) + { + if (aVerbose) + msgDialog(i18n("The specified file does not exist:\n%1") + .tqarg(aFileName)); + return result; + } + if (info.isDir()) + { + if (aVerbose) + msgDialog(i18n("This is a folder and not a file:\n%1") + .tqarg(aFileName)); + return result; + } + if (!info.isReadable()) + { + if (aVerbose) + msgDialog(i18n("You do not have read permissions " + "to the file:\n%1").tqarg(aFileName)); + return result; + } + if (len <= 0) return result; + + if (!file.open(IO_Raw|IO_ReadOnly)) + { + if (aVerbose) switch(file.status()) + { + case IO_ReadError: + msgDialog(i18n("Could not read file:\n%1").tqarg(aFileName)); + break; + case IO_OpenError: + msgDialog(i18n("Could not open file:\n%1").tqarg(aFileName)); + break; + default: + msgDialog(i18n("Error while reading file:\n%1").tqarg(aFileName)); + } + return result; + } + + result.resize(len); + readLen = file.readBlock(result.data(), len); + kdDebug(5300) << TQString( "len %1" ).tqarg(len) << endl; + + if (readLen < len) + { + TQString msg; + msg = i18n("Could only read %1 bytes of %2.") + .tqarg(readLen).tqarg(len); + msgDialog(msg); + return result; + } + + return result; +} +#endif + +//----------------------------------------------------------------------------- +KDE_EXPORT bool kBytesToFile(const char* aBuffer, int len, + const TQString &aFileName, + bool aAskIfExists, bool aBackup, bool aVerbose) +{ + // TODO: use KSaveFile + TQFile file(aFileName); + int writeLen, rc; + + //assert(aFileName!=0); + if(aFileName.isEmpty()) + return FALSE; + + if (file.exists()) + { + if (aAskIfExists) + { + TQString str; + str = i18n("File %1 exists.\nDo you want to replace it?") + .tqarg(aFileName); + rc = KMessageBox::warningContinueCancel(0, + str, i18n("Save to File"), i18n("&Replace")); + if (rc != KMessageBox::Continue) return FALSE; + } + if (aBackup) + { + // make a backup copy + // TODO: use KSaveFile::backupFile() + TQString bakName = aFileName; + bakName += '~'; + TQFile::remove(bakName); + if( !TQDir::current().rename(aFileName, bakName) ) + { + // failed to rename file + if (!aVerbose) return FALSE; + rc = KMessageBox::warningContinueCancel(0, + i18n("Failed to make a backup copy of %1.\nContinue anyway?") + .tqarg(aFileName), + i18n("Save to File"), KStdGuiItem::save() ); + if (rc != KMessageBox::Continue) return FALSE; + } + } + } + + if (!file.open(IO_Raw|IO_WriteOnly|IO_Truncate)) + { + if (aVerbose) switch(file.status()) + { + case IO_WriteError: + msgDialog(i18n("Could not write to file:\n%1").tqarg(aFileName)); + break; + case IO_OpenError: + msgDialog(i18n("Could not open file for writing:\n%1") + .tqarg(aFileName)); + break; + default: + msgDialog(i18n("Error while writing file:\n%1").tqarg(aFileName)); + } + return FALSE; + } + + writeLen = file.writeBlock(aBuffer, len); + + if (writeLen < 0) + { + if (aVerbose) + msgDialog(i18n("Could not write to file:\n%1").tqarg(aFileName)); + return FALSE; + } + else if (writeLen < len) + { + TQString msg = i18n("Could only write %1 bytes of %2.") + .tqarg(writeLen).tqarg(len); + if (aVerbose) + msgDialog(msg); + return FALSE; + } + + return TRUE; +} + +KDE_EXPORT bool kCStringToFile(const TQCString& aBuffer, const TQString &aFileName, + bool aAskIfExists, bool aBackup, bool aVerbose) +{ + return kBytesToFile(aBuffer, aBuffer.length(), aFileName, aAskIfExists, + aBackup, aVerbose); +} + +KDE_EXPORT bool kByteArrayToFile(const TQByteArray& aBuffer, const TQString &aFileName, + bool aAskIfExists, bool aBackup, bool aVerbose) +{ + return kBytesToFile(aBuffer, aBuffer.size(), aFileName, aAskIfExists, + aBackup, aVerbose); +} + + +TQString checkAndCorrectPermissionsIfPossible( const TQString &toCheck, + const bool recursive, const bool wantItReadable, + const bool wantItWritable ) +{ + // First we have to find out which type the toCheck is. This can be + // a directory (follow if recursive) or a file (check permissions). + // Symlinks are followed as expected. + TQFileInfo fiToCheck(toCheck); + fiToCheck.setCaching(false); + TQCString toCheckEnc = TQFile::encodeName(toCheck); + TQString error; + struct stat statbuffer; + + if ( !fiToCheck.exists() ) { + error.append( i18n("%1 does not exist") + .tqarg(toCheck) + "\n"); + } + + // check the access bit of a folder. + if ( fiToCheck.isDir() ) { + if ( stat( toCheckEnc,&statbuffer ) != 0 ) { + kdDebug() << "wantItA: Can't read perms of " << toCheck << endl; + } + TQDir g( toCheck ); + if ( !g.isReadable() ) { + if ( chmod( toCheckEnc, statbuffer.st_mode + S_IXUSR ) != 0 ) { + error.append( i18n("%1 is not accessible and that is " + "unchangeable.").tqarg(toCheck) + "\n"); + } else { + kdDebug() << "Changed access bit for " << toCheck << endl; + } + } + } + + // For each file or folder we can check if the file is readable + // and writable, as requested. + if ( fiToCheck.isFile() || fiToCheck.isDir() ) { + + if ( !fiToCheck.isReadable() && wantItReadable ) { + // Get the current permissions. No need to do anything with an + // error, it will het added to errors anyhow, later on. + if ( stat(toCheckEnc,&statbuffer) != 0 ) { + kdDebug() << "wantItR: Can't read perms of " << toCheck << endl; + } + + // Lets try changing it. + if ( chmod( toCheckEnc, statbuffer.st_mode + S_IRUSR ) != 0 ) { + error.append( i18n("%1 is not readable and that is unchangeable.") + .tqarg(toCheck) + "\n"); + } else { + kdDebug() << "Changed the read bit for " << toCheck << endl; + } + } + + if ( !fiToCheck.isWritable() && wantItWritable ) { + // Gets the current persmissions. Needed because it can be changed + // curing previous operation. + if (stat(toCheckEnc,&statbuffer) != 0) { + kdDebug() << "wantItW: Can't read perms of " << toCheck << endl; + } + + // Lets try changing it. + if ( chmod (toCheckEnc, statbuffer.st_mode + S_IWUSR ) != 0 ) { + error.append( i18n("%1 is not writable and that is unchangeable.") + .tqarg(toCheck) + "\n"); + } else { + kdDebug() << "Changed the write bit for " << toCheck << endl; + } + } + } + + // If it is a folder and recursive is true, then we check the contents of + // the folder. + if ( fiToCheck.isDir() && recursive ){ + TQDir g(toCheck); + // First check if the folder is readable for us. If not, we get + // some ugly crashes. + if ( !g.isReadable() ){ + error.append(i18n("Folder %1 is inaccessible.").tqarg(toCheck) + "\n"); + } else { + const TQFileInfoList *list = g.entryInfoList(); + TQFileInfoListIterator it( *list ); + TQFileInfo *fi; + while ((fi = it.current()) != 0) { + TQString newToCheck = toCheck + "/" + fi->fileName(); + TQFileInfo fiNewToCheck(newToCheck); + if ( fi->fileName() != "." && fi->fileName() != ".." ) { + error.append ( checkAndCorrectPermissionsIfPossible( newToCheck, + recursive, wantItReadable, wantItWritable) ); + } + ++it; + } + } + } + return error; +} + +bool checkAndCorrectPermissionsIfPossibleWithErrorHandling( TQWidget *parent, + const TQString &toCheck, const bool recursive, const bool wantItReadable, + const bool wantItWritable ) +{ + TQString error = checkAndCorrectPermissionsIfPossible(toCheck, recursive, + wantItReadable, wantItWritable); + // There is no KMessageBox with Retry, Cancel and Details. + // so, I can't provide a functionality to recheck. So it now + // it is just a warning. + if ( !error.isEmpty() ) { + kdDebug() << "checkPermissions found:" << error << endl; + KMessageBox::detailedSorry(parent, + i18n("Some files or folders do not have " + "the right permissions, please correct them " + "manually."), + error, i18n("Permissions Check"), false); + return false; + } else { + return true; + } +} + +} diff --git a/libtdepim/kfileio.h b/libtdepim/kfileio.h new file mode 100644 index 000000000..15db09338 --- /dev/null +++ b/libtdepim/kfileio.h @@ -0,0 +1,105 @@ +/* Load / save entire (local) files with nice diagnostics dialog messages. + * These functions load/save the whole buffer in one i/o call, so they + * should be pretty efficient. + * + * Author: Stefan Taferner <taferner@kde.org> + * This code is under GPL. + */ +#ifndef kpim_kfileio_h +#define kpim_kfileio_h + +#include <tqcstring.h> +#include <tqwidget.h> + +#include <tdepimmacros.h> + +class TQString; + +namespace KPIM { + +/** Load a file. Returns a pointer to the memory-block that contains + * the loaded file. Returns a null string if the file could not be loaded. + * If withDialogs is FALSE no warning dialogs are opened if there are + * problems. + * The string returned is always zero-terminated and therefore one + * byte longer than the file itself. + * If ensureNewline is TRUE the string will always have a trailing newline. + */ +TQCString kFileToString(const TQString &fileName, bool ensureNewline=true, + bool withDialogs=true) KDE_EXPORT; + +// unused +//TQByteArray kFileToBytes(const TQString &fileName, bool withDialogs=true); + + +/** Save a file. If withDialogs is FALSE no warning dialogs are opened if + * there are problems. Returns TRUE on success and FALSE on failure. + * Replaces existing files without warning if askIfExists==FALSE. + * Makes a copy if the file exists to filename~ if createBackup==TRUE. + */ +bool kBytesToFile(const char* aBuffer, int len, + const TQString &aFileName, + bool aAskIfExists, bool aBackup, bool aVerbose) KDE_EXPORT; + +bool kCStringToFile(const TQCString& buffer, const TQString &fileName, + bool askIfExists=false, bool createBackup=true, + bool withDialogs=true) KDE_EXPORT; +/** Does not stop at NUL */ +KDE_EXPORT bool kByteArrayToFile(const TQByteArray& buffer, const TQString &fileName, + bool askIfExists=false, bool createBackup=true, + bool withDialogs=true) KDE_EXPORT; + + + /** + * Checks and corrects the permissions of a file or folder, and if requested + * all files and folders below. It gives back a list of files which do not + * have the right permissions. This list can be used to show to the user. + * + * @param toCheck The file or folder of which the permissions should + * be checked. + * @param recursive Set to true, it will check the contents of a folder + * for the permissions recursively. If false only + * toCheck will be checked. + * @param wantItReadable Set to true, it will check for read permissions. + * If the read permissions are not available, there will + * be a attempt to correct this. + * @param wantItWritable Set to true, it will check for write permissions. + * If the write permissions are not available, there + * will be a attempt to correct this. + * @return It will return a string with all files and folders which do not + * have the right permissions. If empty, then all permissions are ok. + */ +TQString checkAndCorrectPermissionsIfPossible( const TQString &toCheck, + const bool recursive, const bool wantItReadable, + const bool wantItWritable ); + + /** + * Checks and corrects the permissions of a file or folder, and if requested + * all files and folders below. If the permissions are not ok, it tries to correct + * them. If that fails then a warning with detailled information is given. + * + * @param parent If parent is 0, then the message box becomes an + * application-global modal dialog box. If parent + * is a widget, the message box becomes modal + * relative to parent. + * @param toCheck The file or folder of which the permissions should + * be checked. + * @param recursive Set to true, it will check the contents of a folder + * for the permissions recursively. If false only + * toCheck will be checked. + * @param wantItReadable Set to true, it will check for read permissions. + * If the read permissions are not available, there will + * be a attempt to correct this. + * @param wantItWritable Set to true, it will check for write permissions. + * If the write permissions are not available, there + * will be a attempt to correct this. + * @return It will return true if all permissions in the end are ok. If false + * then the permissions are not ok and it was not possible to correct + * all errors. + */ +bool checkAndCorrectPermissionsIfPossibleWithErrorHandling( TQWidget *parent, + const TQString &toCheck, const bool recursive, const bool wantItReadable, + const bool wantItWritable ); +} + +#endif /*kpim_kfileio_h*/ diff --git a/libtdepim/kfoldertree.cpp b/libtdepim/kfoldertree.cpp new file mode 100644 index 000000000..64859199a --- /dev/null +++ b/libtdepim/kfoldertree.cpp @@ -0,0 +1,542 @@ +// -*- c-basic-offset: 2 -*- + +#include "kfoldertree.h" +#include <klocale.h> +#include <kio/global.h> +#include <kiconloader.h> +#include <kdebug.h> +#include <kstringhandler.h> +#include <tqpainter.h> +#include <tqapplication.h> +#include <tqheader.h> +#include <tqstyle.h> + +//----------------------------------------------------------------------------- +KFolderTreeItem::KFolderTreeItem( KFolderTree *parent, const TQString & label, + Protocol protocol, Type type ) + : KListViewItem( parent, label ), mProtocol( protocol ), mType( type ), + mUnread(-1), mTotal(0), mSize(0), mFolderIsCloseToQuota( false ) +{ +} + +//----------------------------------------------------------------------------- +KFolderTreeItem::KFolderTreeItem( KFolderTreeItem *parent, + const TQString & label, Protocol protocol, Type type, + int unread, int total ) + : KListViewItem( parent, label ), mProtocol( protocol ), mType( type ), + mUnread( unread ), mTotal( total ), mSize(0), mFolderIsCloseToQuota( false ) +{ +} + +//----------------------------------------------------------------------------- +int KFolderTreeItem::protocolSortingKey() const +{ + // protocol dependant sorting order: + // local < imap < news < search < other + switch ( mProtocol ) { + case Local: + return 1; + case CachedImap: + case Imap: + return 2; + case News: + return 3; + case Search: + return 4; + default: + return 42; + } +} + +//----------------------------------------------------------------------------- +int KFolderTreeItem::typeSortingKey() const +{ + // type dependant sorting order: + // inbox < outbox < sent-mail < trash < drafts + // < calendar < contacts < notes < tasks + // < normal folders + switch ( mType ) { + case Inbox: + return 1; + case Outbox: + return 2; + case SentMail: + return 3; + case Trash: + return 4; + case Drafts: + return 5; + case Templates: + return 6; + case Calendar: + return 7; + case Contacts: + return 8; + case Notes: + return 9; + case Tasks: + return 10; + default: + return 42; + } +} + +//----------------------------------------------------------------------------- +int KFolderTreeItem::compare( TQListViewItem * i, int col, bool ) const +{ + KFolderTreeItem* other = static_cast<KFolderTreeItem*>( i ); + + if (col == 0) + { + // sort by folder + + // local root-folder + if ( depth() == 0 && mProtocol == NONE ) + return -1; + if ( other->depth() == 0 && other->protocol() == NONE ) + return 1; + + // first compare by protocol + int thisKey = protocolSortingKey(); + int thatKey = other->protocolSortingKey(); + if ( thisKey < thatKey ) + return -1; + if ( thisKey > thatKey ) + return 1; + + // then compare by type + thisKey = typeSortingKey(); + thatKey = other->typeSortingKey(); + if ( thisKey < thatKey ) + return -1; + if ( thisKey > thatKey ) + return 1; + + // and finally compare by name + return text( 0 ).localeAwareCompare( other->text( 0 ) ); + } + else + { + // sort by unread or total-column + TQ_INT64 a = 0, b = 0; + if (col == static_cast<KFolderTree*>(listView())->unreadIndex()) + { + a = mUnread; + b = other->unreadCount(); + } + else if (col == static_cast<KFolderTree*>(listView())->totalIndex()) + { + a = mTotal; + b = other->totalCount(); + } + else if (col == static_cast<KFolderTree*>(listView())->sizeIndex()) + { + a = mSize; + b = other->folderSize(); + } + + if ( a == b ) + return 0; + else + return (a < b ? -1 : 1); + } +} + +//----------------------------------------------------------------------------- +void KFolderTreeItem::setUnreadCount( int aUnread ) +{ + if ( aUnread < 0 ) return; + + mUnread = aUnread; + + TQString unread = TQString(); + if (mUnread == 0) + unread = "- "; + else { + unread.setNum(mUnread); + unread += " "; + } + + setText( static_cast<KFolderTree*>(listView())->unreadIndex(), + unread ); +} + +//----------------------------------------------------------------------------- +void KFolderTreeItem::setTotalCount( int aTotal ) +{ + if ( aTotal < 0 ) return; + + mTotal = aTotal; + + TQString total = TQString(); + if (mTotal == 0) + total = "- "; + else { + total.setNum(mTotal); + total += " "; + } + + setText( static_cast<KFolderTree*>(listView())->totalIndex(), + total ); +} + +//----------------------------------------------------------------------------- +void KFolderTreeItem::setFolderSize( TQ_INT64 aSize ) +{ + if ( aSize < 0 ) return; // we need to update even if nothing changed, kids ... + + mSize = aSize; + + TQString size; + if (mType != Root) { + if (mSize == 0 && (childCount() == 0 || isOpen() ) ) + size = "- "; + else + size = KIO::convertSize(mSize); + } + if ( childCount() > 0 && !isOpen() ) { + TQ_INT64 recursiveSize = recursiveFolderSize(); + if ( recursiveSize != mSize ) { + if ( mType != Root ) + size += TQString::tqfromLatin1(" + %1").tqarg( KIO::convertSize( recursiveSize - mSize ) ); + else + size = KIO::convertSize( recursiveSize ); + } + } + size += " "; + + setText( static_cast<KFolderTree*>(listView())->sizeIndex(), size ); +} + +//----------------------------------------------------------------------------- +TQ_INT64 KFolderTreeItem::recursiveFolderSize() const +{ + TQ_INT64 size = mSize; + + for ( TQListViewItem *item = firstChild() ; + item ; item = item->nextSibling() ) + { + size += static_cast<KFolderTreeItem*>(item)->recursiveFolderSize(); + } + return size; +} + + + +//----------------------------------------------------------------------------- +int KFolderTreeItem::countUnreadRecursive() +{ + int count = (mUnread > 0) ? mUnread : 0; + + for ( TQListViewItem *item = firstChild() ; + item ; item = item->nextSibling() ) + { + count += static_cast<KFolderTreeItem*>(item)->countUnreadRecursive(); + } + + return count; +} + +//----------------------------------------------------------------------------- +void KFolderTreeItem::paintCell( TQPainter * p, const TQColorGroup & cg, + int column, int width, int align ) +{ + KFolderTree *ft = static_cast<KFolderTree*>(listView()); + + const int unreadRecursiveCount = countUnreadRecursive(); + const int unreadCount = ( mUnread > 0 ) ? mUnread : 0; + + + // use a special color for folders which are close to their quota + TQColorGroup mycg = cg; + if ( ( column == 0 || column == ft->sizeIndex() ) && folderIsCloseToQuota() ) + { + mycg.setColor( TQColorGroup::Text, ft->paintInfo().colCloseToQuota ); + } + + // use a bold-font for the folder- and the unread-columns + if ( (column == 0 || column == ft->unreadIndex()) + && ( unreadCount > 0 + || ( !isOpen() && unreadRecursiveCount > 0 ) ) ) + { + TQFont f = p->font(); + f.setWeight(TQFont::Bold); + p->setFont(f); + } + + + // most cells can be handled by KListView::paintCell, we only need to + // deal with the folder column if the unread column is not shown + + /* The below is exceedingly silly, but Ingo insists that the unread + * count that is shown in parenthesis after the folder name must + * be configurable in color. That means that paintCell needs to do + * two painting passes which flickers. Since that flicker is not + * needed when there is the unread column, special case that. */ + if ( ft->isUnreadActive() || column != 0 ) { + KListViewItem::paintCell( p, mycg, column, width, align ); + } else { + TQListView *lv = listView(); + TQString oldText = text(column); + + // set an empty text so that we can have our own implementation (see further down) + // but still benefit from KListView::paintCell + setText( column, "" ); + + KListViewItem::paintCell( p, mycg, column, width, align ); + + const TQPixmap *icon = pixmap( column ); + int marg = lv ? lv->itemMargin() : 1; + int r = marg; + + setText( column, oldText ); + if ( isSelected() ) + p->setPen( mycg.highlightedText() ); + else + p->setPen( mycg.color( TQColorGroup::Text ) ); + + if ( icon ) { + r += icon->width() + marg; + } + TQString t = text( column ); + if (t.isEmpty()) + return; + + // draw the unread-count if the unread-column is not active + TQString unread; + + if ( unreadCount > 0 || ( !isOpen() && unreadRecursiveCount > 0 ) ) { + if ( isOpen() ) + unread = " (" + TQString::number( unreadCount ) + ")"; + else if ( unreadRecursiveCount == unreadCount || mType == Root ) + unread = " (" + TQString::number( unreadRecursiveCount ) + ")"; + else + unread = " (" + TQString::number( unreadCount ) + " + " + + TQString::number( unreadRecursiveCount-unreadCount ) + ")"; + } + + // check if the text needs to be squeezed + TQFontMetrics fm( p->fontMetrics() ); + int unreadWidth = fm.width( unread ); + if ( fm.width( t ) + marg + r + unreadWidth > width ) + t = squeezeFolderName( t, fm, width - marg - r - unreadWidth ); + + TQRect br; + p->drawText( r, 0, width-marg-r, height(), + align | AlignVCenter, t, -1, &br ); + + if ( !unread.isEmpty() ) { + if (!isSelected()) + p->setPen( ft->paintInfo().colUnread ); + p->drawText( br.right(), 0, width-marg-br.right(), height(), + align | AlignVCenter, unread ); + } + } +} + + +TQString KFolderTreeItem::squeezeFolderName( const TQString &text, + const TQFontMetrics &fm, + uint width ) const +{ + return KStringHandler::rPixelSqueeze( text, fm, width ); +} + +bool KFolderTreeItem::folderIsCloseToQuota() const +{ + return mFolderIsCloseToQuota; +} + +void KFolderTreeItem::setFolderIsCloseToQuota( bool v ) +{ + if ( mFolderIsCloseToQuota != v) { + mFolderIsCloseToQuota = v; + tqrepaint(); + } +} + + +//============================================================================= + + +KFolderTree::KFolderTree( TQWidget *parent, const char* name ) + : KListView( parent, name ), mUnreadIndex(-1), mTotalIndex(-1), mSizeIndex(-1) +{ + // GUI-options + setStyleDependantFrameWidth(); + setAcceptDrops(true); + setDropVisualizer(false); + setAllColumnsShowFocus(true); + setShowSortIndicator(true); + setUpdatesEnabled(true); + setItemsRenameable(false); + setRootIsDecorated(true); + setSelectionModeExt(Extended); + setAlternateBackground(TQColor()); +#if KDE_IS_VERSION( 3, 3, 90 ) + setShadeSortColumn ( false ); +#endif + setFullWidth(true); + disableAutoSelection(); + setColumnWidth( 0, 120 ); //reasonable default size + + disconnect( header(), TQT_SIGNAL( sizeChange( int, int, int ) ) ); + connect( header(), TQT_SIGNAL( sizeChange( int, int, int ) ), + TQT_SLOT( slotSizeChanged( int, int, int ) ) ); +} + +//----------------------------------------------------------------------------- +void KFolderTree::setStyleDependantFrameWidth() +{ + // set the width of the frame to a reasonable value for the current GUI style + int frameWidth; + if( tqstyle().isA("KeramikStyle") ) + frameWidth = tqstyle().tqpixelMetric( TQStyle::PM_DefaultFrameWidth ) - 1; + else + frameWidth = tqstyle().tqpixelMetric( TQStyle::PM_DefaultFrameWidth ); + if ( frameWidth < 0 ) + frameWidth = 0; + if ( frameWidth != lineWidth() ) + setLineWidth( frameWidth ); +} + +//----------------------------------------------------------------------------- +void KFolderTree::styleChange( TQStyle& oldStyle ) +{ + setStyleDependantFrameWidth(); + KListView::styleChange( oldStyle ); +} + +//----------------------------------------------------------------------------- +void KFolderTree::drawContentsOffset( TQPainter * p, int ox, int oy, + int cx, int cy, int cw, int ch ) +{ + bool oldUpdatesEnabled = isUpdatesEnabled(); + setUpdatesEnabled(false); + KListView::drawContentsOffset( p, ox, oy, cx, cy, cw, ch ); + setUpdatesEnabled(oldUpdatesEnabled); +} + +//----------------------------------------------------------------------------- +void KFolderTree::contentsMousePressEvent( TQMouseEvent *e ) +{ + setSelectionModeExt(Single); + KListView::contentsMousePressEvent(e); +} + +//----------------------------------------------------------------------------- +void KFolderTree::contentsMouseReleaseEvent( TQMouseEvent *e ) +{ + KListView::contentsMouseReleaseEvent(e); + setSelectionModeExt(Extended); +} + +//----------------------------------------------------------------------------- +void KFolderTree::addAcceptableDropMimetype( const char *mimeType, bool outsideOk ) +{ + int oldSize = mAcceptableDropMimetypes.size(); + mAcceptableDropMimetypes.resize(oldSize+1); + mAcceptOutside.resize(oldSize+1); + + mAcceptableDropMimetypes.at(oldSize) = mimeType; + mAcceptOutside.setBit(oldSize, outsideOk); +} + +//----------------------------------------------------------------------------- +bool KFolderTree::acceptDrag( TQDropEvent* event ) const +{ + TQListViewItem* item = itemAt(contentsToViewport(event->pos())); + + for (uint i = 0; i < mAcceptableDropMimetypes.size(); i++) + { + if (event->provides(mAcceptableDropMimetypes[i])) + { + if (item) + return (static_cast<KFolderTreeItem*>(item))->acceptDrag(event); + else + return mAcceptOutside[i]; + } + } + return false; +} + +//----------------------------------------------------------------------------- +void KFolderTree::addUnreadColumn( const TQString & name, int width ) +{ + mUnreadIndex = addColumn( name, width ); + setColumnAlignment( mUnreadIndex, tqApp->reverseLayout() ? TQt::AlignLeft : TQt::AlignRight ); + header()->adjustHeaderSize(); +} + +//----------------------------------------------------------------------------- +void KFolderTree::addTotalColumn( const TQString & name, int width ) +{ + mTotalIndex = addColumn( name, width ); + setColumnAlignment( mTotalIndex, tqApp->reverseLayout() ? TQt::AlignLeft : TQt::AlignRight ); + header()->adjustHeaderSize(); +} + +//----------------------------------------------------------------------------- +void KFolderTree::removeUnreadColumn() +{ + if ( !isUnreadActive() ) return; + removeColumn( mUnreadIndex ); + if ( isTotalActive() && mTotalIndex > mUnreadIndex ) + mTotalIndex--; + if ( isSizeActive() && mSizeIndex > mUnreadIndex ) + mSizeIndex--; + + mUnreadIndex = -1; + header()->adjustHeaderSize(); +} + +//----------------------------------------------------------------------------- +void KFolderTree::removeTotalColumn() +{ + if ( !isTotalActive() ) return; + removeColumn( mTotalIndex ); + if ( isUnreadActive() && mTotalIndex < mUnreadIndex ) + mUnreadIndex--; + if ( isSizeActive() && mTotalIndex < mSizeIndex ) + mSizeIndex--; + mTotalIndex = -1; + header()->adjustHeaderSize(); +} + +//----------------------------------------------------------------------------- +void KFolderTree::addSizeColumn( const TQString & name, int width ) +{ + mSizeIndex = addColumn( name, width ); + setColumnAlignment( mSizeIndex, tqApp->reverseLayout() ? TQt::AlignLeft : TQt::AlignRight ); + header()->adjustHeaderSize(); +} + +//----------------------------------------------------------------------------- +void KFolderTree::removeSizeColumn() +{ + if ( !isSizeActive() ) return; + removeColumn( mSizeIndex ); + if ( isUnreadActive() && mSizeIndex < mUnreadIndex ) + mUnreadIndex--; + if ( isTotalActive() && mSizeIndex < mTotalIndex ) + mTotalIndex--; + mSizeIndex = -1; + header()->adjustHeaderSize(); +} + + +//----------------------------------------------------------------------------- +void KFolderTree::setFullWidth( bool fullWidth ) +{ + if (fullWidth) + header()->setStretchEnabled( true, 0 ); +} + +//----------------------------------------------------------------------------- +void KFolderTree::slotSizeChanged( int section, int, int newSize ) +{ + viewport()->tqrepaint( + header()->sectionPos(section), 0, newSize, visibleHeight(), false ); +} + +#include "kfoldertree.moc" diff --git a/libtdepim/kfoldertree.h b/libtdepim/kfoldertree.h new file mode 100644 index 000000000..5ee255731 --- /dev/null +++ b/libtdepim/kfoldertree.h @@ -0,0 +1,319 @@ +/* -*- mode: C++; c-file-style: "gnu" -*- + + This file is part of libtdepim. + + Copyright (C) 2002 Carsten Burghardt <burghardt@kde.org> + Copyright (C) 2002 Marc Mutz <mutz@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. +*/ +#ifndef __KFOLDERTREE +#define __KFOLDERTREE + +#include <tqpixmap.h> +#include <tqbitarray.h> +#include <tqdragobject.h> +#include <tqcolor.h> +#include <klistview.h> +#include <tdepimmacros.h> + +class KFolderTree; + +/** Information shared by all items in a list view */ +struct KPaintInfo { + + // Popup ids for toggle-able columns + enum ColumnIds + { + COL_SIZE, + COL_ATTACHMENT, + COL_INVITATION, + COL_IMPORTANT, + COL_TODO, + COL_SPAM_HAM, + COL_WATCHED_IGNORED, + COL_STATUS, + COL_SIGNED, + COL_CRYPTO, + COL_RECEIVER, + COL_SCORE + }; + + KPaintInfo() : + pixmapOn(false), + + showSize(false), + showAttachment(false), + showInvitation(false), + showImportant(false), + showTodo(false), + showSpamHam(false), + showWatchedIgnored(false), + showtqStatus(false), + showSigned(false), + showCrypto(false), + showReceiver(false), + showScore(false), + + scoreCol(-1), + flagCol(-1), + senderCol(-1), + receiverCol(-1), + subCol(-1), + dateCol(-1), + sizeCol(-1), + attachmentCol(-1), + invitationCol(-1), + importantCol(-1), + todoCol(-1), + spamHamCol(-1), + watchedIgnoredCol(-1), + statusCol(-1), + signedCol(-1), + cryptoCol(-1), + + orderOfArrival(false), + status(false), + showCryptoIcons(false), + showAttachmentIcon(false), + showInvitationIcon(false) + {} + + bool pixmapOn; + TQPixmap pixmap; + TQColor colFore; + TQColor colBack; + TQColor colNew; + TQColor colUnread; + TQColor colFlag; + TQColor colTodo; + TQColor colCloseToQuota; + + bool showSize; + bool showAttachment; + bool showInvitation; + bool showImportant; + bool showTodo; + bool showSpamHam; + bool showWatchedIgnored; + bool showtqStatus; + bool showSigned; + bool showCrypto; + bool showReceiver; + bool showScore; + + int scoreCol; + int flagCol; + int senderCol; + int receiverCol; + int subCol; + int dateCol; + int sizeCol; + int attachmentCol; + int invitationCol; + int importantCol; + int todoCol; + int spamHamCol; + int watchedIgnoredCol; + int statusCol; + int signedCol; + int cryptoCol; + + bool orderOfArrival; + bool status; + bool showCryptoIcons; + bool showAttachmentIcon; + bool showInvitationIcon; +}; + +//========================================================================== + +class KDE_EXPORT KFolderTreeItem : public KListViewItem +{ + public: + /** Protocol information */ + enum Protocol { + Imap, + Local, + News, + CachedImap, + Search, + NONE + }; + + /** Type information */ + enum Type { + Inbox, + Outbox, + SentMail, + Trash, + Drafts, + Templates, + Root, + Calendar, + Tasks, + Journals, + Contacts, + Notes, + Other + }; + + /** constructs a root-item */ + KFolderTreeItem( KFolderTree *parent, const TQString & label=TQString(), + Protocol protocol=NONE, Type type=Root ); + + /** constructs a child-item */ + KFolderTreeItem( KFolderTreeItem *parent, const TQString & label=TQString(), + Protocol protocol=NONE, Type type=Other, int unread=0, int total=0 ); + + /** compare */ + virtual int compare( TQListViewItem * i, int col, + bool ascending ) const; + + /** set/get the unread-count */ + int unreadCount() { return mUnread; } + virtual void setUnreadCount( int aUnread ); + + /** set/get the total-count */ + int totalCount() { return mTotal; } + virtual void setTotalCount( int aTotal ); + + /** set/get the total-count */ + TQ_INT64 folderSize() { return mSize; } + virtual void setFolderSize( TQ_INT64 aSize ); + + /** set/get the protocol of the item */ + Protocol protocol() const { return mProtocol; } + virtual void setProtocol( Protocol aProtocol ) { mProtocol = aProtocol; } + + /** set/get the type of the item */ + Type type() const { return mType; } + virtual void setType( Type aType ) { mType = aType; } + + /** recursive unread count */ + virtual int countUnreadRecursive(); + + virtual TQ_INT64 recursiveFolderSize() const; + + /** paints the cell */ + virtual void paintCell( TQPainter * p, const TQColorGroup & cg, + int column, int width, int align ); + + /** dnd */ + virtual bool acceptDrag(TQDropEvent* ) const { return true; } + + void setFolderIsCloseToQuota( bool ); + bool folderIsCloseToQuota() const; + + private: + /** returns a sorting key based on the folder's protocol */ + int protocolSortingKey() const; + /** returns a sorting key based on the folder's type */ + int typeSortingKey() const; + + protected: + /** reimplement to use special squeezing algorithm for the folder name */ + virtual TQString squeezeFolderName( const TQString &text, + const TQFontMetrics &fm, + uint width ) const; + + Protocol mProtocol; + Type mType; + int mUnread; + int mTotal; + TQ_INT64 mSize; + bool mFolderIsCloseToQuota; +}; + +//========================================================================== + +class KDE_EXPORT KFolderTree : public KListView +{ + Q_OBJECT + TQ_OBJECT + + public: + KFolderTree( TQWidget *parent, const char *name=0 ); + + /** registers MIMETypes that are handled + @param mimeType the name of the MIMEType + @param outsideOk accept drops of this type even if + the mouse cursor is not on top of an item */ + virtual void addAcceptableDropMimetype( const char *mimeType, bool outsideOk ); + + /** checks if the drag is acceptable */ + virtual bool acceptDrag( TQDropEvent* event ) const; + + /** returns the KPaintInfo */ + KPaintInfo paintInfo() const { return mPaintInfo; } + + /** add/remove unread/total-columns */ + virtual void addUnreadColumn( const TQString & name, int width=70 ); + virtual void removeUnreadColumn(); + virtual void addTotalColumn( const TQString & name, int width=70 ); + virtual void removeTotalColumn(); + virtual void addSizeColumn( const TQString & name, int width=70 ); + virtual void removeSizeColumn(); + + + /** the current index of the unread/total column */ + int unreadIndex() const { return mUnreadIndex; } + int totalIndex() const { return mTotalIndex; } + int sizeIndex() const { return mSizeIndex; } + + /** is the unread/total-column active? */ + bool isUnreadActive() const { return mUnreadIndex >= 0; } + bool isTotalActive() const { return mTotalIndex >= 0; } + bool isSizeActive() const { return mSizeIndex >= 0; } + + /** reimp to set full width of the _first_ column */ + virtual void setFullWidth( bool fullWidth ); + + protected: + /** reimplemented in order to update the frame width in case of a changed + GUI style */ + void styleChange( TQStyle& oldStyle ); + + /** Set the width of the frame to a reasonable value for the current GUI + style */ + void setStyleDependantFrameWidth(); + + virtual void drawContentsOffset( TQPainter * p, int ox, int oy, + int cx, int cy, int cw, int ch ); + + virtual void contentsMousePressEvent( TQMouseEvent *e ); + virtual void contentsMouseReleaseEvent( TQMouseEvent *e ); + + /** for mimetypes */ + TQMemArray<const char*> mAcceptableDropMimetypes; + TQBitArray mAcceptOutside; + + /** shared information */ // ### why isn't it then static? ;-) + KPaintInfo mPaintInfo; + + /** current index of unread/total-column + * -1 is deactivated */ + int mUnreadIndex; + int mTotalIndex; + int mSizeIndex; + + private slots: + /** repaints the complete column (instead of only parts of it as done in + TQListView) if the size has changed */ + void slotSizeChanged( int section, int oldSize, int newSize ); + +}; + +#endif diff --git a/libtdepim/kimportdialog.cpp b/libtdepim/kimportdialog.cpp new file mode 100644 index 000000000..da6c98fad --- /dev/null +++ b/libtdepim/kimportdialog.cpp @@ -0,0 +1,767 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org> + Copyright (c) 2002 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +// Generic CSV import. Please do not add application specific code to this +// class. Application specific code should go to a subclass provided by the +// application using this dialog. + +#include <tqbuttongroup.h> +#include <tqfile.h> +#include <tqlabel.h> +#include <tqlayout.h> +#include <tqlineedit.h> +#include <tqlistview.h> +#include <tqradiobutton.h> +#include <tqregexp.h> +#include <tqtable.h> +#include <tqtextstream.h> +#include <tqvbox.h> + +#include <kapplication.h> +#include <kdebug.h> +#include <kcombobox.h> +#include <kinputdialog.h> +#include <klineedit.h> +#include <klocale.h> +#include <kprogress.h> +#include <ksimpleconfig.h> +#include <kstandarddirs.h> +#include <kurlrequester.h> +#include <kfiledialog.h> + +#include "kimportdialog.h" +#include "kimportdialog.moc" + +KImportColumn::KImportColumn(KImportDialog *dlg,const TQString &header, int count) + : m_maxCount(count), + m_refCount(0), + m_header(header), + mDialog(dlg) +{ + mFormats.append(FormatPlain); + mFormats.append(FormatUnquoted); +// mFormats.append(FormatBracketed); + + mDefaultFormat = FormatUnquoted; + + mDialog->addColumn(this); +} + +TQValueList<int> KImportColumn::formats() +{ + return mFormats; +} + +TQString KImportColumn::formatName(int format) +{ + switch (format) { + case FormatPlain: + return i18n("Plain"); + case FormatUnquoted: + return i18n("Unquoted"); + case FormatBracketed: + return i18n("Bracketed"); + default: + return i18n("Undefined"); + } +} + +int KImportColumn::defaultFormat() +{ + return mDefaultFormat; +} + +TQString KImportColumn::preview(const TQString &value, int format) +{ + if (format == FormatBracketed) { + return "(" + value + ")"; + } else if (format == FormatUnquoted) { + if (value.left(1) == "\"" && value.right(1) == "\"") { + return value.mid(1,value.length()-2); + } else { + return value; + } + } else { + return value; + } +} + +void KImportColumn::addColId(int id) +{ + mColIds.append(id); +} + +void KImportColumn::removeColId(int id) +{ + mColIds.remove(id); +} + +TQValueList<int> KImportColumn::colIdList() +{ + return mColIds; +} + +TQString KImportColumn::convert() +{ + TQValueList<int>::ConstIterator it = mColIds.begin(); + if (it == mColIds.end()) return ""; + else return mDialog->cell(*it); +} + + +class ColumnItem : public TQListViewItem { + public: + ColumnItem(KImportColumn *col,TQListView *parent) : TQListViewItem(parent), mColumn(col) + { + setText(0,mColumn->header()); + } + + KImportColumn *column() { return mColumn; } + + private: + KImportColumn *mColumn; +}; + +/** + This is a generic class for importing line-oriented data from text files. It + provides a dialog for file selection, preview, separator selection and column + assignment as well as generic conversion routines. For conversion to special + data objects, this class has to be inherited by a special class, which + reimplements the convertRow() function. +*/ +KImportDialog::KImportDialog(TQWidget* parent) + : KDialogBase(parent,"importdialog",true,i18n("Import Text File"),Ok|Cancel), + mSeparator(","), + mCurrentRow(0) +{ + mData.setAutoDelete( true ); + + TQVBox *topBox = new TQVBox(this); + setMainWidget(topBox); + topBox->setSpacing(spacingHint()); + + TQHBox *fileBox = new TQHBox(topBox); + fileBox->setSpacing(spacingHint()); + new TQLabel(i18n("File to import:"),fileBox); + KURLRequester *urlRequester = new KURLRequester(fileBox); + urlRequester->setFilter( "*.csv" ); + connect(urlRequester,TQT_SIGNAL(returnPressed(const TQString &)), + TQT_SLOT(setFile(const TQString &))); + connect(urlRequester,TQT_SIGNAL(urlSelected(const TQString &)), + TQT_SLOT(setFile(const TQString &))); + connect(urlRequester->lineEdit(),TQT_SIGNAL(textChanged ( const TQString & )), + TQT_SLOT(slotUrlChanged(const TQString & ))); + mTable = new TQTable(5,5,topBox); + mTable->setMinimumHeight( 150 ); + connect(mTable,TQT_SIGNAL(selectionChanged()),TQT_SLOT(tableSelected())); + + TQHBox *separatorBox = new TQHBox( topBox ); + separatorBox->setSpacing( spacingHint() ); + + new TQLabel( i18n( "Separator:" ), separatorBox ); + + mSeparatorCombo = new KComboBox( separatorBox ); + mSeparatorCombo->insertItem( "," ); + mSeparatorCombo->insertItem( i18n( "Tab" ) ); + mSeparatorCombo->insertItem( i18n( "Space" ) ); + mSeparatorCombo->insertItem( "=" ); + mSeparatorCombo->insertItem( ";" ); + connect(mSeparatorCombo, TQT_SIGNAL( activated(int) ), + this, TQT_SLOT( separatorClicked(int) ) ); + mSeparatorCombo->setCurrentItem( 0 ); + + TQHBox *rowsBox = new TQHBox( topBox ); + rowsBox->setSpacing( spacingHint() ); + + new TQLabel( i18n( "Import starts at row:" ), rowsBox ); + mStartRow = new TQSpinBox( rowsBox ); + mStartRow->setMinValue( 1 ); +/* + new TQLabel( i18n( "And ends at row:" ), rowsBox ); + mEndRow = new TQSpinBox( rowsBox ); + mEndRow->setMinValue( 1 ); +*/ + TQVBox *assignBox = new TQVBox(topBox); + assignBox->setSpacing(spacingHint()); + + TQHBox *listsBox = new TQHBox(assignBox); + listsBox->setSpacing(spacingHint()); + + mHeaderList = new TQListView(listsBox); + mHeaderList->addColumn(i18n("Header")); + connect(mHeaderList, TQT_SIGNAL(selectionChanged(TQListViewItem*)), + this, TQT_SLOT(headerSelected(TQListViewItem*))); + connect(mHeaderList,TQT_SIGNAL(doubleClicked(TQListViewItem*)), + TQT_SLOT(assignColumn(TQListViewItem *))); + + mFormatCombo = new KComboBox( listsBox ); + mFormatCombo->setDuplicatesEnabled( false ); + + TQPushButton *assignButton = new TQPushButton(i18n("Assign to Selected Column"), + assignBox); + connect(assignButton,TQT_SIGNAL(clicked()),TQT_SLOT(assignColumn())); + + TQPushButton *removeButton = new TQPushButton(i18n("Remove Assignment From Selected Column"), + assignBox); + connect(removeButton,TQT_SIGNAL(clicked()),TQT_SLOT(removeColumn())); + + TQPushButton *assignTemplateButton = new TQPushButton(i18n("Assign with Template..."), + assignBox); + connect(assignTemplateButton,TQT_SIGNAL(clicked()),TQT_SLOT(assignTemplate())); + + TQPushButton *saveTemplateButton = new TQPushButton(i18n("Save Current Template"), + assignBox); + connect(saveTemplateButton,TQT_SIGNAL(clicked()),TQT_SLOT(saveTemplate())); + + resize(500,300); + + connect(this,TQT_SIGNAL(okClicked()),TQT_SLOT(applyConverter())); + connect(this,TQT_SIGNAL(applyClicked()),TQT_SLOT(applyConverter())); + enableButtonOK(!urlRequester->lineEdit()->text().isEmpty()); +} + +void KImportDialog::slotUrlChanged(const TQString & text) +{ + enableButtonOK(!text.isEmpty()); +} + +bool KImportDialog::setFile(const TQString& file) +{ + enableButtonOK(!file.isEmpty()); + kdDebug(5300) << "KImportDialog::setFile(): " << file << endl; + + TQFile f(file); + + if (f.open(IO_ReadOnly)) { + mFile = ""; + TQTextStream t(&f); + mFile = t.read(); +// while (!t.eof()) mFile.append(t.readLine()); + f.close(); + + readFile(); + +// mEndRow->setValue( mData.count() ); + + return true; + } else { + kdDebug(5300) << " Open failed" << endl; + return false; + } +} + +void KImportDialog::registerColumns() +{ + TQPtrListIterator<KImportColumn> colIt(mColumns); + for (; colIt.current(); ++colIt) { + new ColumnItem(*colIt,mHeaderList); + } + mHeaderList->setSelected(mHeaderList->firstChild(),true); +} + +void KImportDialog::fillTable() +{ +// kdDebug(5300) << "KImportDialog::fillTable()" << endl; + + int row, column; + + for (row = 0; row < mTable->numRows(); ++row) + for (column = 0; column < mTable->numCols(); ++column) + mTable->clearCell(row, column); + + for ( row = 0; row < int(mData.count()); ++row ) { + TQValueVector<TQString> *rowVector = mData[ row ]; + for( column = 0; column < int(rowVector->size()); ++column ) { + setCellText( row, column, rowVector->at( column ) ); + } + } +} + +void KImportDialog::readFile( int rows ) +{ + kdDebug(5300) << "KImportDialog::readFile(): " << rows << endl; + + mData.clear(); + + int row, column; + enum { S_START, S_TQUOTED_FIELD, S_MAYBE_END_OF_TQUOTED_FIELD, S_END_OF_TQUOTED_FIELD, + S_MAYBE_NORMAL_FIELD, S_NORMAL_FIELD } state = S_START; + + TQChar m_textquote = '"'; + int m_startline = 0; + + TQChar x; + TQString field = ""; + + row = column = 0; + TQTextStream inputStream(mFile, IO_ReadOnly); + inputStream.setEncoding(TQTextStream::Locale); + + KProgressDialog pDialog(this, 0, i18n("Loading Progress"), + i18n("Please wait while the file is loaded."), true); + pDialog.setAllowCancel(true); + pDialog.showCancelButton(true); + pDialog.setAutoClose(true); + + KProgress *progress = pDialog.progressBar(); + progress->setTotalSteps( mFile.contains(mSeparator, false) ); + progress->setValue(0); + int progressValue = 0; + + if (progress->totalSteps() > 0) // We have data + pDialog.show(); + + while (!inputStream.atEnd() && !pDialog.wasCancelled()) { + inputStream >> x; // read one char + + // update the dialog if needed + if (x == mSeparator) + { + progress->setValue(progressValue++); + if (progressValue % 15 == 0) // try not to constantly tqrepaint + kapp->processEvents(); + } + + if (x == '\r') inputStream >> x; // eat '\r', to handle DOS/LOSEDOWS files correctly + + switch (state) { + case S_START : + if (x == m_textquote) { + field += x; + state = S_TQUOTED_FIELD; + } else if (x == mSeparator) { + ++column; + } else if (x == '\n') { + ++row; + column = 0; + } else { + field += x; + state = S_MAYBE_NORMAL_FIELD; + } + break; + case S_TQUOTED_FIELD : + if (x == m_textquote) { + field += x; + state = S_MAYBE_END_OF_TQUOTED_FIELD; + } else if (x == '\n') { + setData(row - m_startline, column, field); + field = ""; + if (x == '\n') { + ++row; + column = 0; + } else { + ++column; + } + state = S_START; + } else { + field += x; + } + break; + case S_MAYBE_END_OF_TQUOTED_FIELD : + if (x == m_textquote) { + field += x; + state = S_TQUOTED_FIELD; + } else if (x == mSeparator || x == '\n') { + setData(row - m_startline, column, field); + field = ""; + if (x == '\n') { + ++row; + column = 0; + } else { + ++column; + } + state = S_START; + } else { + state = S_END_OF_TQUOTED_FIELD; + } + break; + case S_END_OF_TQUOTED_FIELD : + if (x == mSeparator || x == '\n') { + setData(row - m_startline, column, field); + field = ""; + if (x == '\n') { + ++row; + column = 0; + } else { + ++column; + } + state = S_START; + } else { + state = S_END_OF_TQUOTED_FIELD; + } + break; + case S_MAYBE_NORMAL_FIELD : + if (x == m_textquote) { + field = ""; + state = S_TQUOTED_FIELD; + } + case S_NORMAL_FIELD : + if (x == mSeparator || x == '\n') { + setData(row - m_startline, column, field); + field = ""; + if (x == '\n') { + ++row; + column = 0; + } else { + ++column; + } + state = S_START; + } else { + field += x; + } + } + + if ( rows > 0 && row > rows ) break; + } + + fillTable(); +} + +void KImportDialog::setCellText(int row, int col, const TQString& text) +{ + if (row < 0) return; + + if ((mTable->numRows() - 1) < row) mTable->setNumRows(row + 1); + if ((mTable->numCols() - 1) < col) mTable->setNumCols(col + 1); + + KImportColumn *c = mColumnDict.find(col); + TQString formattedText; + if (c) formattedText = c->preview(text,findFormat(col)); + else formattedText = text; + mTable->setText(row, col, formattedText); +} + +void KImportDialog::formatSelected(TQListViewItem*) +{ +// kdDebug(5300) << "KImportDialog::formatSelected()" << endl; +} + +void KImportDialog::headerSelected(TQListViewItem* item) +{ + KImportColumn *col = ((ColumnItem *)item)->column(); + + if (!col) return; + + mFormatCombo->clear(); + + TQValueList<int> formats = col->formats(); + + TQValueList<int>::ConstIterator it = formats.begin(); + TQValueList<int>::ConstIterator end = formats.end(); + while(it != end) { + mFormatCombo->insertItem( col->formatName(*it), *it - 1 ); + ++it; + } + + TQTableSelection selection = mTable->selection(mTable->currentSelection()); + + updateFormatSelection(selection.leftCol()); +} + +void KImportDialog::updateFormatSelection(int column) +{ + int format = findFormat(column); + + if ( format == KImportColumn::FormatUndefined ) + mFormatCombo->setCurrentItem( 0 ); + else + mFormatCombo->setCurrentItem( format - 1 ); +} + +void KImportDialog::tableSelected() +{ + TQTableSelection selection = mTable->selection(mTable->currentSelection()); + + TQListViewItem *item = mHeaderList->firstChild(); + KImportColumn *col = mColumnDict.find(selection.leftCol()); + if (col) { + while(item) { + if (item->text(0) == col->header()) { + break; + } + item = item->nextSibling(); + } + } + if (item) { + mHeaderList->setSelected(item,true); + } + + updateFormatSelection(selection.leftCol()); +} + +void KImportDialog::separatorClicked(int id) +{ + switch(id) { + case 0: + mSeparator = ','; + break; + case 1: + mSeparator = '\t'; + break; + case 2: + mSeparator = ' '; + break; + case 3: + mSeparator = '='; + break; + case 4: + mSeparator = ';'; + break; + default: + mSeparator = ','; + break; + } + + readFile(); +} + +void KImportDialog::assignColumn(TQListViewItem *item) +{ + if (!item) return; + +// kdDebug(5300) << "KImportDialog::assignColumn(): current Col: " << mTable->currentColumn() +// << endl; + + ColumnItem *colItem = (ColumnItem *)item; + + TQTableSelection selection = mTable->selection(mTable->currentSelection()); + +// kdDebug(5300) << " l: " << selection.leftCol() << " r: " << selection.rightCol() << endl; + + for(int i=selection.leftCol();i<=selection.rightCol();++i) { + if (i >= 0) { + mTable->horizontalHeader()->setLabel(i,colItem->text(0)); + mColumnDict.replace(i,colItem->column()); + int format = mFormatCombo->currentItem() + 1; + mFormats.replace(i,format); + colItem->column()->addColId(i); + } + } + + readFile(); +} + +void KImportDialog::assignColumn() +{ + assignColumn(mHeaderList->currentItem()); +} + +void KImportDialog::assignTemplate() +{ + TQMap<uint,int> columnMap; + TQMap<TQString, TQString> fileMap; + TQStringList templates; + + // load all template files + TQStringList list = KGlobal::dirs()->findAllResources( "data" , TQString( kapp->name() ) + + "/csv-templates/*.desktop", true, true ); + + for ( TQStringList::iterator it = list.begin(); it != list.end(); ++it ) + { + KSimpleConfig config( *it, true ); + + if ( !config.hasGroup( "csv column map" ) ) + continue; + + config.setGroup( "Misc" ); + templates.append( config.readEntry( "Name" ) ); + fileMap.insert( config.readEntry( "Name" ), *it ); + } + + // let the user chose, what to take + bool ok = false; + TQString tmp; + tmp = KInputDialog::getItem( i18n( "Template Selection" ), + i18n( "Please select a template, that matches the CSV file:" ), + templates, 0, false, &ok, this ); + + if ( !ok ) + return; + + KSimpleConfig config( fileMap[ tmp ], true ); + config.setGroup( "General" ); + uint numColumns = config.readUnsignedNumEntry( "Columns" ); + int format = config.readNumEntry( "Format" ); + + // create the column map + config.setGroup( "csv column map" ); + for ( uint i = 0; i < numColumns; ++i ) { + int col = config.readNumEntry( TQString::number( i ) ); + columnMap.insert( i, col ); + } + + // apply the column map + for ( uint i = 0; i < columnMap.count(); ++i ) { + int tableColumn = columnMap[i]; + if ( tableColumn == -1 ) + continue; + KImportColumn *col = mColumns.at(i); + mTable->horizontalHeader()->setLabel( tableColumn, col->header() ); + mColumnDict.replace( tableColumn, col ); + mFormats.replace( tableColumn, format ); + col->addColId( tableColumn ); + } + + readFile(); +} + +void KImportDialog::removeColumn() +{ + TQTableSelection selection = mTable->selection(mTable->currentSelection()); + +// kdDebug(5300) << " l: " << selection.leftCol() << " r: " << selection.rightCol() << endl; + + for(int i=selection.leftCol();i<=selection.rightCol();++i) { + if (i >= 0) { + mTable->horizontalHeader()->setLabel(i,TQString::number(i+1)); + KImportColumn *col = mColumnDict.find(i); + if (col) { + mColumnDict.remove(i); + mFormats.remove(i); + col->removeColId(i); + } + } + } + + readFile(); +} + +void KImportDialog::applyConverter() +{ + kdDebug(5300) << "KImportDialog::applyConverter" << endl; + + KProgressDialog pDialog(this, 0, i18n("Importing Progress"), + i18n("Please wait while the data is imported."), true); + pDialog.setAllowCancel(true); + pDialog.showCancelButton(true); + pDialog.setAutoClose(true); + + KProgress *progress = pDialog.progressBar(); + progress->setTotalSteps( mTable->numRows()-1 ); + progress->setValue(0); + + readFile( 0 ); + + pDialog.show(); + for( uint i = mStartRow->value() - 1; i < mData.count() && !pDialog.wasCancelled(); ++i ) { + mCurrentRow = i; + progress->setValue(i); + if (i % 5 == 0) // try to avoid constantly processing events + kapp->processEvents(); + + convertRow(); + } +} + +int KImportDialog::findFormat(int column) +{ + TQMap<int,int>::ConstIterator formatIt = mFormats.find(column); + int format; + if (formatIt == mFormats.end()) format = KImportColumn::FormatUndefined; + else format = *formatIt; + +// kdDebug(5300) << "KImportDialog::findformat(): " << column << ": " << format << endl; + + return format; +} + +TQString KImportDialog::cell(uint col) +{ + if ( col >= mData[ mCurrentRow ]->size() ) return ""; + else return data( mCurrentRow, col ); +} + +void KImportDialog::addColumn(KImportColumn *col) +{ + mColumns.append(col); +} + +void KImportDialog::setData( uint row, uint col, const TQString &value ) +{ + TQString val = value; + val.replace( "\\n", "\n" ); + + if ( row >= mData.count() ) { + mData.resize( row + 1 ); + } + + TQValueVector<TQString> *rowVector = mData[ row ]; + if ( !rowVector ) { + rowVector = new TQValueVector<TQString>; + mData.insert( row, rowVector ); + } + if ( col >= rowVector->size() ) { + rowVector->resize( col + 1 ); + } + + KImportColumn *c = mColumnDict.find( col ); + if ( c ) + rowVector->at( col ) = c->preview( val, findFormat(col) ); + else + rowVector->at( col ) = val; +} + +TQString KImportDialog::data( uint row, uint col ) +{ + return mData[ row ]->at( col ); +} + +void KImportDialog::saveTemplate() +{ + TQString fileName = KFileDialog::getSaveFileName( + locateLocal( "data", TQString( kapp->name() ) + "/csv-templates/" ), + "*.desktop", this ); + + if ( fileName.isEmpty() ) + return; + + if ( !fileName.contains( ".desktop" ) ) + fileName += ".desktop"; + + TQString name = KInputDialog::getText( i18n( "Template Name" ), i18n( "Please enter a name for the template:" ) ); + + if ( name.isEmpty() ) + return; + + KConfig config( fileName ); + config.setGroup( "General" ); + config.writeEntry( "Columns", mColumns.count() ); + config.writeEntry( "Format", mFormatCombo->currentItem() + 1 ); + + config.setGroup( "Misc" ); + config.writeEntry( "Name", name ); + + config.setGroup( "csv column map" ); + + KImportColumn *column; + uint counter = 0; + for ( column = mColumns.first(); column; column = mColumns.next() ) { + TQValueList<int> list = column->colIdList(); + if ( list.count() > 0 ) + config.writeEntry( TQString::number( counter ), list[ 0 ] ); + else + config.writeEntry( TQString::number( counter ), -1 ); + counter++; + } + + config.sync(); +} diff --git a/libtdepim/kimportdialog.h b/libtdepim/kimportdialog.h new file mode 100644 index 000000000..abcbb3181 --- /dev/null +++ b/libtdepim/kimportdialog.h @@ -0,0 +1,137 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org> + Copyright (c) 2002 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef KIMPORTDIALOG_H +#define KIMPORTDIALOG_H + +#include <tqintdict.h> +#include <tqstringlist.h> +#include <tqspinbox.h> +#include <tqptrvector.h> +#include <tqvaluevector.h> + +#include <kdialogbase.h> + +class TQTable; +class TQListView; + +class KImportDialog; +class KComboBox; + +class KImportColumn +{ + public: + enum { FormatUndefined = 0, FormatPlain, FormatUnquoted, FormatBracketed, FormatLast }; + + KImportColumn(KImportDialog *dlg, const TQString &header, int count = 0); + virtual ~KImportColumn() {} + + TQString header() const { return m_header; } + + TQValueList<int> formats(); + TQString formatName(int format); + int defaultFormat(); + + TQString convert(); +// virtual void convert(const TQString &value,int format) = 0; + TQString preview(const TQString &value,int format); + + void addColId(int i); + void removeColId(int i); + + TQValueList<int> colIdList(); + + protected: + + private: + int m_maxCount, m_refCount; + + TQString m_header; + TQValueList<int> mFormats; + int mDefaultFormat; + + TQValueList<int> mColIds; + + KImportDialog *mDialog; +}; + +class KImportDialog : public KDialogBase +{ + Q_OBJECT + TQ_OBJECT + public: + KImportDialog(TQWidget* parent); + + public slots: + bool setFile(const TQString& file); + + TQString cell(uint row); + + void addColumn(KImportColumn *); + + protected: + void readFile( int rows = 10 ); + + void fillTable(); + void registerColumns(); + int findFormat(int column); + + virtual void convertRow() {} + + protected slots: + void separatorClicked(int id); + void formatSelected(TQListViewItem* item); + void headerSelected(TQListViewItem* item); + void assignColumn(TQListViewItem *); + void assignColumn(); + void assignTemplate(); + void removeColumn(); + void applyConverter(); + void tableSelected(); + void slotUrlChanged(const TQString & ); + void saveTemplate(); + + private: + void updateFormatSelection(int column); + void setCellText(int row, int col, const TQString& text); + + void setData( uint row, uint col, const TQString &text ); + TQString data( uint row, uint col ); + + TQListView *mHeaderList; + TQSpinBox *mStartRow; + TQSpinBox *mEndRow; + TQTable *mTable; + + KComboBox *mFormatCombo; + KComboBox *mSeparatorCombo; + + TQString mSeparator; + int mCurrentRow; + TQString mFile; + TQIntDict<KImportColumn> mColumnDict; + TQIntDict<uint> mTemplateDict; + TQMap<int,int> mFormats; + TQPtrList<KImportColumn> mColumns; + TQPtrVector<TQValueVector<TQString> > mData; +}; + +#endif diff --git a/libtdepim/kincidencechooser.cpp b/libtdepim/kincidencechooser.cpp new file mode 100644 index 000000000..fee89bbb6 --- /dev/null +++ b/libtdepim/kincidencechooser.cpp @@ -0,0 +1,326 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Lutz Rogowski <rogowski@kde.org> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + As a special exception, permission is given to link this program + with any edition of TQt, and distribute the resulting executable, + without including the source code for TQt in the source distribution. +*/ + +#include <tqlayout.h> +#include <tqlabel.h> +#include <tqbuttongroup.h> +#include <tqvbox.h> +#include <tqhbox.h> +#include <tqradiobutton.h> +#include <tqpushbutton.h> +#include <tqlayout.h> +#include <tqscrollview.h> +#include <tqtextbrowser.h> +#include <tqapplication.h> + + +#include <klocale.h> +#include <kglobal.h> + +#include "kincidencechooser.h" +#include "libkcal/incidence.h" +#include "libkcal/incidenceformatter.h" + +int KIncidenceChooser::chooseMode = KIncidenceChooser::ask ; + +KIncidenceChooser::KIncidenceChooser(TQWidget *parent, char *name) : + KDialog(parent,name,true) +{ + KDialog *topFrame = this; + TQGridLayout *topLayout = new TQGridLayout(topFrame,5,3); + int iii = 0; + setCaption( i18n("Conflict Detected")); + TQLabel * lab; + lab = new TQLabel( i18n( + "<qt>A conflict was detected. This probably means someone edited the same entry on the server while you changed it locally." + "<br/>NOTE: You have to check mail again to apply your changes to the server.</qt>"), topFrame); + topLayout->addMultiCellWidget(lab, iii,iii,0,2); + ++iii; + TQHBox * b_box = new TQHBox( topFrame ); + topLayout->addMultiCellWidget(b_box, iii,iii,0,2); + ++iii; + TQPushButton* button = new TQPushButton( i18n("Take Local"), b_box ); + connect ( button, TQT_SIGNAL( clicked()), this, TQT_SLOT (takeIncidence1() ) ); + button = new TQPushButton( i18n("Take New"), b_box ); + connect ( button, TQT_SIGNAL( clicked()), this, TQT_SLOT (takeIncidence2() ) ); + button = new TQPushButton( i18n("Take Both"), b_box ); + connect ( button, TQT_SIGNAL( clicked()), this, TQT_SLOT (takeBoth() ) ); + topLayout->setSpacing(spacingHint()); + topLayout->setMargin(marginHint()); + // text is not translated, because text has to be set later + mInc1lab = new TQLabel ( i18n("Local incidence"), topFrame); + topLayout->addWidget(mInc1lab ,iii,0); + mInc1Sumlab = new TQLabel ( i18n("Local incidence summary"), topFrame); + topLayout->addMultiCellWidget(mInc1Sumlab, iii,iii,1,2); + ++iii; + topLayout->addWidget( new TQLabel ( i18n("Last modified:"), topFrame) ,iii,0); + mMod1lab = new TQLabel ( "Set Last modified", topFrame); + topLayout->addWidget(mMod1lab,iii,1); + mShowDetails1 = new TQPushButton( i18n("Show Details"),topFrame ); + connect ( mShowDetails1, TQT_SIGNAL( clicked()), this, TQT_SLOT (showIncidence1() ) ); + topLayout->addWidget(mShowDetails1,iii,2); + ++iii; + + mInc2lab = new TQLabel ( "Local incidence", topFrame); + topLayout->addWidget(mInc2lab,iii,0); + mInc2Sumlab = new TQLabel ( "Local incidence summary", topFrame); + topLayout->addMultiCellWidget(mInc2Sumlab, iii,iii,1,2); + ++iii; + topLayout->addWidget( new TQLabel ( i18n("Last modified:"), topFrame) ,iii,0); + mMod2lab = new TQLabel ( "Set Last modified", topFrame); + topLayout->addWidget(mMod2lab,iii,1); + mShowDetails2 = new TQPushButton( i18n("Show Details"), topFrame); + connect ( mShowDetails2, TQT_SIGNAL( clicked()), this, TQT_SLOT (showIncidence2() ) ); + topLayout->addWidget(mShowDetails2,iii,2); + ++iii; + // +#if 0 + // commented out for now, because the diff code has too many bugs + mDiffBut = new TQPushButton( i18n("Show Differences"), topFrame ); + connect ( mDiffBut, TQT_SIGNAL( clicked()), this, TQT_SLOT ( showDiff() ) ); + topLayout->addMultiCellWidget(mDiffBut, iii,iii,0,2); + ++iii; +#else + mDiffBut = 0; +#endif + mBg = new TQButtonGroup ( 1, Qt::Horizontal, i18n("Sync Preferences"), topFrame); + topLayout->addMultiCellWidget(mBg, iii,iii,0,2); + ++iii; + mBg->insert( new TQRadioButton ( i18n("Take local entry on conflict"), mBg ), KIncidenceChooser::local); + mBg->insert( new TQRadioButton ( i18n("Take new (remote) entry on conflict"), mBg ), KIncidenceChooser::remote); + mBg->insert( new TQRadioButton ( i18n("Take newest entry on conflict"), mBg ), KIncidenceChooser::newest ); + mBg->insert( new TQRadioButton ( i18n("Ask for every entry on conflict"), mBg ),KIncidenceChooser::ask ); + mBg->insert( new TQRadioButton ( i18n("Take both on conflict"), mBg ), KIncidenceChooser::both ); + mBg->setButton ( chooseMode ); + mTbL = 0; + mTbN = 0; + mDisplayDiff = 0; + mSelIncidence = 0; + button = new TQPushButton( i18n("Apply This to All Conflicts of This Sync"), topFrame ); + connect ( button, TQT_SIGNAL( clicked()), this, TQT_SLOT ( setSyncMode() ) ); + topLayout->addMultiCellWidget(button, iii,iii,0,2); +} + +KIncidenceChooser::~KIncidenceChooser() +{ + if ( mTbL ) delete mTbL; + if ( mTbN ) delete mTbN; + if ( mDisplayDiff ) { + delete mDisplayDiff; + delete diff; + } +} + +void KIncidenceChooser::setIncidence( KCal::Incidence* local ,KCal::Incidence* remote ) +{ + mInc1 = local; + mInc2 = remote; + setLabels(); + +} +KCal::Incidence* KIncidenceChooser::getIncidence( ) +{ + + KCal::Incidence* retval = mSelIncidence; + if ( chooseMode == KIncidenceChooser::local ) + retval = mInc1; + else if ( chooseMode == KIncidenceChooser::remote ) + retval = mInc2; + else if ( chooseMode == KIncidenceChooser::both ) { + retval = 0; + } + else if ( chooseMode == KIncidenceChooser::newest ) { + if ( mInc1->lastModified() == mInc2->lastModified()) + retval = 0; + if ( mInc1->lastModified() > mInc2->lastModified() ) + retval = mInc1; + else + retval = mInc2; + } + return retval; +} + +void KIncidenceChooser::setSyncMode() +{ + chooseMode = mBg->selectedId (); + if ( chooseMode != KIncidenceChooser::ask ) + TQDialog::accept(); + +} + +void KIncidenceChooser::useGlobalMode() +{ + if ( chooseMode != KIncidenceChooser::ask ) + TQDialog::reject(); +} + +void KIncidenceChooser::setLabels() +{ + KCal::Incidence* inc = mInc1; + TQLabel* des = mInc1lab; + TQLabel * sum = mInc1Sumlab; + + + if ( inc->type() == "Event" ) { + des->setText( i18n( "Local Event") ); + sum->setText( inc->summary().left( 30 )); + if ( mDiffBut ) + mDiffBut->setEnabled( true ); + } + else if ( inc->type() == "Todo" ) { + des->setText( i18n( "Local Todo") ); + sum->setText( inc->summary().left( 30 )); + if ( mDiffBut ) + mDiffBut->setEnabled( true ); + + } + else if ( inc->type() == "Journal" ) { + des->setText( i18n( "Local Journal") ); + sum->setText( inc->description().left( 30 )); + if ( mDiffBut ) + mDiffBut->setEnabled( false ); + } + mMod1lab->setText( KGlobal::locale()->formatDateTime(inc->lastModified() )); + inc = mInc2; + des = mInc2lab; + sum = mInc2Sumlab; + if ( inc->type() == "Event" ) { + des->setText( i18n( "New Event") ); + sum->setText( inc->summary().left( 30 )); + } + else if ( inc->type() == "Todo" ) { + des->setText( i18n( "New Todo") ); + sum->setText( inc->summary().left( 30 )); + + } + else if ( inc->type() == "Journal" ) { + des->setText( i18n( "New Journal") ); + sum->setText( inc->description().left( 30 )); + + } + mMod2lab->setText( KGlobal::locale()->formatDateTime(inc->lastModified() )); +} + +void KIncidenceChooser::showIncidence1() +{ + if ( mTbL ) { + if ( mTbL->isVisible() ) { + mShowDetails1->setText( i18n("Show Details")); + mTbL->hide(); + } else { + mShowDetails1->setText( i18n("Hide Details")); + mTbL->show(); + mTbL->raise(); + } + return; + } + mTbL = new KDialogBase( this, "", false /*not modal*/, mInc1lab->text(), KDialogBase::Ok ); + mTbL->setEscapeButton( KDialogBase::Ok ); + connect( mTbL, TQT_SIGNAL( okClicked() ), this, TQT_SLOT( detailsDialogClosed() ) ); + TQTextBrowser* textBrowser = new TQTextBrowser( mTbL ); + mTbL->setMainWidget( textBrowser ); + textBrowser->setText( KCal::IncidenceFormatter::extensiveDisplayString( mInc1 ) ); + mTbL->setMinimumSize( 400, 400 ); + mShowDetails1->setText( i18n("Hide Details")); + mTbL->show(); + mTbL->raise(); +} + +void KIncidenceChooser::detailsDialogClosed() +{ + KDialogBase* dialog = static_cast<KDialogBase *>( TQT_TQWIDGET( const_cast<TQT_BASE_OBJECT_NAME*>(sender()) ) ); + if ( dialog == mTbL ) + mShowDetails1->setText( i18n( "Show details..." ) ); + else + mShowDetails2->setText( i18n( "Show details..." ) ); +} + +void KIncidenceChooser::showDiff() +{ + if ( mDisplayDiff ) { + mDisplayDiff->show(); + mDisplayDiff->raise(); + return; + } + mDisplayDiff = new KPIM::HTMLDiffAlgoDisplay (this); + if ( mInc1->summary().left( 20 ) != mInc2->summary().left( 20 ) ) + mDisplayDiff->setCaption( i18n( "Differences of %1 and %2").tqarg( mInc1->summary().left( 20 ) ).tqarg( mInc2->summary().left( 20 ) ) ); + else + mDisplayDiff->setCaption( i18n( "Differences of %1").tqarg( mInc1->summary().left( 20 ) ) ); + + diff = new KPIM::CalendarDiffAlgo( mInc1, mInc2); + diff->setLeftSourceTitle( i18n( "Local entry")); + diff->setRightSourceTitle(i18n( "New (remote) entry") ); + diff->addDisplay( mDisplayDiff ); + diff->run(); + mDisplayDiff->show(); + mDisplayDiff->raise(); +} + +void KIncidenceChooser::showIncidence2() +{ + if ( mTbN ) { + if ( mTbN->isVisible() ) { + mShowDetails2->setText( i18n("Show Details")); + mTbN->hide(); + } else { + mShowDetails2->setText( i18n("Hide Details")); + mTbN->show(); + mTbN->raise(); + } + return; + } + mTbN = new KDialogBase( this, "", false /*not modal*/, mInc2lab->text(), KDialogBase::Ok ); + mTbN->setEscapeButton( KDialogBase::Ok ); + connect( mTbN, TQT_SIGNAL( okClicked() ), this, TQT_SLOT( detailsDialogClosed() ) ); + TQTextBrowser* textBrowser = new TQTextBrowser( mTbN ); + mTbN->setMainWidget( textBrowser ); + textBrowser->setText( KCal::IncidenceFormatter::extensiveDisplayString( mInc2 ) ); + mTbN->setMinimumSize( 400, 400 ); + mShowDetails2->setText( i18n("Hide Details")); + mTbN->show(); + mTbN->raise(); +} + +void KIncidenceChooser::takeIncidence1() +{ + mSelIncidence = mInc1; + TQDialog::accept(); +} + +void KIncidenceChooser::takeIncidence2() +{ + mSelIncidence = mInc2; + TQDialog::accept(); +} + +void KIncidenceChooser::takeBoth() +{ + + mSelIncidence = 0; + TQDialog::accept(); +} + + +#include "kincidencechooser.moc" diff --git a/libtdepim/kincidencechooser.h b/libtdepim/kincidencechooser.h new file mode 100644 index 000000000..0d8107f05 --- /dev/null +++ b/libtdepim/kincidencechooser.h @@ -0,0 +1,84 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Lutz Rogowski <rogowski@kde.org> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + As a special exception, permission is given to link this program + with any edition of TQt, and distribute the resulting executable, + without including the source code for TQt in the source distribution. +*/ +#ifndef _KINCIDENCECHOOSER_H +#define _KINCIDENCECHOOSER_H + +#include "calendardiffalgo.h" +#include "htmldiffalgodisplay.h" + +#include <kdialogbase.h> + +namespace KCal { + class Incidence; +} +using namespace KCal; + +class TQButtonGroup; + +/** Dialog to change the korganizer configuration. + */ + +class KDE_EXPORT KIncidenceChooser : public KDialog +{ + Q_OBJECT + TQ_OBJECT + public: + enum mode { + local, remote, newest, ask, both + }; + /** Initialize dialog and pages */ + KIncidenceChooser( TQWidget *parent=0, char *name=0 ); + ~KIncidenceChooser(); + //void setChooseText( TQString ); + void setIncidence( KCal::Incidence *, KCal::Incidence * ); + KCal::Incidence *getIncidence(); + static int chooseMode; + + public slots: + void useGlobalMode(); + + protected slots: + void showIncidence1(); + void showIncidence2(); + void showDiff(); + void takeIncidence1(); + void takeIncidence2(); + void takeBoth(); + void setLabels(); + void setSyncMode(); + void detailsDialogClosed(); + + private: + KPIM::HTMLDiffAlgoDisplay *mDisplayDiff; + KPIM::CalendarDiffAlgo *diff; + KDialogBase *mTbL, *mTbN; + KCal::Incidence *mSelIncidence; + KCal::Incidence *mInc1, *mInc2; + TQButtonGroup *mBg; + TQPushButton *mDiffBut,*mShowDetails1,*mShowDetails2; + TQLabel *mInc1lab, *mInc2lab,* mInc1Sumlab, *mInc2Sumlab,*mMod1lab,*mMod2lab; + +}; + +#endif diff --git a/libtdepim/kmailcompletion.cpp b/libtdepim/kmailcompletion.cpp new file mode 100644 index 000000000..d7fcc8ce4 --- /dev/null +++ b/libtdepim/kmailcompletion.cpp @@ -0,0 +1,103 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2006 Christian Schaarschmidt <schaarsc@gmx.de> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "kmailcompletion.h" +#include <kdebug.h> + +#include <tqregexp.h> + +using namespace KPIM; + +KMailCompletion::KMailCompletion() +{ + setIgnoreCase( true ); +} + +void KMailCompletion::clear() +{ + m_keyMap.clear(); + KCompletion::clear(); +} + +TQString KMailCompletion::makeCompletion( const TQString &string ) +{ + TQString match = KCompletion::makeCompletion( string ); + + // this should be in postProcessMatch, but postProcessMatch is const and will not allow nextMatch + if ( !match.isEmpty() ){ + const TQString firstMatch( match ); + while ( match.find( TQRegExp( "(@)|(<.*>)" ) ) == -1 ) { + /* local email do not require @domain part, if match is an address we'll find + * last+first <match> in m_keyMap and we'll know that match is already a valid email. + * + * Distribution list do not have last+first <match> entry, they will be in mailAddr + */ + const TQStringList &mailAddr = m_keyMap[ match ]; //get all mailAddr for this keyword + bool isEmail = false; + for ( TQStringList::ConstIterator sit ( mailAddr.begin() ), sEnd( mailAddr.end() ); sit != sEnd; ++sit ) + if ( (*sit).find( "<" + match + ">" ) != -1 || (*sit) == match ) { + isEmail = true; + break; + } + + if ( !isEmail ) { + // match is a keyword, skip it and try to find match <email@domain> + match = nextMatch(); + if ( firstMatch == match ){ + match = TQString(); + break; + } + } else + break; + } + } + return match; +} + +void KMailCompletion::addItemWithKeys( const TQString& email, int weight, const TQStringList* keyWords) +{ + Q_ASSERT( keyWords != 0 ); + for ( TQStringList::ConstIterator it( keyWords->begin() ); it != keyWords->end(); ++it ) { + TQStringList &emailList = m_keyMap[ (*it) ]; //lookup email-list for given keyword + if ( emailList.find( email ) == emailList.end() ) //add email if not there + emailList.append( email ); + addItem( (*it),weight ); //inform KCompletion about keyword + } +} + +void KMailCompletion::postProcessMatches( TQStringList * pMatches )const +{ + Q_ASSERT( pMatches != 0 ); + if ( pMatches->isEmpty() ) + return; + + //KCompletion has found the keywords for us, we can now map them to mail-addr + TQMap< TQString, bool > mailAddrDistinct; //TODO replace with TQSet in KDE4 + for ( TQStringList::ConstIterator sit ( pMatches->begin() ), sEnd( pMatches->end() ); sit != sEnd; ++sit ) { + const TQStringList &mailAddr = m_keyMap[ (*sit) ]; //get all mailAddr for this keyword + for ( TQStringList::ConstIterator sit ( mailAddr.begin() ), sEnd( mailAddr.end() ); sit != sEnd; ++sit ) { + mailAddrDistinct[ (*sit) ] = true; //store mailAddr, TQMap will make them unique + } + } + pMatches->clear(); //delete keywords + (*pMatches) += mailAddrDistinct.keys(); //add emailAddr +} +#include "kmailcompletion.moc" diff --git a/libtdepim/kmailcompletion.h b/libtdepim/kmailcompletion.h new file mode 100644 index 000000000..9251c2797 --- /dev/null +++ b/libtdepim/kmailcompletion.h @@ -0,0 +1,79 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2006 Christian Schaarschmidt <schaarsc@gmx.de> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef KPIM_KMAILCOMPLETION_H +#define KPIM_KMAILCOMPLETION_H + +#include <tqmap.h> +#include <tqstringlist.h> +#include "kcompletion.h" + + +namespace KPIM { + +/** + * KMailCompletion allows lookup of email addresses by keyword. + * Typically a keywods would be firstname, lastname, nickname or domain. + */ +class KMailCompletion : public KCompletion +{ + Q_OBJECT + TQ_OBJECT + + public: + KMailCompletion(); + + /** + * Clears internal keyword map and calls KCompletion::clear. + */ + virtual void clear(); + + /** + * Uses KCompletion::makeCompletion to find email addresses which starts with string. + * ignores keywords. + * + * @returns email address + */ + TQString makeCompletion( const TQString &string ); + + /** + * Specify keywords for email. + * + * Items may be added with KCompletion::addItem, those will only be returned as match if they + * are in one of these formats: + * \li contains localpart@domain + * \li contains <email> + * or if they have also been added with this function. + */ + void addItemWithKeys( const TQString& email, int weight, const TQStringList * keyWords); + + /** + * Uses an internal map to replace all keywords in pMatches whith corrsesponding email addresses. + */ + virtual void postProcessMatches( TQStringList * pMatches )const; + + private: + TQMap< TQString, TQStringList > m_keyMap; +}; + +} + +#endif diff --git a/libtdepim/komposer/Makefile.am b/libtdepim/komposer/Makefile.am new file mode 100644 index 000000000..13509925e --- /dev/null +++ b/libtdepim/komposer/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = core plugins test diff --git a/libtdepim/komposer/core/Makefile.am b/libtdepim/komposer/core/Makefile.am new file mode 100644 index 000000000..b494e7151 --- /dev/null +++ b/libtdepim/komposer/core/Makefile.am @@ -0,0 +1,28 @@ +AM_CPPFLAGS = -I$(top_builddir)/libtdepim $(all_includes) +METASOURCES = AUTO + +lib_LTLIBRARIES = libkomposer.la + +libkomposer_la_SOURCES = plugin.cpp editor.cpp core.cpp attachment.cpp corewidget.cpp \ + settings.kcfgc pluginmanager.cpp komposerIface.skel +libkomposer_la_LDFLAGS = -no-undefined $(KDE_RPATH) -version-info 1:0:0 $(all_libraries) +libkomposer_la_LIBADD = $(LIB_KPARTS) $(top_builddir)/libtdepim/libtdepim.la -lkutils + +#kde_module_LTLIBRARIES = kcm_komposer.la + +#kcm_komposer_la_SOURCES = prefsmodule.cpp +#kcm_komposer_la_LDFLAGS = -module -avoid-version -no-undefined $(all_libraries) +#kcm_komposer_la_LIBADD = libkomposer.la $(top_builddir)/libtdepim/libtdepim.la $(LIB_KDECORE) + +kpincludedir = $(includedir)/komposer +kpinclude_HEADERS = plugin.h editor.h core.h + +rcdir = $(kde_datadir)/komposer +rc_DATA = komposerui.rc + +kde_kcfg_DATA = komposer.kcfg + +servicetypedir = $(kde_servicetypesdir) +servicetype_DATA = komposerplugin.desktop komposereditor.desktop + +kde_services_DATA = komposerconfig.desktop diff --git a/libtdepim/komposer/core/attachment.cpp b/libtdepim/komposer/core/attachment.cpp new file mode 100644 index 000000000..df803de60 --- /dev/null +++ b/libtdepim/komposer/core/attachment.cpp @@ -0,0 +1,112 @@ +// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*- +/** + * attachment.cpp + * + * Copyright (C) 2003 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#include "attachment.h" + +using namespace Komposer; + +class Attachment::Private +{ +public: + TQString name; + TQCString cte; + TQByteArray data; + TQCString type; + TQCString subType; + TQCString paramAttr; + TQString paramValue; + TQCString contDisp; +}; + +Attachment::Attachment( const TQString &name, + const TQCString &cte, + const TQByteArray &data, + const TQCString &type, + const TQCString &subType, + const TQCString ¶mAttr, + const TQString ¶mValue, + const TQCString &contDisp ) + : d( new Private ) +{ + d->name = name; + d->cte = cte; + d->data = data; + d->type = type; + d->subType = subType; + d->paramAttr = paramAttr; + d->paramValue = paramValue; + d->contDisp = contDisp; +} + +Attachment::~Attachment() +{ + delete d; d = 0; +} + +TQString +Attachment::name() const +{ + return d->name; +} + +TQCString +Attachment::cte() const +{ + return d->cte; +} + +TQByteArray +Attachment::data() const +{ + return d->data; +} + +TQCString +Attachment::type() const +{ + return d->type; +} + + +TQCString +Attachment::subType() const +{ + return d->subType; +} + +TQCString +Attachment::paramAttr() const +{ + return d->paramAttr; +} + +TQString +Attachment::paramValue() const +{ + return d->paramValue; +} + +TQCString +Attachment::contentDisposition() const +{ + return d->contDisp; +} + diff --git a/libtdepim/komposer/core/attachment.h b/libtdepim/komposer/core/attachment.h new file mode 100644 index 000000000..5c4e1ebf2 --- /dev/null +++ b/libtdepim/komposer/core/attachment.h @@ -0,0 +1,61 @@ +// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*- +/** + * attachment.h + * + * Copyright (C) 2003 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#ifndef KOMPOSER_ATTACHMENT_H +#define KOMPOSER_ATTACHMENT_H + +#include <tqstring.h> +#include <tqcstring.h> +#include <tqvaluelist.h> + +namespace Komposer +{ + + class Attachment + { + public: + Attachment( const TQString &name, + const TQCString &cte, + const TQByteArray &data, + const TQCString &type, + const TQCString &subType, + const TQCString ¶mAttr, + const TQString ¶mValue, + const TQCString &contDisp ); + ~Attachment(); + + TQString name() const; + TQCString cte() const; + TQByteArray data() const; + TQCString type() const; + TQCString subType() const; + TQCString paramAttr() const; + TQString paramValue() const; + TQCString contentDisposition() const; + + private: + class Private; + Private *d; + }; + typedef TQValueList<Attachment> AttachmentList; +} + +#endif diff --git a/libtdepim/komposer/core/core.cpp b/libtdepim/komposer/core/core.cpp new file mode 100644 index 000000000..f93e59cc9 --- /dev/null +++ b/libtdepim/komposer/core/core.cpp @@ -0,0 +1,357 @@ +// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*- +/** + * core.cpp + * + * Copyright (C) 2003 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include "core.h" + +#include "pluginmanager.h" +#include "editor.h" +#include "plugin.h" + +#include <ksettings/dialog.h> +#include <kplugininfo.h> +#include <kapplication.h> +#include <kconfig.h> +#include <ktrader.h> +#include <klibloader.h> +#include <kstdaction.h> +#include <klistbox.h> +#include <kiconloader.h> +#include <kstandarddirs.h> +#include <kshortcut.h> +#include <klocale.h> +#include <kstatusbar.h> +#include <kguiitem.h> +#include <kpopupmenu.h> +#include <kshortcut.h> +#include <kcmultidialog.h> +#include <kaction.h> +#include <kstdaccel.h> +#include <kdebug.h> + +#include <tqwidgetstack.h> +#include <tqhbox.h> +#include <tqwidget.h> + +using namespace Komposer; + +Core::Core( TQWidget *parent, const char *name ) + : KomposerIface( "KomposerIface" ), + KMainWindow( parent, name ), m_currentEditor( 0 ) +{ + initWidgets(); + initCore(); + initConnections(); + setInstance( new KInstance( "komposer" ) ); + + createActions(); + setXMLFile( "komposerui.rc" ); + + createGUI( 0 ); + + resize( 600, 400 ); // initial size + setAutoSaveSettings(); + + loadSettings(); +} + +Core::~Core() +{ + saveSettings(); + + //Prefs::self()->writeConfig(); +} + +void +Core::addEditor( Komposer::Editor *editor ) +{ + if ( editor->widget() ) { + m_stack->addWidget( editor->widget() ); + m_stack->raiseWidget( editor->widget() ); + editor->widget()->show(); + m_currentEditor = editor; + } + + // merge the editors GUI into the main window + //insertChildClient( editor ); + guiFactory()->addClient( editor ); +} + +void +Core::addPlugin( Komposer::Plugin *plugin ) +{ + //insertChildClient( plugin ); + guiFactory()->addClient( plugin ); +} + +void +Core::slotPluginLoaded( Plugin *plugin ) +{ + kdDebug() << "Plugin loaded "<<endl; + + Editor *editor = dynamic_cast<Editor*>( plugin ); + if ( editor ) { + addEditor( editor ); + } else { + addPlugin( plugin ); + } +} + +void +Core::slotAllPluginsLoaded() +{ + TQValueList<KPluginInfo*> plugins = m_pluginManager->availablePlugins(); + kdDebug()<<"Number of available plugins is "<< plugins.count() <<endl; + for ( TQValueList<KPluginInfo*>::iterator it = plugins.begin(); it != plugins.end(); ++it ) { + KPluginInfo *i = ( *it ); + kdDebug()<<"\tAvailable plugin "<< i->pluginName() + <<", comment = "<< i->comment() <<endl; + } + + if ( !m_stack->visibleWidget() ) { + m_pluginManager->loadPlugin( "komposer_defaulteditor", PluginManager::LoadAsync ); + } +} + +#if 0 +void +Core::slotActivePartChanged( KParts::Part *part ) +{ + if ( !part ) { + createGUI( 0 ); + return; + } + + kdDebug() << "Part activated: " << part << " with stack id. " + << m_stack->id( part->widget() )<< endl; + + createGUI( part ); +} + +void +Core::selectEditor( Komposer::Editor *editor ) +{ + if ( !editor ) + return; + + KParts::Part *part = editor->part(); + + editor->select(); + + TQPtrList<KParts::Part> *partList = const_cast<TQPtrList<KParts::Part>*>( + m_partManager->parts() ); + if ( partList->find( part ) == -1 ) + addPart( part ); + + m_partManager->setActivePart( part ); + TQWidget *view = part->widget(); + Q_ASSERT( view ); + + kdDebug()<<"Raising view "<<view<<endl; + if ( view ) + { + m_stack->raiseWidget( view ); + view->show(); + view->setFocus(); + m_currentEditor = editor; + } +} + +void +Core::selectEditor( const TQString &editorName ) +{ + +} +#endif + +void +Core::loadSettings() +{ + //kdDebug()<<"Trying to select "<< Prefs::self()->m_activeEditor <<endl; + //selectEditor( Prefs::self()->m_activeEditor ); + + //m_activeEditors = Prefs::self()->m_activeEditors; +} + +void +Core::saveSettings() +{ + //if ( m_currentEditor ) + //Prefs::self()->m_activeEditor = m_currentEditor->identifier(); +} + +void +Core::slotQuit() +{ + kdDebug()<<"exit"<<endl; + m_pluginManager->shutdown(); +} + +void +Core::slotPreferences() +{ + if ( m_dlg == 0 ) + m_dlg = new KSettings::Dialog( this ); + m_dlg->show(); +} + +void +Core::initWidgets() +{ + statusBar()->show(); + TQHBox *topWidget = new TQHBox( this ); + setCentralWidget( topWidget ); + m_stack = new TQWidgetStack( topWidget ); +} + +void +Core::initCore() +{ + m_pluginManager = new PluginManager( this ); + connect( m_pluginManager, TQT_SIGNAL(pluginLoaded(Plugin*)), + TQT_SLOT(slotPluginLoaded(Plugin*)) ); + connect( m_pluginManager, TQT_SIGNAL(allPluginsLoaded()), + TQT_SLOT(slotAllPluginsLoaded()) ); + + + m_pluginManager->loadAllPlugins(); + kdDebug()<<"Loading"<<endl; +} + +void +Core::initConnections() +{ + connect( kapp, TQT_SIGNAL(shutDown()), + TQT_SLOT(slotQuit()) ); +} + +void +Core::createActions() +{ + KStdAction::close( this, TQT_SLOT(slotClose()), actionCollection() ); + + (void) new KAction( i18n( "&Send" ), "mail_send", CTRL+Key_Return, + this, TQT_SLOT(slotSendNow()), actionCollection(), + "send_default" ); + + (void) new KAction( i18n( "&Queue" ), "queue", 0, + this, TQT_SLOT(slotSendLater()), + actionCollection(), "send_alternative" ); + + (void) new KAction( i18n( "Save in &Drafts Folder" ), "filesave", 0, + this, TQT_SLOT(slotSaveDraft()), + actionCollection(), "save_in_drafts" ); + (void) new KAction( i18n( "&Insert File..." ), "fileopen", 0, + this, TQT_SLOT(slotInsertFile()), + actionCollection(), "insert_file" ); + (void) new KAction( i18n( "&Address Book" ), "contents",0, + this, TQT_SLOT(slotAddrBook()), + actionCollection(), "addressbook" ); + (void) new KAction( i18n( "&New Composer" ), "mail_new", + KStdAccel::shortcut( KStdAccel::New ), + this, TQT_SLOT(slotNewComposer()), + actionCollection(), "new_composer" ); + + (void) new KAction( i18n( "&Attach File..." ), "attach", + 0, this, TQT_SLOT(slotAttachFile()), + actionCollection(), "attach_file" ); +} + +void +Core::slotClose() +{ + close( false ); +} + +void +Core::slotSendNow() +{ + +} + +void +Core::slotSendLater() +{ + +} + +void +Core::slotSaveDraft() +{ + +} + +void +Core::slotInsertFile() +{ + +} + +void +Core::slotAddrBook() +{ + +} + +void +Core::slotNewComposer() +{ + +} + +void +Core::slotAttachFile() +{ + +} + +void +Core::send( int how ) +{ + +} + +void +Core::addAttachment( const KURL &url, const TQString &comment ) +{ + +} + +void +Core::setBody( const TQString &body ) +{ + m_currentEditor->setText( body ); +} + +void +Core::addAttachment( const TQString &name, + const TQCString &cte, + const TQByteArray &data, + const TQCString &type, + const TQCString &subType, + const TQCString ¶mAttr, + const TQString ¶mValue, + const TQCString &contDisp ) +{ + +} + +#include "core.moc" diff --git a/libtdepim/komposer/core/core.h b/libtdepim/komposer/core/core.h new file mode 100644 index 000000000..91487572a --- /dev/null +++ b/libtdepim/komposer/core/core.h @@ -0,0 +1,108 @@ +// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*- +/** + * core.h + * + * Copyright (C) 2003 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#ifndef KOMPOSER_CORE_H +#define KOMPOSER_CORE_H + +#include "komposerIface.h" + +#include <kmainwindow.h> +#include <tqptrlist.h> + +namespace KSettings { + class Dialog; +} +class TQWidgetStack; + +namespace Komposer +{ + + class Editor; + class Plugin; + class PluginManager; + + /** + * This class provides the interface to the Komposer core for the editor. + */ + class Core : public KMainWindow, virtual public KomposerIface + { + Q_OBJECT + TQ_OBJECT + public: + Core( TQWidget *parentWidget = 0, const char *name = 0 ); + virtual ~Core(); + + public slots: + virtual void send( int how ); + virtual void addAttachment( const KURL &url, const TQString &comment ); + virtual void setBody( const TQString &body ); + virtual void addAttachment( const TQString &name, + const TQCString &cte, + const TQByteArray &data, + const TQCString &type, + const TQCString &subType, + const TQCString ¶mAttr, + const TQString ¶mValue, + const TQCString &contDisp ); + + + + protected slots: + //void slotActivePartChanged( KParts::Part *part ); + void slotPluginLoaded( Plugin* ); + void slotAllPluginsLoaded(); + void slotPreferences(); + void slotQuit(); + void slotClose(); + + void slotSendNow(); + void slotSendLater(); + void slotSaveDraft(); + void slotInsertFile(); + void slotAddrBook(); + void slotNewComposer(); + void slotAttachFile(); + + protected: + virtual void initWidgets(); + void initCore(); + void initConnections(); + void loadSettings(); + void saveSettings(); + void createActions(); + + void addEditor( Komposer::Editor *editor ); + void addPlugin( Komposer::Plugin *plugin ); + + private: + TQWidgetStack *m_stack; + Editor *m_currentEditor; + PluginManager *m_pluginManager; + + KSettings::Dialog *m_dlg; + + class Private; + Private *d; +}; + +} + +#endif diff --git a/libtdepim/komposer/core/corewidget.cpp b/libtdepim/komposer/core/corewidget.cpp new file mode 100644 index 000000000..c65893766 --- /dev/null +++ b/libtdepim/komposer/core/corewidget.cpp @@ -0,0 +1,32 @@ +// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*- +/** + * corewidget.cpp + * + * Copyright (C) 2003 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include "corewidget.h" + +using namespace Komposer; + +CoreWidget::CoreWidget( TQWidget *parent, const char *name ) + : TQWidget( parent, name ) +{ +} + +#include "corewidget.moc" diff --git a/libtdepim/komposer/core/corewidget.h b/libtdepim/komposer/core/corewidget.h new file mode 100644 index 000000000..7d2acc6db --- /dev/null +++ b/libtdepim/komposer/core/corewidget.h @@ -0,0 +1,49 @@ +// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*- +/** + * corewidget.h + * + * Copyright (C) 2003-2004 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#ifndef COREWIDGET_H +#define COREWIDGET_H + +#include "attachment.h" + +#include <tqwidget.h> + +namespace Komposer +{ + + class CoreWidget : public TQWidget + { + Q_OBJECT + TQ_OBJECT + public: + CoreWidget( TQWidget *parent, const char *name=0 ); + + virtual TQString subject() const =0; + virtual TQStringList to() const =0; + virtual TQStringList cc() const =0; + virtual TQStringList bcc() const =0; + virtual TQString from() const =0; + virtual TQString replyTo() const =0; + virtual AttachmentList attachments() const =0; + }; +} + +#endif diff --git a/libtdepim/komposer/core/editor.cpp b/libtdepim/komposer/core/editor.cpp new file mode 100644 index 000000000..5dada393f --- /dev/null +++ b/libtdepim/komposer/core/editor.cpp @@ -0,0 +1,51 @@ +// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*- +/** + * editor.cpp + * + * Copyright (C) 2003 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include "editor.h" +#include "core.h" + +namespace Komposer { + +class Editor::Private { +public: + TQString id; +}; + +Editor::Editor( TQObject *parent, const char *name, const TQStringList &args ) + : Plugin( parent, name, args ), d( new Private ) +{ +} + +Editor::~Editor() +{ + delete d; d = 0; +} + +void +Editor::select() +{ +} + + +} + +#include "editor.moc" diff --git a/libtdepim/komposer/core/editor.h b/libtdepim/komposer/core/editor.h new file mode 100644 index 000000000..cdc05710d --- /dev/null +++ b/libtdepim/komposer/core/editor.h @@ -0,0 +1,105 @@ +// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*- +/** + * editor.h + * + * Copyright (C) 2003-2004 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef KOMPOSER_EDITOR_H +#define KOMPOSER_EDITOR_H + +#include "plugin.h" + +#include <tqstringlist.h> + +namespace KParts { + class Part; +} + +namespace Komposer { + + class Core; + + class Editor : public Plugin + { + Q_OBJECT + TQ_OBJECT + public: + enum TextType { + Plain = 1 << 0, + RichText = 1 << 1, + HTML = 1 << 2 + }; + virtual ~Editor(); + + /** + * This is the magic function that all derivatives have to reimplement. + * It returns the actual editor component. + */ + virtual TQWidget *widget() =0; + + int supportedTextFormats() const; + + /** + * Returns the full text inside the editor. + */ + virtual TQString text() const =0; + + /** + * This function is called when the plugin is selected by the user before the + * widget of the KPart belonging to the plugin is raised. + */ + virtual void select(); + + /** + * Reimplement this method and return a @ref TQStringList of all config + * modules your application part should offer via Komposer. Note that the + * part and the module will have to take care for config syncing themselves. + * Usually @p DCOP used for that purpose. + * + * @note Make sure you offer the modules in the form: + * <code>"pathrelativetosettings/mysettings.desktop"</code> + */ + virtual TQStringList configModules() const { return TQStringList(); } + + + public slots: + /** + * Sets the text of the opened editor. + * Most commonly used on replaying. + * If any text is present if will be deleted. + */ + virtual void setText( const TQString &txt ) =0; + + /** + * Changes currently used signature. If no signature is present + * a new one should be appened. + */ + virtual void changeSignature( const TQString &txt ) =0; + + protected: + Editor( TQObject *parent, const char *name, const TQStringList &args ); + + private: + class Private; + Private *d; + }; + +} + +#endif diff --git a/libtdepim/komposer/core/komposer.kcfg b/libtdepim/komposer/core/komposer.kcfg new file mode 100644 index 000000000..111584b88 --- /dev/null +++ b/libtdepim/komposer/core/komposer.kcfg @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> +<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0 + http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" > + <kcfgfile name="komposerrc"/> + + <group name="Colors"> + + <entry type="Color" name="foregroundColor"> + </entry> + + <entry type="Color" name="backgroundColor"> + </entry> + + <entry type="Color" name="QuotedText$(Level)" key="quotedColor_$(Level)"> + <parameter name="Level" type="Int" max="3"/> + <default param="0">#ff0000</default> + <default param="1">#00ff00</default> + <default param="2">#0000ff</default> + <default param="3">#ffff00</default> + </entry> + + </group> + +</kcfg> diff --git a/libtdepim/komposer/core/komposerIface.h b/libtdepim/komposer/core/komposerIface.h new file mode 100644 index 000000000..eecab2f3a --- /dev/null +++ b/libtdepim/komposer/core/komposerIface.h @@ -0,0 +1,85 @@ +/* + * komposerIface.h + * + * Copyright (C) 2004 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#ifndef KOMPOSERIFACE_H +#define KOMPOSERIFACE_H + +#include <dcopobject.h> +#include <kurl.h> + +namespace Komposer +{ + +/** + DCOP interface for mail composer window. The address header fields are set, + when the composer is constructed. KMailIface::openComposer() returns a + reference to the DCOP interface of the new composer window, which provides the + functions defined in the MailComposerIface. +*/ +class KomposerIface : virtual public DCOPObject +{ + K_DCOP +k_dcop: + /** + Send message. + + @param how 0 for deafult method, 1 for sending now, 2 for sending later. + */ + virtual void send(int how) = 0; + + /** + Add url as attachment with a user-defined comment. + */ + virtual void addAttachment( const KURL &url, const TQString &comment) = 0; + + /** + Set message body. + */ + virtual void setBody( const TQString &body ) = 0; + + /** + Add attachment. + + @param name Name of Attachment + @param cte Content Transfer Encoding + @param data Data to be attached + @param type MIME content type + @param subType MIME content sub type + @param paramAttr Attribute name of parameter of content type + @param paramValue Value of parameter of content type + @param contDisp Content disposition + */ + virtual void addAttachment( const TQString &name, + const TQCString &cte, + const TQByteArray &data, + const TQCString &type, + const TQCString &subType, + const TQCString ¶mAttr, + const TQString ¶mValue, + const TQCString &contDisp ) = 0; +public: + KomposerIface( const char *name ) + : DCOPObject( name ) + {} +}; + +} + +#endif diff --git a/libtdepim/komposer/core/komposerconfig.desktop b/libtdepim/komposer/core/komposerconfig.desktop new file mode 100644 index 000000000..58c8bd765 --- /dev/null +++ b/libtdepim/komposer/core/komposerconfig.desktop @@ -0,0 +1,56 @@ +[Desktop Entry] +Exec=kcmshell komposerconfig +Icon=komposer +Type=Application +Terminal=false + +X-KDE-ModuleType=Library +X-KDE-Library=komposer +X-KDE-FactoryName=komposerconfig +X-KDE-HasReadOnlyMode=false + +Name=Komposer +Name[cy]=Kyfansoddydd +Name[da]=Brevskriver +Name[hi]=कमà¥à¤ªà¥‹à¤œà¤¼à¤° +Name[ms]=Penggubah +Name[ne]=कमà¥à¤ªà¥‹à¤œà¤° +Name[pt]=Kompositor +Name[ta]=கமà¯à®ªà¯‹à®šà®°à¯ +Comment=KDE Komposer +Comment[bg]=ПиÑане на форматиран текÑÑ‚ +Comment[br]=Komposer KDE +Comment[ca]=Compositor per a KDE +Comment[cy]=Cyfansoddydd KDE +Comment[da]=KDE Brevskriver +Comment[de]=KDE-Komposer +Comment[fr]=Komposer KDE +Comment[ga]=Komposer KDE +Comment[hi]=केडीई कमà¥à¤ªà¥‹à¤œà¤¼à¤° +Comment[ms]=Penggubah KDE +Comment[nds]=Nettbreef-Editor vun KDE +Comment[ne]=केडीई कमà¥à¤ªà¥‹à¤œà¤° +Comment[nn]=KDE, tekstredigerar for e-post +Comment[pl]=Komposer dla KDE +Comment[pt]=Kompositor do KDE +Comment[pt_BR]=Konposer do KDE +Comment[ru]=Композитор KDE +Comment[ta]=கேடிஇ கமà¯à®ªà¯‹à®šà®°à¯ +Comment[tg]=Композитори KDE +Keywords=komposer +Keywords[bg]=редактор, ÑÑŠÑтавител, форматиране, текÑÑ‚, komposer +Keywords[ca]=compositor +Keywords[cy]=komposer,kyfansoddydd +Keywords[da]=brevskriver +Keywords[de]=Komposer +Keywords[fy]=komposer,opstellen, opsteller +Keywords[hi]=कमà¥à¤ªà¥‹à¤œà¤¼à¤° +Keywords[nds]=Komposer +Keywords[ne]=कमà¥à¤ªà¥‹à¤œà¤° +Keywords[nl]=komposer,opstellen +Keywords[nn]=komposer,tekstredigerar,epost +Keywords[ru]=komposer,ноты +Keywords[sr]=komposer,ÑаÑтављач +Keywords[sr@Latn]=komposer,sastavljaÄ +Keywords[ta]=கமà¯à®ªà¯‹à®šà®°à¯ +Keywords[tg]=komposer,нотаҳо diff --git a/libtdepim/komposer/core/komposereditor.desktop b/libtdepim/komposer/core/komposereditor.desktop new file mode 100644 index 000000000..fd1c1341e --- /dev/null +++ b/libtdepim/komposer/core/komposereditor.desktop @@ -0,0 +1,56 @@ +[Desktop Entry] +Type=ServiceType +X-KDE-ServiceType=Komposer/Editor +X-KDE-Derived=Komposer/Plugin +Comment=Komposer Editor +Comment[af]=Komposer Redigeerder +Comment[bg]=ПиÑане на форматиран текÑÑ‚ +Comment[br]=Aozer Komposer +Comment[bs]=Komposer editor +Comment[ca]=Compositor per a KDE +Comment[cs]=Komposer editor +Comment[da]=Komposer editor +Comment[el]=ΕπεξεÏγαστής Komposer +Comment[eo]=Komposer-redaktilo +Comment[es]=Editor Komposer +Comment[et]=Komposeri redaktor +Comment[eu]=Komposer editorea +Comment[fa]=ویرایشگر Komposer +Comment[fi]=Komposer-muokkain +Comment[fr]=Éditeur Komposer +Comment[fy]=Komposer-bewurker +Comment[ga]=Eagarthóir Komposer +Comment[gl]=Editor Komposer +Comment[he]=עורך של Komposer +Comment[hu]=Komposer +Comment[is]=Komposer ritill +Comment[it]=Komposer editor +Comment[ja]=Komposer,エディタ +Comment[kk]=Komposer өңдегіші +Comment[km]=កម្មវិធី​និពន្ធ Komposer +Comment[ko]=Komposer 편집기 +Comment[lt]=Komposer redaktorius +Comment[ms]=Editor Komposer +Comment[nb]=Komposer redigerer +Comment[nds]=Komposer-Editor +Comment[ne]=कमà¥à¤ªà¥‹à¤œà¤° समà¥à¤ªà¤¾à¤¦à¤• +Comment[nl]=Komposer-editor +Comment[nn]=Komposer redigerar +Comment[pl]=Edytor Komposer +Comment[pt]=Editor Kompositor +Comment[pt_BR]=Editor de Mensagens +Comment[ru]=Редактор Komposer +Comment[sk]=Editor Komposer +Comment[sl]=Urejevalnik Komposer +Comment[sr]=Уређивач Komposer +Comment[sr@Latn]=UreÄ‘ivaÄ Komposer +Comment[sv]=Komposer editor +Comment[ta]=கமà¯à®ªà¯‹à®šà®°à¯ தொகà¯à®ªà¯à®ªà®¾à®©à¯ +Comment[tg]=Муҳаррири Komposer +Comment[tr]=Komposer Düzenleyicisi +Comment[uk]=Редактор Komposer +Comment[zh_CN]=Komposer 编辑器 +Comment[zh_TW]=Komposer 編輯器 + +[PropertyDef::X-Komposer-Weight] +Type=int diff --git a/libtdepim/komposer/core/komposerplugin.desktop b/libtdepim/komposer/core/komposerplugin.desktop new file mode 100644 index 000000000..b398e1c67 --- /dev/null +++ b/libtdepim/komposer/core/komposerplugin.desktop @@ -0,0 +1,59 @@ +[Desktop Entry] +Type=ServiceType +X-KDE-ServiceType=Komposer/Plugin +X-KDE-Derived=KPluginInfo +Name=Komposer Plugin +Name[af]=Komposer inprop module +Name[bg]=ПриÑтавка за Komposer +Name[br]=Lugent Komposer +Name[bs]=Komposer dodatak +Name[ca]=Endollable Komposer +Name[cs]=Komposer modul +Name[cy]=Ategyn Kyfansoddydd +Name[da]=Brevskriver-plugin +Name[de]=Komposer-Modul +Name[el]=Î Ïόσθετο του Komposer +Name[eo]=Komposer-kromaĵo +Name[es]=Accesorio Komposer +Name[et]=Komposeri plugin +Name[eu]=Komposer plugin-a +Name[fa]=وصلۀ Komposer +Name[fi]=Komposer-liitännäinen +Name[fr]=Module de Komposer +Name[fy]=Komposer-plugin +Name[ga]=Breiseán Komposer +Name[gl]=Extensión Komposer +Name[he]=תןסף Kompoer +Name[hi]=कमà¥à¤ªà¥‹à¤œà¤¼à¤° पà¥à¤²à¤—इन +Name[hu]=Komposer bÅ‘vÃtÅ‘modul +Name[is]=Komposer Ãforrit +Name[it]=Plugin Komposer +Name[ja]=Komposer プラグイン +Name[kk]=Komposer плагин модулі +Name[km]=កម្មវិធី​ជំនួយ Komposer +Name[ko]=Komposer í”ŒëŸ¬ê·¸ì¸ +Name[lt]=Komposer priedas +Name[ms]=Plugin Komposer +Name[nb]=Komposer-programtillegg +Name[nds]=Komposer-Moduul +Name[ne]=कमà¥à¤ªà¥‹à¤œà¤° पà¥à¤²à¤—इन +Name[nn]=Komposer-programtillegg +Name[pl]=Wtyczka Komposer +Name[pt]='Plugin' do Kompositor +Name[pt_BR]=Plug-in do Komposer +Name[ro]=Modul Komposer +Name[ru]=Модуль Komposer +Name[sk]=Modul Komposer +Name[sl]=Vstavek za Komposer +Name[sr]=Прикључак Komposer-а +Name[sr@Latn]=PrikljuÄak Komposer-a +Name[sv]=Komposer-insticksprogram +Name[ta]=கமà¯à®ªà¯‹à®šà®°à¯ சொரà¯à®•à¯à®ªà¯à®ªà¯Šà®°à¯à®³à¯ +Name[tg]=Модули Komposer +Name[tr]=Komposer Eklentisi +Name[uk]=Втулок Komposer +Name[zh_CN]=Komposer æ’件 +Name[zh_TW]=Komposer å¤–æŽ›ç¨‹å¼ + +[PropertyDef::X-Komposer-Version] +Type=int diff --git a/libtdepim/komposer/core/komposerui.rc b/libtdepim/komposer/core/komposerui.rc new file mode 100644 index 000000000..83ec4d299 --- /dev/null +++ b/libtdepim/komposer/core/komposerui.rc @@ -0,0 +1,51 @@ +<!DOCTYPE kpartgui > +<kpartgui version="2" name="komposer" > +<MenuBar> + <Menu name="file" noMerge="1"> + <text>&Message</text> + <Action name="new_composer" /> + <Action name="open_mailreader" /> + <Separator/> + <Action name="send_default" /> + <Action name="send_alternative" /> + <Action name="save_in_drafts" /> + <Separator/> + <Action name="insert_file" /> + <Action name="file_print" /> + <Separator/> + <Merge/> + <Separator/> + <Action name="file_close" /> + </Menu> + <Merge /> + <Menu noMerge="1" name="attach"> + <text>&Attach</text> + <Merge/> + <Separator/> + <Action name="attach_file"/> + <Action name="remove_attachment"/> + <Action name="attachment_properties"/> + </Menu> + <Menu noMerge="1" name="Tools"> + <text>&Tools</text> + <Merge/> + <Action name="addressbook"/> + </Menu> + <Menu noMerge="1" name="settings"> + <text>&Settings</text> + <Merge/> + <Separator/> + <Action name="settings_configure_komposer"/> + </Menu> +</MenuBar> +<ToolBar position="Top" iconText="IconOnly" noMerge="1" name="mainToolBar"><text>Main Toolbar</text> + <Action name="send_default" /> + <Action name="send_alternative" /> + <Separator/> + <Action name="attach_file"/> + <Separator/> + <Merge/> + <Separator/> + <Action name="help_whats_this"/> +</ToolBar> +</kpartgui> diff --git a/libtdepim/komposer/core/plugin.cpp b/libtdepim/komposer/core/plugin.cpp new file mode 100644 index 000000000..41af47cd9 --- /dev/null +++ b/libtdepim/komposer/core/plugin.cpp @@ -0,0 +1,80 @@ +// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*- +/** + * plugin.cpp + * + * Copyright (C) 2003 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#include "plugin.h" + +#include "core.h" + +#include <kdebug.h> +#include <tqstring.h> + +namespace Komposer +{ + +class Plugin::Private +{ +public: + //Core* core; +}; + +Plugin::Plugin( TQObject *parent, const char *name, const TQStringList & ) + : TQObject( parent, name ), d( new Private ) +{ + //d->core = core; +} + +Plugin::~Plugin() +{ + delete d; d = 0; +} + +void +Plugin::startedComposing() +{ +} + +void +Plugin::sendClicked() +{ +} + +void +Plugin::quitClicked() +{ +} + +void +Plugin::aboutToUnload() +{ + kdDebug()<<"plugin unloading"<<endl; + emit readyForUnload(); +} + +Core* +Plugin::core() const +{ + return 0; + //return d->core; +} + +}//end namespace Komposer + +#include "plugin.moc" diff --git a/libtdepim/komposer/core/plugin.h b/libtdepim/komposer/core/plugin.h new file mode 100644 index 000000000..7acd7f70f --- /dev/null +++ b/libtdepim/komposer/core/plugin.h @@ -0,0 +1,76 @@ +// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*- +/** + * plugin.h + * + * Copyright (C) 2003 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#ifndef KOMPOSER_PLUGIN_H +#define KOMPOSER_PLUGIN_H + +#include <tqobject.h> +#include <kxmlguiclient.h> + +namespace Komposer +{ + class Core; + + class Plugin : public TQObject, + virtual public KXMLGUIClient + { + Q_OBJECT + TQ_OBJECT + public: + virtual ~Plugin(); + + signals: + void statusMessage( const TQString & ); + void readyForUnload(); + + protected slots: + /** + * Called when a new message is created. + */ + virtual void startedComposing(); + + /** + * Called after the send button has been pressed + * and before the message has been sent. + */ + virtual void sendClicked(); + + /** + * Called after the quit button has been pressed + */ + virtual void quitClicked(); + + virtual void aboutToUnload(); + + protected: + Core *core() const; + protected: + friend class PluginManager; + Plugin( TQObject *parent, const char *name, const TQStringList& args = TQStringList() ); + + private: + class Private; + Private *d; + }; + +} + +#endif diff --git a/libtdepim/komposer/core/pluginmanager.cpp b/libtdepim/komposer/core/pluginmanager.cpp new file mode 100644 index 000000000..dcfea6f4b --- /dev/null +++ b/libtdepim/komposer/core/pluginmanager.cpp @@ -0,0 +1,489 @@ +// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 2; -*- +/** + * pluginmanager.cpp + * Most of this code has been lifted from Martijn's KopetePluginManager class + * + * Copyright (C) 2004 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#include "pluginmanager.h" + +#include "plugin.h" + +#include <tqapplication.h> +#include <tqfile.h> +#include <tqregexp.h> +#include <tqtimer.h> +#include <tqvaluestack.h> + +#include <kapplication.h> +#include <kdebug.h> +#include <kparts/componentfactory.h> +#include <kplugininfo.h> +#include <ksettings/dispatcher.h> +#include <ksimpleconfig.h> +#include <kstandarddirs.h> +#include <kstaticdeleter.h> +#include <kurl.h> + + +namespace Komposer +{ + +class PluginManager::Private +{ +public: + // All available plugins, regardless of category, and loaded or not + TQValueList<KPluginInfo*> plugins; + + // Dict of all currently loaded plugins, mapping the KPluginInfo to + // a plugin + TQMap<KPluginInfo*, Plugin*> loadedPlugins; + + // The plugin manager's mode. The mode is StartingUp until loadAllPlugins() + // has finished loading the plugins, after which it is set to Running. + // ShuttingDown and DoneShutdown are used during Komposer shutdown by the + // async unloading of plugins. + enum ShutdownMode { StartingUp, Running, ShuttingDown, DoneShutdown }; + ShutdownMode shutdownMode; + + KSharedConfig::Ptr config; + // Plugins pending for loading + TQValueStack<TQString> pluginsToLoad; +}; + +PluginManager::PluginManager( TQObject *parent ) + : TQObject( parent ) +{ + d = new Private; + + // We want to add a reference to the application's event loop so we + // can remain in control when all windows are removed. + // This way we can unload plugins asynchronously, which is more + // robust if they are still doing processing. + kapp->ref(); + d->shutdownMode = Private::StartingUp; + + KSettings::Dispatcher::self()->registerInstance( KGlobal::instance(), + this, TQT_SLOT( loadAllPlugins() ) ); + + d->plugins = KPluginInfo::fromServices( + KTrader::self()->query( TQString::tqfromLatin1( "Komposer/Plugin" ), + TQString::tqfromLatin1( "[X-Komposer-Version] == 1" ) ) ); +} + +PluginManager::~PluginManager() +{ + if ( d->shutdownMode != Private::DoneShutdown ) { + slotShutdownTimeout(); +#if 0 + kdWarning() << k_funcinfo + << "Destructing plugin manager without going through " + << "the shutdown process!" + << endl + << kdBacktrace(10) << endl; +#endif + } + + // Quick cleanup of the remaining plugins, hope it helps + TQMap<KPluginInfo*, Plugin*>::ConstIterator it; + for ( it = d->loadedPlugins.begin(); it != d->loadedPlugins.end(); /* EMPTY */ ) + { + // Remove causes the iterator to become invalid, so pre-increment first + TQMap<KPluginInfo*, Plugin*>::ConstIterator nextIt( it ); + ++nextIt; + kdWarning() << k_funcinfo << "Deleting stale plugin '" + << it.data()->name() << "'" << endl; + delete it.data(); + it = nextIt; + } + + delete d; +} + +TQValueList<KPluginInfo*> +PluginManager::availablePlugins( const TQString &category ) const +{ + if ( category.isEmpty() ) + return d->plugins; + + TQValueList<KPluginInfo*> result; + TQValueList<KPluginInfo*>::ConstIterator it; + for ( it = d->plugins.begin(); it != d->plugins.end(); ++it ) + { + if ( ( *it )->category() == category ) + result.append( *it ); + } + + return result; +} + +TQMap<KPluginInfo*, Plugin*> +PluginManager::loadedPlugins( const TQString &category ) const +{ + TQMap<KPluginInfo*, Plugin*> result; + TQMap<KPluginInfo*, Plugin*>::ConstIterator it; + for ( it = d->loadedPlugins.begin(); it != d->loadedPlugins.end(); ++it ) + { + if ( category.isEmpty() || it.key()->category() == category ) + result.insert( it.key(), it.data() ); + } + + return result; +} + +void +PluginManager::shutdown() +{ + d->shutdownMode = Private::ShuttingDown; + + // Remove any pending plugins to load, we're shutting down now :) + d->pluginsToLoad.clear(); + + // Ask all plugins to unload + if ( d->loadedPlugins.empty() ) { + d->shutdownMode = Private::DoneShutdown; + } else { + TQMap<KPluginInfo*, Plugin*>::ConstIterator it; + for ( it = d->loadedPlugins.begin(); it != d->loadedPlugins.end(); /* EMPTY */ ) + { + // Remove causes the iterator to become invalid, so pre-increment first + TQMap<KPluginInfo*, Plugin*>::ConstIterator nextIt( it ); + ++nextIt; + it.data()->aboutToUnload(); + it = nextIt; + } + } + + TQTimer::singleShot( 3000, this, TQT_SLOT(slotShutdownTimeout()) ); +} + +void +PluginManager::slotPluginReadyForUnload() +{ + // Using TQObject::sender() is on purpose here, because otherwise all + // plugins would have to pass 'this' as parameter, which makes the API + // less clean for plugin authors + Plugin* plugin = dynamic_cast<Plugin*>( const_cast<TQObject*>( sender() ) ); + if ( !plugin ) + { + kdWarning() << k_funcinfo << "Calling object is not a plugin!" << endl; + return; + + } + kdDebug()<<"manager unloading"<<endl; + plugin->deleteLater(); +} + +void +PluginManager::slotShutdownTimeout() +{ + // When we were already done the timer might still fire. + // Do nothing in that case. + if ( d->shutdownMode == Private::DoneShutdown ) + return; + +#ifndef NDEBUG + TQStringList remaining; + for ( TQMap<KPluginInfo*, Plugin*>::ConstIterator it = d->loadedPlugins.begin(); + it != d->loadedPlugins.end(); ++it ) + remaining.append( it.key()->pluginName() ); + + kdWarning() << k_funcinfo << "Some plugins didn't shutdown in time!" << endl + << "Remaining plugins: " + << remaining.join( TQString::tqfromLatin1( ", " ) ) << endl + << "Forcing Komposer shutdown now." << endl; +#endif + + slotShutdownDone(); +} + +void +PluginManager::slotShutdownDone() +{ + d->shutdownMode = Private::DoneShutdown; + + kapp->deref(); +} + +void +PluginManager::loadAllPlugins() +{ + // FIXME: We need session management here - Martijn + + if ( !d->config ) + d->config = KSharedConfig::openConfig( "komposerrc" ); + + TQMap<TQString, TQString> entries = d->config->entryMap( + TQString::tqfromLatin1( "Plugins" ) ); + + TQMap<TQString, TQString>::Iterator it; + for ( it = entries.begin(); it != entries.end(); ++it ) + { + TQString key = it.key(); + if ( key.endsWith( TQString::tqfromLatin1( "Enabled" ) ) ) + { + key.setLength( key.length() - 7 ); + //kdDebug() << k_funcinfo << "Set " << key << " to " << it.data() << endl; + + if ( it.data() == TQString::tqfromLatin1( "true" ) ) + { + if ( !plugin( key ) ) + d->pluginsToLoad.push( key ); + } + else + { + // FIXME: Does this ever happen? As loadAllPlugins is only called on startup + // I'd say 'no'. If it does, it should be made async + // though. - Martijn + if ( plugin( key ) ) + unloadPlugin( key ); + } + } + } + + // Schedule the plugins to load + TQTimer::singleShot( 0, this, TQT_SLOT( slotLoadNextPlugin() ) ); +} + +void PluginManager::slotLoadNextPlugin() +{ + if ( d->pluginsToLoad.isEmpty() ) + { + if ( d->shutdownMode == Private::StartingUp ) + { + d->shutdownMode = Private::Running; + emit allPluginsLoaded(); + } + return; + } + + TQString key = d->pluginsToLoad.pop(); + loadPluginInternal( key ); + + // Schedule the next run unconditionally to avoid code duplication on the + // allPluginsLoaded() signal's handling. This has the added benefit that + // the signal is delayed one event loop, so the accounts are more likely + // to be instantiated. + TQTimer::singleShot( 0, this, TQT_SLOT( slotLoadNextPlugin() ) ); +} + +Plugin* +PluginManager::loadPlugin( const TQString &pluginId, + PluginLoadMode mode /* = LoadSync */ ) +{ + if ( mode == LoadSync ) { + return loadPluginInternal( pluginId ); + } else { + d->pluginsToLoad.push( pluginId ); + TQTimer::singleShot( 0, this, TQT_SLOT( slotLoadNextPlugin() ) ); + return 0; + } +} + +Plugin* +PluginManager::loadPluginInternal( const TQString &pluginId ) +{ + KPluginInfo* info = infoForPluginId( pluginId ); + if ( !info ) { + kdWarning() << k_funcinfo << "Unable to find a plugin named '" + << pluginId << "'!" << endl; + return 0; + } + + if ( d->loadedPlugins.contains( info ) ) + return d->loadedPlugins[ info ]; + + int error = 0; + Plugin *plugin = KParts::ComponentFactory::createInstanceFromQuery<Komposer::Plugin>( + TQString::tqfromLatin1( "Komposer/Plugin" ), + TQString::tqfromLatin1( "[X-KDE-PluginInfo-Name]=='%1'" ).tqarg( pluginId ), + this, 0, TQStringList(), &error ); + + if ( plugin ) { + d->loadedPlugins.insert( info, plugin ); + info->setPluginEnabled( true ); + + connect( plugin, TQT_SIGNAL(destroyed(TQObject*)), + this, TQT_SLOT(slotPluginDestroyed(TQObject*)) ); + connect( plugin, TQT_SIGNAL(readyForUnload()), + this, TQT_SLOT(slotPluginReadyForUnload()) ); + + kdDebug() << k_funcinfo << "Successfully loaded plugin '" + << pluginId << "'" << endl; + + emit pluginLoaded( plugin ); + } else { + switch ( error ) { + case KParts::ComponentFactory::ErrNoServiceFound: + kdDebug() << k_funcinfo << "No service implementing the given mimetype " + << "and fullfilling the given constraint expression can be found." + << endl; + break; + + case KParts::ComponentFactory::ErrServiceProvidesNoLibrary: + kdDebug() << "the specified service provides no shared library." << endl; + break; + + case KParts::ComponentFactory::ErrNoLibrary: + kdDebug() << "the specified library could not be loaded." << endl; + break; + + case KParts::ComponentFactory::ErrNoFactory: + kdDebug() << "the library does not export a factory for creating components." + << endl; + break; + + case KParts::ComponentFactory::ErrNoComponent: + kdDebug() << "the factory does not support creating components " + << "of the specified type." + << endl; + break; + } + + kdDebug() << k_funcinfo << "Loading plugin '" << pluginId + << "' failed, KLibLoader reported error: '" + << KLibLoader::self()->lastErrorMessage() + << "'" << endl; + } + + return plugin; +} + +bool +PluginManager::unloadPlugin( const TQString &spec ) +{ + TQMap<KPluginInfo*, Plugin*>::ConstIterator it; + for ( it = d->loadedPlugins.begin(); it != d->loadedPlugins.end(); ++it ) + { + if ( it.key()->pluginName() == spec ) + { + it.data()->aboutToUnload(); + return true; + } + } + + return false; +} + +void +PluginManager::slotPluginDestroyed( TQObject *plugin ) +{ + TQMap<KPluginInfo*, Plugin*>::Iterator it; + for ( it = d->loadedPlugins.begin(); it != d->loadedPlugins.end(); ++it ) + { + if ( it.data() == plugin ) + { + d->loadedPlugins.erase( it ); + break; + } + } + + if ( d->shutdownMode == Private::ShuttingDown && d->loadedPlugins.isEmpty() ) + { + // Use a timer to make sure any pending deleteLater() calls have + // been handled first + TQTimer::singleShot( 0, this, TQT_SLOT(slotShutdownDone()) ); + } +} + +Plugin* +PluginManager::plugin( const TQString &pluginId ) const +{ + KPluginInfo *info = infoForPluginId( pluginId ); + if ( !info ) + return 0; + + if ( d->loadedPlugins.contains( info ) ) + return d->loadedPlugins[ info ]; + else + return 0; +} + +TQString +PluginManager::pluginName( const Plugin *plugin ) const +{ + TQMap<KPluginInfo*, Plugin*>::ConstIterator it; + for ( it = d->loadedPlugins.begin(); it != d->loadedPlugins.end(); ++it ) + { + if ( it.data() == plugin ) + return it.key()->name(); + } + + return TQString::tqfromLatin1( "Unknown" ); +} + +TQString +PluginManager::pluginId( const Plugin *plugin ) const +{ + TQMap<KPluginInfo*, Plugin*>::ConstIterator it; + for ( it = d->loadedPlugins.begin(); it != d->loadedPlugins.end(); ++it ) + { + if ( it.data() == plugin ) + return it.key()->pluginName(); + } + + return TQString::tqfromLatin1( "unknown" ); +} + +TQString +PluginManager::pluginIcon( const Plugin *plugin ) const +{ + TQMap<KPluginInfo*, Plugin*>::ConstIterator it; + for ( it = d->loadedPlugins.begin(); it != d->loadedPlugins.end(); ++it ) + { + if ( it.data() == plugin ) + return it.key()->icon(); + } + + return TQString::tqfromLatin1( "Unknown" ); +} + +KPluginInfo* +PluginManager::infoForPluginId( const TQString &pluginId ) const +{ + TQValueList<KPluginInfo*>::ConstIterator it; + for ( it = d->plugins.begin(); it != d->plugins.end(); ++it ) + { + if ( ( *it )->pluginName() == pluginId ) + return *it; + } + + return 0; +} + +bool +PluginManager::setPluginEnabled( const TQString &pluginId, bool enabled /* = true */ ) +{ + if ( !d->config ) + d->config = KSharedConfig::openConfig( "komposerrc" ); + + d->config->setGroup( "Plugins" ); + + + if ( !infoForPluginId( pluginId ) ) + return false; + + d->config->writeEntry( pluginId + TQString::tqfromLatin1( "Enabled" ), enabled ); + d->config->sync(); + + return true; +} + +} + +#include "pluginmanager.moc" diff --git a/libtdepim/komposer/core/pluginmanager.h b/libtdepim/komposer/core/pluginmanager.h new file mode 100644 index 000000000..34cc6b9c5 --- /dev/null +++ b/libtdepim/komposer/core/pluginmanager.h @@ -0,0 +1,252 @@ +// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*- +/** + * pluginmanager.h + * + * Copyright (C) 2004 Zack Rusin <zack@kde.org> + * Copyright (C) 2003 Martijn Klingens <klingens@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef KOMPOSER_PLUGINMANAGER_H +#define KOMPOSER_PLUGINMANAGER_H + +#include <tqmap.h> +#include <tqobject.h> +#include <tqstring.h> +#include <tqstringlist.h> +#include <tqvaluelist.h> + +class KPluginInfo; + +namespace Komposer +{ + + class Plugin; + + class PluginManager : public TQObject + { + Q_OBJECT + TQ_OBJECT + + public: + PluginManager( TQObject * ); + + ~PluginManager(); + + /** + * Returns a list of all available plugins for the given category. + * Currently there are two categories, "Plugins" and "Editors", but + * you can add your own categories if you want. + * + * If you pass an empty string you get the complete list of ALL plugins. + * + * You can query all information on the plugins through the @ref KPluginInfo + * interface. + */ + TQValueList<KPluginInfo*> availablePlugins( const TQString &category + = TQString() ) const; + + /** + * Returns a list of all plugins that are actually loaded. + * If you omit the category you get all, otherwise it's a filtered list. + * See also @ref availablePlugins(). + */ + TQMap<KPluginInfo*, Plugin*> loadedPlugins( const TQString &category + = TQString() ) const; + + /** + * @brief Search by plugin name. This is the key used as X-KDE-PluginInfo-Name + * in the .desktop file, e.g. "komposer_attachment" + * + * @return The @ref Plugin object found by the search, or a null + * pointer if the plugin is not loaded. + * + * If you want to also load the plugin you can better use @ref loadPlugin, + * which returns + * the pointer to the plugin if it's already loaded. + */ + Plugin* plugin( const TQString &pluginName ) const; + + /** + * @brief Return the short user-visible name of the plugin. + * + * If you want to have the internal name, use @ref pluginId() instead. + * + * @return The name of the protocol, in the user's locale. + */ + TQString pluginName( const Plugin *plugin ) const; + + /** + * @brief Return the internal name of the plugin. + * + * You cannot display this name on the screen, it's used internally for + * passing around IDs. Use @ref pluginName() for a string ready for display. + * + * @return The name of the protocol, in the user's locale. + */ + TQString pluginId( const Plugin *plugin ) const; + + /** + * @brief Unload the plugin specified by @p pluginName + */ + bool unloadPlugin( const TQString &pluginName ); + + /** + * @brief Retrieve the name of the icon for a @ref Plugin. + * + * @return An empty string if the given plugin is not loaded + * or the filename of the icon to use. + */ + TQString pluginIcon( const Plugin *plugin ) const; + + /** + * Shuts down the plugin manager on Komposer shutdown, but first + * unloads all plugins asynchronously. + * + * After 3 seconds all plugins should be removed; what's still left + * by then is unloaded through a hard delete instead. + * + * Note that this call also derefs the plugin manager from the event + * loop, so do NOT call this method when not terminating Komposer! + */ + void shutdown(); + + /** + * Enable a plugin. + * + * This marks a plugin as enabled in the config file, so loadAll() + * can pick it up later. + * + * This method does not actually load a plugin, it only edits the + * config file. + * + * @param name is the name of the plugin as it is listed in the .desktop + * file in the X-KDE-Library field. + * + * Returns false when no appropriate plugin can be found. + */ + bool setPluginEnabled( const TQString &name, bool enabled = true ); + + /** + * Plugin loading mode. Used by @loadPlugin. Code that doesn't want to block + * the GUI and/or lot a lot of plugins at once should use Async loading. + * The default is sync loading. + */ + enum PluginLoadMode { LoadSync, LoadAsync }; + + public slots: + /** + * @brief Load a single plugin by plugin name. Returns an existing plugin + * if one is already loaded in memory. + * + * If mode is set to Async, the plugin will be queued and loaded in + * the background. This method will return a null pointer. To get + * the loaded plugin you can track the @ref pluginLoaded() signal. + * + * See also @ref plugin(). + */ + Plugin* loadPlugin( const TQString &pluginId, PluginLoadMode mode = LoadSync ); + + /** + * @brief Loads all the enabled plugins. Also used to reread the + * config file when the configuration has changed. + */ + void loadAllPlugins(); + + signals: + /** + * @brief Signals a new plugin has just been loaded. + */ + void pluginLoaded( Plugin *plugin ); + + /** + * @brief All plugins have been loaded by the plugin manager. + * + * This signal is emitted exactly ONCE, when the plugin manager has emptied + * its plugin queue for the first time. This means that if you call an async + * loadPlugin() before loadAllPlugins() this signal is probably emitted after + * the initial call completes, unless you are quick enough to fill the queue + * before it completes, which is a dangerous race you shouldn't count upon :) + * + * The signal is delayed one event loop iteration through a singleShot timer, + * but that is not guaranteed to be enough for account instantiation. You may + * need an additional timer for it in the code if you want to programmatically + * act on it. + * + * If you use the signal for enabling/disabling GUI objects there is little + * chance a user is able to activate them in the short while that's remaining, + * the slow part of the code is over now and the remaining processing time + * is neglectable for the user. + */ + void allPluginsLoaded(); + + private slots: + /** + * @brief Cleans up some references if the plugin is destroyed + */ + void slotPluginDestroyed( TQObject *plugin ); + + /** + * shutdown() starts a timer, when it fires we force all plugins + * to be unloaded here by deref()-ing the event loop to trigger the plugin + * manager's destruction + */ + void slotShutdownTimeout(); + + /** + * Common entry point to deref() the KApplication. Used both by the clean + * shutdown and the timeout condition of slotShutdownTimeout() + */ + void slotShutdownDone(); + + /** + * Emitted by a Plugin when it's ready for unload + */ + void slotPluginReadyForUnload(); + + /** + * Load a plugin from our queue. Does nothing if the queue is empty. + * Schedules itself again if more plugins are pending. + */ + void slotLoadNextPlugin(); + + private: + /** + * @internal + * + * The internal method for loading plugins. + * Called by @ref loadPlugin directly or through the queue for async plugin + * loading. + */ + Plugin *loadPluginInternal( const TQString &pluginId ); + + /** + * @internal + * + * Find the KPluginInfo structure by key. Reduces some code duplication. + * + * Returns a null pointer when no plugin info is found. + */ + KPluginInfo *infoForPluginId( const TQString &pluginId ) const; + private: + class Private; + Private *d; + }; + +} + +#endif // KOMPOSER_PLUGINMANAGER_H diff --git a/libtdepim/komposer/core/prefsmodule.cpp b/libtdepim/komposer/core/prefsmodule.cpp new file mode 100644 index 000000000..77e1c264b --- /dev/null +++ b/libtdepim/komposer/core/prefsmodule.cpp @@ -0,0 +1,142 @@ +/** + * prefsmodule.cpp + * + * Copyright (C) 2003-2004 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include "prefsmodule.h" + +#include <kaboutdata.h> +#include <kdebug.h> +#include <kcombobox.h> +#include <klocale.h> +#include <ktrader.h> + +#include <tqlayout.h> +#include <tqlabel.h> +#include <tqbuttongroup.h> + +#include <tdepimmacros.h> + +extern "C" +{ + KDE_EXPORT KCModule *create_komposerconfig( TQWidget *parent, const char * ) { + return new Komposer::PrefsModule( parent, "komposerprefs" ); + } +} +using namespace Komposer; + +PrefsModule::PrefsModule( TQWidget *parent, const char *name ) + : KPrefsModule( Komposer::Prefs::self(), parent, name ) +{ + TQVBoxLayout *topLayout = new TQVBoxLayout( this ); + + EditorSelection *editors = new EditorSelection( i18n( "Editors" ), + Komposer::Prefs::self()->m_activeEditor, + this ); + topLayout->addWidget( editors->groupBox() ); + + addWid( editors ); + + load(); +} + +const KAboutData* +PrefsModule::aboutData() const +{ + KAboutData *about = new KAboutData( I18N_NOOP( "komposerconfig" ), + I18N_NOOP( "KDE Komposer" ), + 0, 0, KAboutData::License_LGPL, + I18N_NOOP( "(c), 2003-2004 Zack Rusin" ) ); + + about->addAuthor( "Zack Rusin", 0, "zack@kde.org" );; + + return about; +} + + +EditorSelection::EditorSelection( const TQString &text, TQString &reference, + TQWidget *parent ) + : m_reference( reference ) +{ + m_box = new TQGroupBox( 0, TQt::Vertical, text, parent ); + TQVBoxLayout *boxLayout = new TQVBoxLayout( m_box->tqlayout() ); + boxLayout->tqsetAlignment( TQt::AlignTop ); + + m_editorsCombo = new KComboBox( m_box ); + boxLayout->addWidget( m_editorsCombo ); + + connect( m_editorsCombo, TQT_SIGNAL(activated(const TQString&)), + TQT_SLOT(slotActivated(const TQString&)) ); +} + +EditorSelection::~EditorSelection() +{ +} + +TQGroupBox* +EditorSelection::groupBox() const +{ + return m_box; +} + +void +EditorSelection::readConfig() +{ + m_editorsCombo->clear(); + + KTrader::OfferList editors = KTrader::self()->query( + TQString::tqfromLatin1( "Komposer/Editor" ) ); + KTrader::OfferList::ConstIterator it; + int i = 0; + for ( it = editors.begin(); it != editors.end(); ++it, ++i ) { + if ( !(*it)->hasServiceType( TQString::tqfromLatin1( "Komposer/Editor" ) ) ) + continue; + + TQString name = (*it)->property( "X-KDE-KomposerIdentifier" ).toString(); + m_editorsCombo->insertItem( name ); + if ( m_reference.contains( name ) ) + m_editorsCombo->setCurrentItem( i ); + } +} + +void EditorSelection::writeConfig() +{ + m_reference = m_services[ m_editorsCombo->currentText()]-> + property( "X-KDE-KomposerIdentifier" ).toString(); +} + +void +EditorSelection::slotActivated( const TQString &editor ) +{ + if ( !editor.isEmpty() ) + emit changed(); +} + +void +EditorSelection::setItem( const TQString &str ) +{ + for ( int i = 0; i < m_editorsCombo->count(); ++i ) { + if ( m_editorsCombo->text( i ) == str ) { + m_editorsCombo->setCurrentItem( i ); + break; + } + } +} + +#include "prefsmodule.moc" diff --git a/libtdepim/komposer/core/prefsmodule.h b/libtdepim/komposer/core/prefsmodule.h new file mode 100644 index 000000000..aba969f16 --- /dev/null +++ b/libtdepim/komposer/core/prefsmodule.h @@ -0,0 +1,72 @@ +/* + * prefsmodule.h + * + * Copyright (C) 2003 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#ifndef KOMPOSER_PREFSMODULE_H +#define KOMPOSER_PREFSMODULE_H + +#include <kprefsdialog.h> +#include <kservice.h> +#include <tqmap.h> +class TQGroupBox; +class TQListViewItem; + +class KAboutData; +class KComboBox; + +namespace Komposer { + + class PrefsModule : public KPrefsModule + { + Q_OBJECT + TQ_OBJECT + public: + PrefsModule( TQWidget *parent=0, const char *name=0 ); + virtual const KAboutData *aboutData() const; + }; + + class EditorSelection : public KPrefsWid + { + Q_OBJECT + TQ_OBJECT + + public: + EditorSelection( const TQString &text, TQString &reference, TQWidget *parent ); + ~EditorSelection(); + + void readConfig(); + void writeConfig(); + + TQGroupBox *groupBox() const; + + private slots: + void slotActivated( const TQString & ); + + private: + void setItem( const TQString & ); + private: + TQString &m_reference; + + TQGroupBox *m_box; + KComboBox *m_editorsCombo; + TQMap<TQString, KService::Ptr> m_services; + }; +} + +#endif diff --git a/libtdepim/komposer/core/settings.kcfgc b/libtdepim/komposer/core/settings.kcfgc new file mode 100644 index 000000000..fc98478a9 --- /dev/null +++ b/libtdepim/komposer/core/settings.kcfgc @@ -0,0 +1,9 @@ +# Code generation options for kconfig_compiler +File=komposer.kcfg +NameSpace=Komposer +ClassName=Config +Singleton=true +Mutators=true +#MemberVariables=public +GlobalEnums=true +#SetUserTexts=true diff --git a/libtdepim/komposer/core/tests/Makefile.am b/libtdepim/komposer/core/tests/Makefile.am new file mode 100644 index 000000000..fbf127956 --- /dev/null +++ b/libtdepim/komposer/core/tests/Makefile.am @@ -0,0 +1,20 @@ +INCLUDES = -I$(top_builddir)/libtdepim/komposer/core $(all_includes) + +check_PROGRAMS = testfactory testmanager testkomposer + +METASOURCES = AUTO + +testfactory_SOURCES = main.cpp managertest.cpp +testfactory_LDFLAGS = $(all_libraries) $(KDE_RPATH) $(LIB_QT) -lDCOP $(LIB_KDECORE) $(LIB_KDEUI) -ltdefx $(LIB_KIO) -lktexteditor +testfactory_LDADD = ../libkomposer.la + +testmanager_SOURCES = testmanager.cpp +testmanager_LDFLAGS = $(all_libraries) $(KDE_RPATH) $(LIB_QT) -lDCOP $(LIB_KDECORE) $(LIB_KDEUI) -ltdefx $(LIB_KIO) -lktexteditor +testmanager_LDADD = ../libkomposer.la + +testkomposer_SOURCES = testkomposer.cpp +testkomposer_LDFLAGS = $(all_libraries) $(KDE_RPATH) $(LIB_QT) -lDCOP $(LIB_KDECORE) $(LIB_KDEUI) -ltdefx $(LIB_KIO) -lktexteditor +testkomposer_LDADD = ../libkomposer.la + +check: testfactory + @./testfactory 2>&1 | grep "tests:" diff --git a/libtdepim/komposer/core/tests/main.cpp b/libtdepim/komposer/core/tests/main.cpp new file mode 100644 index 000000000..4a33a2f13 --- /dev/null +++ b/libtdepim/komposer/core/tests/main.cpp @@ -0,0 +1,52 @@ +/** + * main.cpp + * + * Copyright (C) 2004 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#include "testfactory.h" +#include <kaboutdata.h> +#include <kapplication.h> +#include <kdebug.h> +#include <klocale.h> +#include <kcmdlineargs.h> + +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + + +static const KCmdLineOptions options[] = +{ + {"verbose", "Verbose output", 0}, + KCmdLineLastOption +}; +int main( int argc, char** argv ) +{ + KAboutData aboutData( "tests","Test","0.1" ); + KCmdLineArgs::init( argc, argv, &aboutData ); + KCmdLineArgs::addCmdLineOptions( options ); + + KApplication app; + + KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); + Q_UNUSED( args ); + + TestFactory t; + return t.runTests(); +} diff --git a/libtdepim/komposer/core/tests/managertest.cpp b/libtdepim/komposer/core/tests/managertest.cpp new file mode 100644 index 000000000..487a84c59 --- /dev/null +++ b/libtdepim/komposer/core/tests/managertest.cpp @@ -0,0 +1,43 @@ +/** + * managertest.cpp + * + * Copyright (C) 2004 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#include "managertest.h" + +#include "pluginmanager.h" +using namespace Komposer; + +ManagerTest::ManagerTest( TQObject* parent ) + : TQObject( parent ) +{ + m_manager = new PluginManager( this ); +} + + +void ManagerTest::allTests() +{ + CHECK( m_manager->availablePlugins().isEmpty(), true ); + CHECK( m_manager->loadedPlugins().empty(), true ); + CHECK( m_manager->plugin( "non-existing" ), ( Plugin* )0 ); + m_manager->loadAllPlugins(); + CHECK( m_manager->loadedPlugins().empty(), true ); + m_manager->shutdown(); +} + +#include "managertest.moc" diff --git a/libtdepim/komposer/core/tests/managertest.h b/libtdepim/komposer/core/tests/managertest.h new file mode 100644 index 000000000..f02510e67 --- /dev/null +++ b/libtdepim/komposer/core/tests/managertest.h @@ -0,0 +1,45 @@ +/* + * managertest.h + * + * Copyright (C) 2004 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#ifndef MANAGERTEST_H +#define MANAGERTEST_H + +#include <tqobject.h> +#include "tester.h" + +namespace Komposer { + class PluginManager; +} + +class ManagerTest : public TQObject, + public Tester +{ + Q_OBJECT + TQ_OBJECT +public: + ManagerTest( TQObject* parent = 0 ); + +public slots: + void allTests(); +private: + Komposer::PluginManager* m_manager; +}; + +#endif diff --git a/libtdepim/komposer/core/tests/tester.cpp b/libtdepim/komposer/core/tests/tester.cpp new file mode 100644 index 000000000..b32c89644 --- /dev/null +++ b/libtdepim/komposer/core/tests/tester.cpp @@ -0,0 +1,30 @@ +/** + * tester.cpp + * + * Copyright (C) 2004 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#include "tester.h" + + +Tester::Tester( TQObject* parent ) + : TQObject( parent ) +{ +} + + +#include "tester.moc" diff --git a/libtdepim/komposer/core/tests/tester.h b/libtdepim/komposer/core/tests/tester.h new file mode 100644 index 000000000..5981d681d --- /dev/null +++ b/libtdepim/komposer/core/tests/tester.h @@ -0,0 +1,71 @@ +/* + * tester.h + * + * Copyright (C) 2004 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#ifndef TESTER_H +#define TESTER_H + +#include <kdebug.h> +#include <tqstringlist.h> + +#define CHECK( x, y ) check( __FILE__, __LINE__, #x, x, y ) + +class Tester +{ +public: + Tester() : m_tests( 0 ) {} + virtual ~Tester() {} + +public: + virtual void allTests() = 0; + +public: + int testsFinished() const { + return m_tests; + } + int testsFailed() const { + return m_errorList.count(); + } + TQStringList errorList() const { + return m_errorList; + } + +protected: + template<typename T> + void check( const char* file, int line, const char* str, + const T& result, const T& expectedResult ) + { + if ( result != expectedResult ) { + TQString error; + TQTextStream ts( &error, IO_WriteOnly ); + ts << file << "["<< line <<"]:" + <<" failed on \""<< str <<"\"" + << "\ntests:\t\t result = " + << result + << ", expected = "<< expectedResult; + m_errorList.append( error ); + } + ++m_tests; + } +private: + TQStringList m_errorList; + int m_tests; +}; + +#endif diff --git a/libtdepim/komposer/core/tests/testfactory.h b/libtdepim/komposer/core/tests/testfactory.h new file mode 100644 index 000000000..9354e52ad --- /dev/null +++ b/libtdepim/komposer/core/tests/testfactory.h @@ -0,0 +1,71 @@ +/* + * testfactory.h + * + * Copyright (C) 2004 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#ifndef TESTFACTORY_H +#define TESTFACTORY_H + +#include "managertest.h" + +#include <tqasciidict.h> + +#define ADD_TEST(x) addTest( #x, new x ) + +class TestFactory +{ +public: + TestFactory() + { + ADD_TEST( ManagerTest ); + m_tests.setAutoDelete( true ); + } + + int runTests() + { + int result = 0; + kdDebug()<<"Running tests..."<<endl; + TQAsciiDictIterator<Tester> it( m_tests ); + for( ; it.current(); ++it ) { + Tester* test = it.current(); + test->allTests(); + TQStringList errorList = test->errorList(); + if ( !errorList.empty() ) { + ++result; + kdDebug()<< it.currentKey() <<" errors:" << endl; + for ( TQStringList::Iterator itr = errorList.begin(); + itr != errorList.end(); ++itr ) { + kdDebug()<< "\t" << (*itr).latin1() <<endl;; + } + } else { + kdDebug()<< it.currentKey()<< " OK "<<endl; + } + } + kdDebug()<< "Done" <<endl; + return result; + } +public: + void addTest( const char* name, Tester* test ) + { + m_tests.insert( name, test ); + } +private: + TQAsciiDict<Tester> m_tests; +}; + +#endif diff --git a/libtdepim/komposer/core/tests/testkomposer.cpp b/libtdepim/komposer/core/tests/testkomposer.cpp new file mode 100644 index 000000000..b7997e8c3 --- /dev/null +++ b/libtdepim/komposer/core/tests/testkomposer.cpp @@ -0,0 +1,18 @@ +#include "core.h" + +#include <kplugininfo.h> +#include <kcmdlineargs.h> +#include <kapplication.h> +#include <kdebug.h> + +int main( int argc, char **argv ) +{ + KCmdLineArgs::init( argc, argv, "test", "test", "test", "0.1" ); + KApplication app; + + Komposer::Core *core = new Komposer::Core(); + app.setMainWidget( core ); + core->show(); + + return app.exec(); +} diff --git a/libtdepim/komposer/core/tests/testmanager.cpp b/libtdepim/komposer/core/tests/testmanager.cpp new file mode 100644 index 000000000..0fcba16b9 --- /dev/null +++ b/libtdepim/komposer/core/tests/testmanager.cpp @@ -0,0 +1,53 @@ +#include "testmanager.h" +#include "testmanager.moc" + +#include "pluginmanager.h" +#include "plugin.h" + +#include <kplugininfo.h> +#include <kcmdlineargs.h> +#include <kapplication.h> +#include <kdebug.h> + +using namespace Komposer; + +TestManager::TestManager( TQObject *parent ) + : TQObject( parent ) +{ + m_manager = new PluginManager( this ); + connect( m_manager, TQT_SIGNAL(pluginLoaded(Plugin*)), + TQT_SLOT(slotPluginLoaded(Plugin*)) ); + connect( m_manager, TQT_SIGNAL(allPluginsLoaded()), + TQT_SLOT(slotAllPluginsLoaded()) ); + m_manager->loadAllPlugins(); + + TQValueList<KPluginInfo*> plugins = m_manager->availablePlugins(); + kdDebug()<<"Number of available plugins is "<< plugins.count() <<endl; + for ( TQValueList<KPluginInfo*>::iterator it = plugins.begin(); it != plugins.end(); ++it ) { + KPluginInfo *i = ( *it ); + kdDebug()<<"\tAvailable plugin "<< i->name() + <<", comment = "<< i->comment() <<endl; + } +} + +void TestManager::slotAllPluginsLoaded() +{ + kdDebug()<<"Done"<<endl; + m_manager->shutdown(); + tqApp->exit(); +} + +void TestManager::slotPluginLoaded( Plugin *plugin ) +{ + kdDebug()<<"A plugin "<< m_manager->pluginName( plugin ) << " has been loaded"<<endl; +} + +int main( int argc, char **argv ) +{ + KCmdLineArgs::init( argc, argv, "test", "test", "test", "0.1" ); + KApplication app; + + TestManager manager( &app ); + + return app.exec(); +} diff --git a/libtdepim/komposer/core/tests/testmanager.h b/libtdepim/komposer/core/tests/testmanager.h new file mode 100644 index 000000000..5dfaa15d1 --- /dev/null +++ b/libtdepim/komposer/core/tests/testmanager.h @@ -0,0 +1,26 @@ +#ifndef TESTMANAGER_H +#define TESTMANAGER_H + +#include <tqobject.h> + +namespace Komposer { + class Plugin; + class PluginManager; +} +using Komposer::Plugin; + +class TestManager : public TQObject +{ + Q_OBJECT + TQ_OBJECT +public: + TestManager( TQObject *parent ); + +public slots: + void slotPluginLoaded( Plugin *plugin ); + void slotAllPluginsLoaded(); +private: + Komposer::PluginManager *m_manager; +}; + +#endif diff --git a/libtdepim/komposer/plugins/Makefile.am b/libtdepim/komposer/plugins/Makefile.am new file mode 100644 index 000000000..dcc4764a7 --- /dev/null +++ b/libtdepim/komposer/plugins/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = default diff --git a/libtdepim/komposer/plugins/default/Makefile.am b/libtdepim/komposer/plugins/default/Makefile.am new file mode 100644 index 000000000..7ae32dc08 --- /dev/null +++ b/libtdepim/komposer/plugins/default/Makefile.am @@ -0,0 +1,15 @@ +AM_CPPFLAGS = -I$(top_builddir)/libtdepim/komposer/core $(all_includes) + +kde_module_LTLIBRARIES = libkomposer_defaulteditor.la +libkomposer_defaulteditor_la_LDFLAGS = $(KDE_PLUGIN) $(all_libraries) +libkomposer_defaulteditor_la_LIBADD = ../../core/libkomposer.la $(LIB_KPARTS) + +libkomposer_defaulteditor_la_SOURCES = defaulteditor.cpp + +METASOURCES = AUTO + +servicedir = $(kde_servicesdir)/komposer +service_DATA = defaulteditor.desktop + +rcdir = $(kde_datadir)/komposer_defaulteditor +rc_DATA = defaulteditorui.rc diff --git a/libtdepim/komposer/plugins/default/defaulteditor.cpp b/libtdepim/komposer/plugins/default/defaulteditor.cpp new file mode 100644 index 000000000..27018ce5a --- /dev/null +++ b/libtdepim/komposer/plugins/default/defaulteditor.cpp @@ -0,0 +1,361 @@ +/** + * defaulteditor.cpp + * + * Copyright (C) 2004 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#include "defaulteditor.h" +#include "core.h" + +#include <kgenericfactory.h> +#include <kapplication.h> +#include <kaction.h> +#include <kiconloader.h> +#include <kdebug.h> + +#include <kaction.h> +#include <kcolordialog.h> +#include <kfiledialog.h> +#include <kinstance.h> +#include <klocale.h> +#include <kstdaction.h> +#include <kprinter.h> +#include <kfinddialog.h> +#include <kfind.h> +#include <kreplacedialog.h> +#include <kreplace.h> + +#include <tqtextedit.h> +#include <tqwidget.h> + +typedef KGenericFactory<DefaultEditor> DefaultEditorFactory; +K_EXPORT_COMPONENT_FACTORY( libkomposer_defaulteditor, + DefaultEditorFactory( "komposer_defaulteditor" ) ) + +DefaultEditor::DefaultEditor( TQObject *parent, const char *name, const TQStringList &args ) + : Editor( parent, name, args ), m_textEdit( 0 ) +{ + setInstance( DefaultEditorFactory::instance() ); + + m_textEdit = new TQTextEdit( 0 ); + + createActions( actionCollection() ); + + setXMLFile( "defaulteditorui.rc" ); +} + +DefaultEditor::~DefaultEditor() +{ +} + + +TQWidget* +DefaultEditor::widget() +{ + return m_textEdit; +} + +TQString +DefaultEditor::text() const +{ + return m_textEdit->text(); +} + +void +DefaultEditor::setText( const TQString &text ) +{ + m_textEdit->setText( text ); +} + +void +DefaultEditor::changeSignature( const TQString &sig ) +{ + TQString text = m_textEdit->text(); + + int sigStart = text.findRev( "-- " ); + TQString sigText = TQString( "-- \n%1" ).tqarg( sig ); + + text.replace( sigStart, text.length(), sigText ); +} + +void +DefaultEditor::createActions( KActionCollection *ac ) +{ + // + // File Actions + // + (void) KStdAction::open( this, TQT_SLOT(open()), ac ); + (void) KStdAction::openRecent( this, TQT_SLOT(openURL(const KURL &)), ac ); + (void) KStdAction::save( this, TQT_SLOT(save()), ac ); + (void) KStdAction::saveAs( this, TQT_SLOT(saveAs()), ac ); + + // + // Edit Actions + // + KAction *actionUndo = KStdAction::undo( m_textEdit, TQT_SLOT(undo()), ac ); + actionUndo->setEnabled( false ); + connect( m_textEdit, TQT_SIGNAL(undoAvailable(bool)), + actionUndo, TQT_SLOT(setEnabled(bool)) ); + + KAction *actionRedo = KStdAction::redo( m_textEdit, TQT_SLOT(redo()), ac ); + actionRedo->setEnabled( false ); + connect( m_textEdit, TQT_SIGNAL(redoAvailable(bool)), + actionRedo, TQT_SLOT(setEnabled(bool)) ); + + KAction *action_cut = KStdAction::cut( m_textEdit, TQT_SLOT(cut()), ac ); + action_cut->setEnabled( false ); + connect( m_textEdit, TQT_SIGNAL(copyAvailable(bool)), + action_cut, TQT_SLOT(setEnabled(bool)) ); + + KAction *action_copy = KStdAction::copy( m_textEdit, TQT_SLOT(copy()), ac ); + action_copy->setEnabled( false ); + connect( m_textEdit, TQT_SIGNAL(copyAvailable(bool)), + action_copy, TQT_SLOT(setEnabled(bool)) ); + + (void) KStdAction::print( this, TQT_SLOT(print()), ac ); + + (void) KStdAction::paste( m_textEdit, TQT_SLOT(paste()), ac ); + (void) new KAction( i18n( "C&lear" ), 0, + m_textEdit, TQT_SLOT(removeSelectedText()), + ac, "edit_clear" ); + + (void) KStdAction::selectAll( m_textEdit, TQT_SLOT(selectAll()), ac ); + + // + // View Actions + // + (void) KStdAction::zoomIn( m_textEdit, TQT_SLOT(zoomIn()), ac ); + (void) KStdAction::zoomOut( m_textEdit, TQT_SLOT(zoomOut()), ac ); + + // + // Character Formatting + // + m_actionBold = new KToggleAction( i18n("&Bold"), "text_bold", CTRL+Key_B, + ac, "format_bold" ); + connect( m_actionBold, TQT_SIGNAL(toggled(bool)), + m_textEdit, TQT_SLOT(setBold(bool)) ); + + m_actionItalic = new KToggleAction( i18n("&Italic"), "text_italic", CTRL+Key_I, + ac, "format_italic" ); + + connect( m_actionItalic, TQT_SIGNAL(toggled(bool)), + m_textEdit, TQT_SLOT(setItalic(bool) )); + + m_actionUnderline = new KToggleAction( i18n("&Underline"), "text_under", CTRL+Key_U, + ac, "format_underline" ); + + connect( m_actionUnderline, TQT_SIGNAL(toggled(bool)), + m_textEdit, TQT_SLOT(setUnderline(bool)) ); + + (void) new KAction( i18n("Text &Color..."), "colorpicker", 0, + this, TQT_SLOT(formatColor()), + ac, "format_color" ); + + // + // Font + // + m_actionFont = new KFontAction( i18n("&Font"), 0, + ac, "format_font" ); + connect( m_actionFont, TQT_SIGNAL(activated(const TQString &)), + m_textEdit, TQT_SLOT(setFamily(const TQString &)) ); + + + m_actionFontSize = new KFontSizeAction( i18n("Font &Size"), 0, + ac, "format_font_size" ); + connect( m_actionFontSize, TQT_SIGNAL(fontSizeChanged(int)), + m_textEdit, TQT_SLOT(setPointSize(int)) ); + + // + // Alignment + // + m_actionAlignLeft = new KToggleAction( i18n("Align &Left"), "text_left", 0, + ac, "format_align_left" ); + connect( m_actionAlignLeft, TQT_SIGNAL(toggled(bool)), + this, TQT_SLOT(setAlignLeft(bool)) ); + + m_actionAlignCenter = new KToggleAction( i18n("Align &Center"), "text_center", 0, + ac, "format_align_center" ); + connect( m_actionAlignCenter, TQT_SIGNAL(toggled(bool)), + this, TQT_SLOT(setAlignCenter(bool)) ); + + m_actionAlignRight = new KToggleAction( i18n("Align &Right"), "text_right", 0, + ac, "format_align_right" ); + connect( m_actionAlignRight, TQT_SIGNAL(toggled(bool)), + this, TQT_SLOT(setAlignRight(bool)) ); + + m_actionAlignJustify = new KToggleAction( i18n("&Justify"), "text_block", 0, + ac, "format_align_justify" ); + connect( m_actionAlignJustify, TQT_SIGNAL(toggled(bool)), + this, TQT_SLOT(setAlignJustify(bool)) ); + + m_actionAlignLeft->setExclusiveGroup( "tqalignment" ); + m_actionAlignCenter->setExclusiveGroup( "tqalignment" ); + m_actionAlignRight->setExclusiveGroup( "tqalignment" ); + m_actionAlignJustify->setExclusiveGroup( "tqalignment" ); + + // + // Tools + // + (void) KStdAction::spelling( this, TQT_SLOT(checkSpelling()), ac ); + + // + // Setup enable/disable + // + updateActions(); + + connect( m_textEdit, TQT_SIGNAL(currentFontChanged(const TQFont &)), + this, TQT_SLOT( updateFont() ) ); + connect( m_textEdit, TQT_SIGNAL(currentFontChanged(const TQFont &)), + this, TQT_SLOT(updateCharFmt()) ); + connect( m_textEdit, TQT_SIGNAL(cursorPositionChanged(int, int)), + this, TQT_SLOT(updateAligment()) ); +} + +void +DefaultEditor::updateActions() +{ + updateCharFmt(); + updateAligment(); + updateFont(); +} + +void +DefaultEditor::updateCharFmt() +{ + m_actionBold->setChecked( m_textEdit->bold() ); + m_actionItalic->setChecked( m_textEdit->italic() ); + m_actionUnderline->setChecked( m_textEdit->underline() ); +} + +void +DefaultEditor::updateAligment() +{ + int align = m_textEdit->tqalignment(); + + switch ( align ) { + case AlignRight: + m_actionAlignRight->setChecked( true ); + break; + case AlignCenter: + m_actionAlignCenter->setChecked( true ); + break; + case AlignLeft: + m_actionAlignLeft->setChecked( true ); + break; + case AlignJustify: + m_actionAlignJustify->setChecked( true ); + break; + default: + break; + } +} + +void +DefaultEditor::updateFont() +{ + if ( m_textEdit->pointSize() > 0 ) + m_actionFontSize->setFontSize( m_textEdit->pointSize() ); + m_actionFont->setFont( m_textEdit->family() ); +} + +void +DefaultEditor::formatColor() +{ + TQColor col; + + int s = KColorDialog::getColor( col, m_textEdit->color(), m_textEdit ); + if ( s != TQDialog::Accepted ) + return; + + m_textEdit->setColor( col ); +} + +void +DefaultEditor::setAlignLeft( bool yes ) +{ + if ( yes ) + m_textEdit->tqsetAlignment( AlignLeft ); +} + +void +DefaultEditor::setAlignRight( bool yes ) +{ + if ( yes ) + m_textEdit->tqsetAlignment( AlignRight ); +} + +void +DefaultEditor::setAlignCenter( bool yes ) +{ + if ( yes ) + m_textEdit->tqsetAlignment( AlignCenter ); +} + +void +DefaultEditor::setAlignJustify( bool yes ) +{ + if ( yes ) + m_textEdit->tqsetAlignment( AlignJustify ); +} + +// +// Content Actions +// + +bool +DefaultEditor::open() +{ + KURL url = KFileDialog::getOpenURL(); + if ( url.isEmpty() ) + return false; + + //fixme + //return openURL( url ); + return true; +} + +bool +DefaultEditor::saveAs() +{ + KURL url = KFileDialog::getSaveURL(); + if ( url.isEmpty() ) + return false; + + //FIXME + //return KParts::ReadWritePart::saveAs( url ); + return true; +} + +void +DefaultEditor::checkSpelling() +{ + TQString s; + if ( m_textEdit->hasSelectedText() ) + s = m_textEdit->selectedText(); + else + s = m_textEdit->text(); + + //KSpell::modalCheck( s ); +} + +bool +DefaultEditor::print() +{ + return true; +} + +#include "defaulteditor.moc" diff --git a/libtdepim/komposer/plugins/default/defaulteditor.desktop b/libtdepim/komposer/plugins/default/defaulteditor.desktop new file mode 100644 index 000000000..910f7d89c --- /dev/null +++ b/libtdepim/komposer/plugins/default/defaulteditor.desktop @@ -0,0 +1,109 @@ +[Desktop Entry] +Type=Service +Icon=editor +ServiceTypes=Komposer/Editor + +X-KDE-Library=libkomposer_defaulteditor +X-Komposer-Version=1 +X-Komposer-Weight=10 + +X-KDE-PluginInfo-Author=Zack Rusin +X-KDE-PluginInfo-Email=zack@kde.org +X-KDE-PluginInfo-Name=komposer_defaulteditor +X-KDE-PluginInfo-Version=0.0.1 +X-KDE-PluginInfo-Website=http://www.kde.org +X-KDE-PluginInfo-Category=Editors +X-KDE-PluginInfo-Depends= +X-KDE-PluginInfo-License=LGPL +X-KDE-PluginInfo-EnabledByDefault=true +Name=Komposer Editor +Name[af]=Komposer Redigeerder +Name[bg]=Редактор за Komposer +Name[br]=Aozer Komposer +Name[ca]=Editor Komposer +Name[cs]=Komposer editor +Name[da]=Komposer-editor +Name[el]=ΕπεξεÏγαστής Komposer +Name[eo]=Komposer-redaktilo +Name[es]=Editor Komposer +Name[et]=Komposeri redaktor +Name[eu]=Komposer editorea +Name[fa]=ویرایشگر Komposer +Name[fi]=Komposer-muokkain +Name[fr]=Éditeur Komposer +Name[fy]=Komposer-bewurker +Name[ga]=Eagarthóir Komposer +Name[gl]=Editor Komposer +Name[hu]=Komposer szerkesztÅ‘ +Name[is]=Komposer ritill +Name[it]=Editor Komposer +Name[ja]=Komposer エディタ +Name[kk]=Komposer өңдегіші +Name[km]=កម្មវិធី​និពន្ធ Komposer +Name[lt]=Komposer redaktorius +Name[ms]=Editor Komposer +Name[nb]=Komposer-redigering +Name[ne]=कमà¥à¤ªà¥‹à¤œà¤° समà¥à¤ªà¤¾à¤¦à¤• +Name[nl]=Komposer-editor +Name[nn]=Komposer-redigering +Name[pl]=Edytor Komposer +Name[pt]=Editor Kompositor +Name[pt_BR]=Editor do Komposer +Name[ru]=Редактор Komposer +Name[sk]=Editor Komposer +Name[sl]=Urejevalnik Komposer +Name[sr]=Уређивач Komposer-а +Name[sr@Latn]=UreÄ‘ivaÄ Komposer-a +Name[sv]=Komposer editor +Name[ta]=கமà¯à®ªà¯‹à®šà®°à¯ தொகà¯à®ªà¯à®ªà®¾à®©à¯ +Name[tr]=Komposer Düzenleyicisi +Name[uk]=Редактор Komposer +Name[zh_CN]=Komposer 编辑器 +Name[zh_TW]=Komposer 編輯器 +Comment=Komposer default editor +Comment[af]=Komposer standaard redigeerder +Comment[bg]=Подразбиращ Ñе редактор за Komposer +Comment[ca]=Editor predeterminat de Komposer +Comment[cs]=Výchozà Komposer editor +Comment[da]=Komposer standardeditor +Comment[de]=Komposer Standardeditor +Comment[el]=Î ÏοεπιλεγμÎνος επεξεÏγαστής του Komposer +Comment[eo]=Komposer-redaktilo apriora +Comment[es]=Editor predefinido Komposer +Comment[et]=Komposeri vaikeredaktor +Comment[eu]=Komposer editore lehenetsia +Comment[fa]=ویرایشگر پیش‌Ùرض Komposer +Comment[fi]=Komposer oletusmuokkain +Comment[fr]=Éditeur Komposer par défaut +Comment[fy]=Komposer standertbewurker +Comment[ga]=Eagarthóir réamhshocraithe Komposer +Comment[gl]=Editor por defecto Komposer +Comment[he]=עורך ברירת מחדל של Kompoer +Comment[hu]=A Komposer alapértelmezett szerkesztÅ‘je +Comment[is]=Sjálfgefinn ritill Komposer +Comment[it]=Editor di default per Komposer +Comment[ja]=Komposer 標準エディタ +Comment[kk]=Komposer әдетті өңдегіші +Comment[km]=កម្មវិធី​និពន្ធ​លំនាំដើម​របស់ Komposer +Comment[ko]=Komposer 기본 편집기 +Comment[lt]=Komposer numatytasis redaktorius +Comment[ms]=Pengedit piawai Komposer +Comment[nb]=Komposer standard-redigerer +Comment[nds]=Komposer-Standardeditor +Comment[ne]=कमà¥à¤ªà¥‹à¤œà¤°à¤•à¥‹ पूरà¥à¤µà¤¨à¤¿à¤°à¥à¤§à¤¾à¤°à¤¿à¤¤ समà¥à¤ªà¤¾à¤¦à¤• +Comment[nl]=Komposer standaardeditor +Comment[nn]=Komposer standard-redigeringsprogram +Comment[pl]=DomyÅ›lny edytor Komposera +Comment[pt]=Editor predefinido Kompositor +Comment[pt_BR]=Editor padrão do Komposer +Comment[ru]=Редактор Komposer по умолчанию +Comment[sk]=Å tandardný editor Komposer +Comment[sl]=Privzeti urejevalnik Komposer +Comment[sr]=Подразумевани Komposer-ов уређивач +Comment[sr@Latn]=Podrazumevani Komposer-ov ureÄ‘ivaÄ +Comment[sv]=Komposer standardeditor +Comment[ta]=கமà¯à®ªà¯‹à®šà®°à¯ à®®à¯à®©à¯à®©à®¿à®°à¯à®ªà¯à®ªà¯ தொகà¯à®ªà¯à®ªà®¾à®©à¯ +Comment[tr]=Öntanımlı Komposer düzenleyicisi +Comment[uk]=Типовий редактор Komposer +Comment[zh_CN]=Komposer 默认编辑器 +Comment[zh_TW]=Komposer é è¨ç·¨è¼¯å™¨ diff --git a/libtdepim/komposer/plugins/default/defaulteditor.h b/libtdepim/komposer/plugins/default/defaulteditor.h new file mode 100644 index 000000000..84cedcbc6 --- /dev/null +++ b/libtdepim/komposer/plugins/default/defaulteditor.h @@ -0,0 +1,118 @@ +/* + * defaulteditor.h + * + * Copyright (C) 2004 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef DEFAULTEDITOR_H +#define DEFAULTEDITOR_H + +#include "editor.h" + +class TQTextEdit; +class KFontAction; +class KFontSizeAction; +class KToggleAction; +class KActionCollection; + + +class DefaultEditor : public Komposer::Editor +{ + Q_OBJECT + TQ_OBJECT +public: + DefaultEditor( TQObject *parent, const char *name, const TQStringList &args ); + ~DefaultEditor(); + + virtual TQWidget *widget(); + virtual TQString text() const; +public slots: + virtual void setText( const TQString &txt ); + virtual void changeSignature( const TQString &txt ); + + /** + * Displays a file dialog and loads the selected file. + */ + bool open(); + + /** + * Displays a file dialog and saves to the selected file. + */ + bool saveAs(); + + /** + * Prints the current document + */ + bool print(); + + /** + * Displays a color dialog and sets the text color to the selected value. + */ + void formatColor(); + + void checkSpelling(); + + /** + * @internal + */ + void setAlignLeft( bool yes ); + + /** + * @internal + */ + void setAlignRight( bool yes ); + + /** + * @internal + */ + void setAlignCenter( bool yes ); + + /** + * @internal + */ + void setAlignJustify( bool yes ); + +protected slots: + /** + * Creates the part's actions in the part's action collection. + */ + void createActions( KActionCollection *ac ); + + void updateActions(); + + void updateFont(); + void updateCharFmt(); + void updateAligment(); + +private: + TQTextEdit *m_textEdit; + + KToggleAction *m_actionBold; + KToggleAction *m_actionItalic; + KToggleAction *m_actionUnderline; + + KFontAction *m_actionFont; + KFontSizeAction *m_actionFontSize; + + KToggleAction *m_actionAlignLeft; + KToggleAction *m_actionAlignRight; + KToggleAction *m_actionAlignCenter; + KToggleAction *m_actionAlignJustify; +}; + +#endif diff --git a/libtdepim/komposer/plugins/default/defaulteditorui.rc b/libtdepim/komposer/plugins/default/defaulteditorui.rc new file mode 100644 index 000000000..de8c2e6d2 --- /dev/null +++ b/libtdepim/komposer/plugins/default/defaulteditorui.rc @@ -0,0 +1,90 @@ +<!DOCTYPE kpartgui> +<kpartgui name="defaulteditor" version="3"> +<MenuBar> + <Menu name="edit"><text>&Edit</text> + <Action name="edit_undo"/> + <Action name="edit_redo"/> + <Separator/> + <Action name="edit_cut" append="edit_paste_merge"/> + <Action name="edit_copy" append="edit_paste_merge"/> + <Action name="edit_paste" append="edit_paste_merge"/> + <Action name="edit_clear" append="edit_paste_merge"/> + <Separator/> + <Action name="edit_select_all" append="edit_select_merge"/> + </Menu> + <Menu name="view"><text>&View</text> + <Action name="view_zoom_in" /> + <Action name="view_zoom_out" /> + <DefineGroup name="view_zoom_group" /> + </Menu> + <Menu name="format"><text>F&ormat</text> + <Action name="format_bold"/> + <Action name="format_italic"/> + <Action name="format_underline"/> + <Action name="format_color"/> + <DefineGroup name="format_chars_group" /> + <Separator/> + <Menu name="tqalignment"><text>&Alignment</text> + <Action name="format_align_left"/> + <Action name="format_align_center"/> + <Action name="format_align_right"/> + <Action name="format_align_justify"/> + <DefineGroup name="format_align_group" /> + </Menu> + <Separator/> + <Action name="format_font"/> + <Action name="format_font_size"/> + <DefineGroup name="format_font_group" /> + </Menu> + <Menu name="tools"><text>&Tools</text> + <Action name="tools_spelling"/> + </Menu> + <Merge /> + <Menu name="help"><text>&Help</text> + </Menu> +</MenuBar> +<ToolBar name="mainToolBar"><text>Editor Toolbar</text> + <Separator lineSeparator="true"/> + <Action name="edit_undo"/> + <Action name="edit_redo"/> + <Separator lineSeparator="true"/> + <Action name="edit_cut"/> + <Action name="edit_copy"/> + <Action name="edit_paste"/> + <Separator lineSeparator="true"/> + <Action name="view_zoom_in" /> + <Action name="view_zoom_out" /> +</ToolBar> +<ToolBar name="formatToolBar"><text>Format Toolbar</text> + <Action name="format_bold"/> + <Action name="format_italic"/> + <Action name="format_underline"/> + <Action name="format_color"/> + <DefineGroup name="format_chars_group" /> + <Separator lineSeparator="true"/> + <Action name="format_font"/> + <Action name="format_font_size"/> + <DefineGroup name="format_font_group" /> + <Separator lineSeparator="true"/> + <Action name="format_align_left"/> + <Action name="format_align_center"/> + <Action name="format_align_right"/> + <Action name="format_align_justify"/> + <DefineGroup name="format_align_group" /> +</ToolBar> + +<Menu name="editor_popup"> + <Action name="edit_undo"/> + <Action name="edit_redo"/> + <Separator/> + <Action name="edit_cut"/> + <Action name="edit_copy"/> + <Action name="edit_paste"/> + <Action name="edit_clear"/> + <Separator/> + <Action name="edit_select_all"/> +</Menu> + +</kpartgui> + + diff --git a/libtdepim/komposer/test/Makefile.am b/libtdepim/komposer/test/Makefile.am new file mode 100644 index 000000000..af51004e8 --- /dev/null +++ b/libtdepim/komposer/test/Makefile.am @@ -0,0 +1,7 @@ +AM_CPPFLAGS = -I$(top_srcdir) -I../core $(all_includes) +LDADD = $(LIB_KPARTS) ../core/libkomposer.la +AM_LDFLAGS = $(all_libraries) $(KDE_RPATH) $(LIB_QT) -lDCOP $(LIB_KDECORE) $(LIB_KDEUI) -ltdefx $(LIB_KIO) -lktexteditor + +check_PROGRAMS = test + +test_SOURCES = test.cpp diff --git a/libtdepim/komposer/test/test.cpp b/libtdepim/komposer/test/test.cpp new file mode 100644 index 000000000..5d4544d2a --- /dev/null +++ b/libtdepim/komposer/test/test.cpp @@ -0,0 +1,56 @@ +/** + * test.cpp + * + * Copyright (C) 2003 Zack Rusin <zack@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include "core.h" + +#include <kaboutdata.h> +#include <kcmdlineargs.h> +#include <kiconloader.h> +#include <klocale.h> +#include <kuniqueapplication.h> + +#include <tqlabel.h> + +static const char description[] = + I18N_NOOP( "KDE mail editing manager" ); + +static const char version[] = "0.0.1 (SVN)"; + +int main(int argc, char **argv) +{ + KAboutData about( "komposertest", I18N_NOOP( "KomposerTest" ), version, description, + KAboutData::License_GPL, "(C) 2001-2003 The Kontact developers", 0, "http://kontact.kde.org", "zack@kde.org" ); + about.addAuthor( "Zack Rusin", 0, "zack@kde.org" ); + + KCmdLineArgs::init( argc, argv, &about ); + KUniqueApplication app; + + // see if we are starting with session management + if ( app.isRestored() ) + RESTORE( Komposer::Core ) + else { + // no session.. just start up normally + Komposer::Core *mw = new Komposer::Core; + mw->show(); + } + + return app.exec(); +} diff --git a/libtdepim/kpartsdesignerplugin/CMakeLists.txt b/libtdepim/kpartsdesignerplugin/CMakeLists.txt new file mode 100644 index 000000000..55c75251c --- /dev/null +++ b/libtdepim/kpartsdesignerplugin/CMakeLists.txt @@ -0,0 +1,32 @@ +################################################# +# +# (C) 2010-2011 Serghei Amelian +# serghei (DOT) amelian (AT) gmail.com +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +add_definitions( -DQT_PLUGIN ) + +include_directories( + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_SOURCE_DIR}/libtdepim + ${TDE_INCLUDE_DIR} + ${TQT_INCLUDE_DIRS} +) + +link_directories( + ${TQT_LIBRARY_DIRS} +) + + +##### kpartsdesignerplugin (module) ############# + +tde_add_kpart( kpartsdesignerplugin AUTOMOC + SOURCES kpartsdesignerplugin.cpp + LINK kparts-shared kio-shared + DESTINATION ${PLUGIN_INSTALL_DIR}/plugins/designer +) diff --git a/libtdepim/kpartsdesignerplugin/Makefile.am b/libtdepim/kpartsdesignerplugin/Makefile.am new file mode 100644 index 000000000..ebbfbfba2 --- /dev/null +++ b/libtdepim/kpartsdesignerplugin/Makefile.am @@ -0,0 +1,8 @@ +AM_CXXFLAGS = -DQT_PLUGIN +METASOURCES = AUTO +INCLUDES = $(all_includes) + +kde_widget_LTLIBRARIES = kpartsdesignerplugin.la +kpartsdesignerplugin_la_LDFLAGS = $(KDE_PLUGIN) -module $(all_libraries) $(LIB_QT) -ltdecore -lkio +kpartsdesignerplugin_la_LIBADD = $(LIB_KPARTS) +kpartsdesignerplugin_la_SOURCES = kpartsdesignerplugin.cpp diff --git a/libtdepim/kpartsdesignerplugin/README b/libtdepim/kpartsdesignerplugin/README new file mode 100644 index 000000000..fb9f42837 --- /dev/null +++ b/libtdepim/kpartsdesignerplugin/README @@ -0,0 +1,2 @@ +When moving this to kdelibs: the KPartsGenericPart class should move to libkparts so +that it's possible to add an accessor for m_part there. diff --git a/libtdepim/kpartsdesignerplugin/kpartsdesignerplugin.cpp b/libtdepim/kpartsdesignerplugin/kpartsdesignerplugin.cpp new file mode 100644 index 000000000..c3eedc329 --- /dev/null +++ b/libtdepim/kpartsdesignerplugin/kpartsdesignerplugin.cpp @@ -0,0 +1,117 @@ +/* + Copyright (C) 2004, David Faure <faure@kde.org> + This file is part of the KDE project + + 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 "kpartsdesignerplugin.h" + +#include <kparts/componentfactory.h> +#include <kparts/part.h> +#include <kmimetype.h> +#include <tqlayout.h> +#include <kapplication.h> +#include <tdepimmacros.h> + +KPartsGenericPart::KPartsGenericPart( TQWidget* parentWidget, const char* name ) + : TQWidget( parentWidget, name ), m_part( 0 ) +{ + TQVBoxLayout* tqlayout = new TQVBoxLayout( this ); + tqlayout->setAutoAdd( true ); +} + +void KPartsGenericPart::load() +{ + if ( m_mimetype.isEmpty() || m_url.isEmpty() ) + return; // not enough info yet + // Here it crashes in KSycoca::openDatabase when trying to load the stuff from designer itself + // Not sure why - but it's not really needed anyway. + if ( !kapp ) + return; + TQString mimetype = m_mimetype; + if ( mimetype == "auto" ) + mimetype == KMimeType::findByURL( m_url )->name(); + if ( m_part ) { + delete m_part; + } + // "this" is both the parent widget and the parent object + m_part = KParts::ComponentFactory::createPartInstanceFromQuery<KParts::ReadOnlyPart>( mimetype, TQString(), TQT_TQWIDGET(this), 0, TQT_TQOBJECT(this), 0 ); + if ( m_part ) { + m_part->openURL( m_url ); + m_part->widget()->show(); + } +} + +//// + +static const char* mykey = "KPartsGenericPart"; + +TQStringList KPartsWidgetPlugin::keys() const { + return TQStringList() << mykey; +} + +TQWidget * KPartsWidgetPlugin::create( const TQString & key, TQWidget * parent, const char * name ) { + if ( key == mykey ) + return new KPartsGenericPart( parent, name ); + return 0; +} + +TQString KPartsWidgetPlugin::group( const TQString & key ) const { + if ( key == mykey ) + return "Display (KDE)"; + return TQString(); +} + +#if 0 +TQIconSet KPartsWidgetPlugin::iconSet( const TQString & key ) const { + return TQIconSet(); +} +#endif + +TQString KPartsWidgetPlugin::includeFile( const TQString & key ) const { + if ( key == mykey ) + return "partplugin.h"; + return TQString(); +} + +TQString KPartsWidgetPlugin::toolTip( const TQString & key ) const { + if ( key == mykey ) + return "Generic KParts viewer"; + return TQString(); +} + +TQString KPartsWidgetPlugin::whatsThis( const TQString & key ) const { + if ( key == mykey ) + return "A widget to embed any KParts viewer, given a url and optionally a mimetype"; + return TQString(); +} + +bool KPartsWidgetPlugin::isContainer( const TQString & /*key*/ ) const { + return false; +} + +/// Duplicated from kdelibs/tdecore/kdemacros.h.in for those with kdelibs < 3.4 +#ifndef KDE_TQ_EXPORT_PLUGIN +#define KDE_TQ_EXPORT_PLUGIN(PLUGIN) \ + TQ_EXTERN_C KDE_EXPORT const char* qt_ucm_query_verification_data(); \ + TQ_EXTERN_C KDE_EXPORT TQUnknownInterface* ucm_instantiate(); \ + TQ_EXPORT_PLUGIN(PLUGIN) +#endif + +KDE_TQ_EXPORT_PLUGIN( KPartsWidgetPlugin ) + +#include "kpartsdesignerplugin.moc" + diff --git a/libtdepim/kpartsdesignerplugin/kpartsdesignerplugin.h b/libtdepim/kpartsdesignerplugin/kpartsdesignerplugin.h new file mode 100644 index 000000000..fc49f2511 --- /dev/null +++ b/libtdepim/kpartsdesignerplugin/kpartsdesignerplugin.h @@ -0,0 +1,71 @@ +/* + Copyright (C) 2005, David Faure <faure@kde.org> + This file is part of the KDE project + + 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. +*/ + + +#ifndef DESIGNER_PARTPLUGIN_H +#define DESIGNER_PARTPLUGIN_H + +#include <tqwidgetplugin.h> +#include <tqwidget.h> +namespace KParts { class ReadOnlyPart; } + +/** + * Generic part loader, able to view any kind of file for which + * a KParts::ReadOnlyPart is available + */ +class KPartsGenericPart : public TQWidget { + Q_OBJECT + TQ_OBJECT + TQ_PROPERTY( TQString url READ url WRITE setURL ) + TQ_PROPERTY( TQString mimetype READ mimetype WRITE setMimetype ) +public: + KPartsGenericPart( TQWidget* parentWidget, const char* name ); + + TQString url() const { return m_url; } + void setURL( const TQString& url ) { m_url = url; load(); } + + // The mimetype, or "auto" if unknown + TQString mimetype() const { return m_mimetype; } + void setMimetype( const TQString& mimetype ) { m_mimetype = mimetype; load(); } + +private: + void load(); + +private: + TQString m_mimetype; + TQString m_url; + KParts::ReadOnlyPart* m_part; +}; + +/** + * TQt designer plugin for embedding a KParts using KPartsGenericPart + */ +class KPartsWidgetPlugin : public TQWidgetPlugin { +public: + TQStringList keys() const; + TQWidget * create( const TQString & key, TQWidget * parent, const char * name ); + TQString group( const TQString & key ) const; + //TQIconSet iconSet( const TQString & key ) const; + TQString includeFile( const TQString & key ) const; + TQString toolTip( const TQString & key ) const; + TQString whatsThis( const TQString & key ) const; + bool isContainer( const TQString & key ) const; +}; + +#endif diff --git a/libtdepim/kpimprefs.cpp b/libtdepim/kpimprefs.cpp new file mode 100644 index 000000000..478ed7047 --- /dev/null +++ b/libtdepim/kpimprefs.cpp @@ -0,0 +1,187 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2002 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <config.h> + +#include <time.h> +#include <unistd.h> +#include <stdlib.h> + +#include <tqstring.h> + +#include <kstandarddirs.h> +#include <kglobal.h> +#include <kconfig.h> +#include <klocale.h> +#include <kdebug.h> + +#include "kpimprefs.h" + +KPimPrefs::KPimPrefs( const TQString &name ) + : KConfigSkeleton( name ) +{ +} + +KPimPrefs::~KPimPrefs() +{ +} + +void KPimPrefs::usrSetDefaults() +{ + setCategoryDefaults(); +} + +void KPimPrefs::usrReadConfig() +{ + kdDebug(5300) << "KPimPrefs::usrReadConfig()" << endl; + + config()->setGroup("General"); + mCustomCategories = config()->readListEntry( "Custom Categories" ); + if ( mCustomCategories.isEmpty() ) setCategoryDefaults(); + mCustomCategories.sort(); +} + +const TQString KPimPrefs::timezone() +{ + TQString zone = ""; + + // Read TimeZoneId from korganizerrc. + KConfig korgcfg( locate( "config", "korganizerrc" ) ); + korgcfg.setGroup( "Time & Date" ); + TQString tz( korgcfg.readEntry( "TimeZoneId" ) ); + if ( !tz.isEmpty() ) { + zone = tz; + kdDebug(5300) << "timezone from korganizerrc is " << zone << endl; + } + + // If timezone not found in KOrg, use the system's default timezone. + if ( zone.isEmpty() ) { + char zonefilebuf[ PATH_MAX ]; + + int len = readlink( "/etc/localtime", zonefilebuf, PATH_MAX ); + if ( len > 0 && len < PATH_MAX ) { + zone = TQString::fromLocal8Bit( zonefilebuf, len ); + zone = zone.mid( zone.find( "zoneinfo/" ) + 9 ); + kdDebug(5300) << "system timezone from /etc/localtime is " << zone + << endl; + } else { + tzset(); + zone = tzname[ 0 ]; + kdDebug(5300) << "system timezone from tzset() is " << zone << endl; + } + } + + return( zone ); +} + +TQDateTime KPimPrefs::utcToLocalTime( const TQDateTime &_dt, + const TQString &timeZoneId ) +{ + TQDateTime dt(_dt); +// kdDebug() << "--- UTC: " << dt.toString() << endl; + + int yearCorrection = 0; + // The timezone conversion only works for dates > 1970 + // For dates < 1970 we adjust the date to be in 1970, + // do the correction there and then re-adjust back. + // Actually, we use 1971 to prevent errors around + // January 1, 1970 + int year = dt.date().year(); + if (year < 1971) + { + yearCorrection = 1971 - year; + dt = dt.addYears(yearCorrection); +// kdDebug() << "--- Adjusted UTC: " << dt.toString() << endl; + } + + TQCString origTz = getenv("TZ"); + + setenv( "TZ", "UTC", 1 ); + time_t utcTime = dt.toTime_t(); + + setenv( "TZ", timeZoneId.local8Bit(), 1 ); + struct tm *local = localtime( &utcTime ); + + if ( origTz.isNull() ) { + unsetenv( "TZ" ); + } else { + setenv( "TZ", origTz, 1 ); + } + tzset(); + + TQDateTime result( TQDate( local->tm_year + 1900 - yearCorrection, + local->tm_mon + 1, local->tm_mday ), + TQTime( local->tm_hour, local->tm_min, local->tm_sec ) ); + +// kdDebug() << "--- LOCAL: " << result.toString() << endl; + return result; +} + +TQDateTime KPimPrefs::localTimeToUtc( const TQDateTime &_dt, + const TQString &timeZoneId ) +{ + TQDateTime dt(_dt); +// kdDebug() << "--- LOCAL: " << dt.toString() << endl; + + int yearCorrection = 0; + // The timezone conversion only works for dates > 1970 + // For dates < 1970 we adjust the date to be in 1970, + // do the correction there and then re-adjust back. + // Actually, we use 1971 to prevent errors around + // January 1, 1970 + + int year = dt.date().year(); + if (year < 1971) + { + yearCorrection = 1971 - year; + dt = dt.addYears(yearCorrection); +// kdDebug() << "--- Adjusted LOCAL: " << dt.toString() << endl; + } + + TQCString origTz = getenv("TZ"); + + setenv( "TZ", timeZoneId.local8Bit(), 1 ); + time_t localTime = dt.toTime_t(); + + setenv( "TZ", "UTC", 1 ); + struct tm *utc = gmtime( &localTime ); + + if ( origTz.isNull() ) { + unsetenv( "TZ" ); + } else { + setenv( "TZ", origTz, 1 ); + } + tzset(); + + TQDateTime result( TQDate( utc->tm_year + 1900 - yearCorrection, + utc->tm_mon + 1, utc->tm_mday ), + TQTime( utc->tm_hour, utc->tm_min, utc->tm_sec ) ); + +// kdDebug() << "--- UTC: " << result.toString() << endl; + + return result; +} + +void KPimPrefs::usrWriteConfig() +{ + config()->setGroup( "General" ); + config()->writeEntry( "Custom Categories", mCustomCategories ); +} diff --git a/libtdepim/kpimprefs.h b/libtdepim/kpimprefs.h new file mode 100644 index 000000000..1d88a8d8f --- /dev/null +++ b/libtdepim/kpimprefs.h @@ -0,0 +1,86 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2002 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef KPIMPREFS_H +#define KPIMPREFS_H + +#include <tqstringlist.h> + +#include <kconfigskeleton.h> +#include <tdepimmacros.h> + +class TQString; + +class KDE_EXPORT KPimPrefs : public KConfigSkeleton +{ + public: + KPimPrefs( const TQString &name = TQString() ); + + virtual ~KPimPrefs(); + + /** Set preferences to default values */ + void usrSetDefaults(); + + /** Read preferences from config file */ + void usrReadConfig(); + + /** Write preferences to config file */ + void usrWriteConfig(); + + /** + * Get user's timezone. + * + * This will first look for whatever timezone is stored in KOrganizer's + * configuration file. If no timezone is found there, it uses + * /etc/localtime. + * + * The value returned may be in various formats (for example, + * America/New_York or EST) so your program should be prepared to these + * formats. + * + * The Calendar class in libkcal says accepts all timezone codes that are + * listed in /usr/share/zoneinfo/zone.tab. + * + * @see Calendar + */ + static const TQString timezone(); + + /** + Convert time given in UTC to local time at timezone specified by given + timezone id. + */ + static TQDateTime utcToLocalTime( const TQDateTime &dt, + const TQString &timeZoneId ); + + /** + Convert time given in local time at timezone specified by given + timezone id to UTC. + */ + static TQDateTime localTimeToUtc( const TQDateTime &dt, + const TQString &timeZoneId ); + + public: + TQStringList mCustomCategories; + + protected: + virtual void setCategoryDefaults() {} +}; + +#endif diff --git a/libtdepim/kpimurlrequesterdlg.cpp b/libtdepim/kpimurlrequesterdlg.cpp new file mode 100644 index 000000000..553ca659f --- /dev/null +++ b/libtdepim/kpimurlrequesterdlg.cpp @@ -0,0 +1,41 @@ +/* This file is part of the KDE libraries + Copyright (C) 2000 Wilco Greven <greven@kde.org> + + library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <kpimurlrequesterdlg.h> +#include <krecentdocument.h> +#include <kurlrequesterdlg.h> +#include <klocale.h> + +// KDE3.4 or KDE4.0: FIXME: Move to kdelibs! +KURL KPimURLRequesterDlg::getURL( const TQString& dir, const TQString &label, + TQWidget *parent, const TQString& caption ) +{ + KURLRequesterDlg dlg(dir, label, parent, "filedialog", true); + + dlg.setCaption(caption.isNull() ? i18n("Open") : caption); + + dlg.exec(); + + const KURL& url = dlg.selectedURL(); + if (url.isValid()) + KRecentDocument::add(url); + + return url; +} diff --git a/libtdepim/kpimurlrequesterdlg.h b/libtdepim/kpimurlrequesterdlg.h new file mode 100644 index 000000000..6a00cf4ef --- /dev/null +++ b/libtdepim/kpimurlrequesterdlg.h @@ -0,0 +1,47 @@ +/* This file is part of the KDE libraries + Copyright (C) 2000 Wilco Greven <greven@kde.org> + + library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef KPIMURLREQUESTDLG_H_INCLUDED +#define KPIMURLREQUESTDLG_H_INCLUDED + +#include <tdepimmacros.h> +#include <kurl.h> +#include <tqstring.h> + +class TQWidget; + +class KDE_EXPORT KPimURLRequesterDlg +{ + public: + /** + * Creates a modal dialog with the given label text, executes it and + * returns the selected URL. + * + * @param url This specifies the initial path of the input line. + * @param text The text to be shown on the label. + * @param parent The widget the dialog will be centered on initially. + */ + static KURL getURL( const TQString &url = TQString(), + const TQString &text = TQString(), + TQWidget *parent = 0, + const TQString &caption = TQString() ); +}; + +#endif // KPIMURLREQUESTDLG_H_INCLUDED + diff --git a/libtdepim/kpixmapregionselectordialog.cpp b/libtdepim/kpixmapregionselectordialog.cpp new file mode 100644 index 000000000..b715e000b --- /dev/null +++ b/libtdepim/kpixmapregionselectordialog.cpp @@ -0,0 +1,131 @@ +/* + This file is part of libtdepim. + + Copyright (C) 2004 Antonio Larrosa <larrosa@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "kpixmapregionselectordialog.h" +#include <kdialogbase.h> +#include <tqdialog.h> +#include <tqdesktopwidget.h> +#include <klocale.h> +#include <kdialog.h> + +using namespace KPIM; + +KPixmapRegionSelectorDialog::KPixmapRegionSelectorDialog(TQWidget *parent, + const char *name, bool modal ) : KDialogBase(parent, name, modal, i18n("Select Region of Image"), Help|Ok|Cancel, Ok, true ) +{ + TQVBox *vbox=new TQVBox(this); + new TQLabel(i18n("Please click and drag on the image to select the region of interest:"), vbox); + m_pixmapSelectorWidget= new KPixmapRegionSelectorWidget(vbox); + + vbox->setSpacing( KDialog::spacingHint() ); + + setMainWidget(vbox); +} + +KPixmapRegionSelectorDialog::~KPixmapRegionSelectorDialog() +{ +} + +TQRect KPixmapRegionSelectorDialog::getSelectedRegion(const TQPixmap &pixmap, TQWidget *parent ) +{ + KPixmapRegionSelectorDialog dialog(parent); + + dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap); + + TQDesktopWidget desktopWidget; + TQRect screen=desktopWidget.availableGeometry(); + dialog.pixmapRegionSelectorWidget()->setMaximumWidgetSize( + (int)(screen.width()*4.0/5), (int)(screen.height()*4.0/5)); + + int result = dialog.exec(); + + TQRect rect; + + if ( result == TQDialog::Accepted ) + rect = dialog.pixmapRegionSelectorWidget()->unzoomedSelectedRegion(); + + return rect; +} + +TQRect KPixmapRegionSelectorDialog::getSelectedRegion(const TQPixmap &pixmap, int aspectRatioWidth, int aspectRatioHeight, TQWidget *parent ) +{ + KPixmapRegionSelectorDialog dialog(parent); + + dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap); + dialog.pixmapRegionSelectorWidget()->setSelectionAspectRatio(aspectRatioWidth,aspectRatioHeight); + + TQDesktopWidget desktopWidget; + TQRect screen=desktopWidget.availableGeometry(); + dialog.pixmapRegionSelectorWidget()->setMaximumWidgetSize( + (int)(screen.width()*4.0/5), (int)(screen.height()*4.0/5)); + + int result = dialog.exec(); + + TQRect rect; + + if ( result == TQDialog::Accepted ) + rect = dialog.pixmapRegionSelectorWidget()->unzoomedSelectedRegion(); + + return rect; +} + +TQImage KPixmapRegionSelectorDialog::getSelectedImage(const TQPixmap &pixmap, TQWidget *parent ) +{ + KPixmapRegionSelectorDialog dialog(parent); + + dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap); + + TQDesktopWidget desktopWidget; + TQRect screen=desktopWidget.availableGeometry(); + dialog.pixmapRegionSelectorWidget()->setMaximumWidgetSize( + (int)(screen.width()*4.0/5), (int)(screen.height()*4.0/5)); + int result = dialog.exec(); + + TQImage image; + + if ( result == TQDialog::Accepted ) + image = dialog.pixmapRegionSelectorWidget()->selectedImage(); + + return image; +} + +TQImage KPixmapRegionSelectorDialog::getSelectedImage(const TQPixmap &pixmap, int aspectRatioWidth, int aspectRatioHeight, TQWidget *parent ) +{ + KPixmapRegionSelectorDialog dialog(parent); + + dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap); + dialog.pixmapRegionSelectorWidget()->setSelectionAspectRatio(aspectRatioWidth,aspectRatioHeight); + + TQDesktopWidget desktopWidget; + TQRect screen=desktopWidget.availableGeometry(); + dialog.pixmapRegionSelectorWidget()->setMaximumWidgetSize( + (int)(screen.width()*4.0/5), (int)(screen.height()*4.0/5)); + + int result = dialog.exec(); + + TQImage image; + + if ( result == TQDialog::Accepted ) + image = dialog.pixmapRegionSelectorWidget()->selectedImage(); + + return image; +} + diff --git a/libtdepim/kpixmapregionselectordialog.h b/libtdepim/kpixmapregionselectordialog.h new file mode 100644 index 000000000..ee864391a --- /dev/null +++ b/libtdepim/kpixmapregionselectordialog.h @@ -0,0 +1,112 @@ +/* + This file is part of libtdepim. + + Copyright (C) 2004 Antonio Larrosa <larrosa@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef __KPIXMAPREGIONSELECTORDIALOG_H__ +#define __KPIXMAPREGIONSELECTORDIALOG_H__ + +#include <kdialogbase.h> +#include <libtdepim/kpixmapregionselectorwidget.h> +#include <tqimage.h> +#include <tdepimmacros.h> + +namespace KPIM { + +/** + * A dialog that uses a KPixmapRegionSelectorWidget to allow the user + * to select a region of an image. If you want to use special features + * like forcing the selected area to have a fixed aspect ratio, you can use + * @see pixmapRegionSelectorWidget() to get the pointer to the + * pixmapRegionSelectorWidget object and set the desired options there. + * + * There are some convenience methods that allow to easily show a dialog + * for the user to select a region of an image, and just care about the selected + * image. + * + * @author Antonio Larrosa <larrosa@kde.org> + * @since 3.4 + */ +class KDE_EXPORT KPixmapRegionSelectorDialog : public KDialogBase +{ +public: + /** + * The constructor of an empty KPixmapRegionSelectorDialog, you have to call + * later the setPixmap method of the KPixmapRegionSelectorWidget widget of + * the new object. + */ + KPixmapRegionSelectorDialog(TQWidget *parent=0L, const char *name=0L, + bool modal = false ); + /** + * The destructor of the dialog + */ + ~KPixmapRegionSelectorDialog(); + + /** + * @returns the KPixmapRegionSelectorWidget widget so that additional + * parameters can be set by using it. + */ + KPIM::KPixmapRegionSelectorWidget *pixmapRegionSelectorWidget() const + { return m_pixmapSelectorWidget; } + + /** + * Creates a modal dialog, lets the user to select a region of the @p pixmap + * and returns when the dialog is closed. + * + * @returns the selected rectangle, or an invalid rectangle if the user + * pressed the Cancel button. + */ + static TQRect getSelectedRegion(const TQPixmap &pixmap, TQWidget *parent = 0L ); + + /** + * Creates a modal dialog, lets the user to select a region of the @p pixmap + * with the same aspect ratio than @p aspectRatioWidth x @p aspectRatioHeight + * and returns when the dialog is closed. + * + * @returns the selected rectangle, or an invalid rectangle if the user + * pressed the Cancel button. + */ + static TQRect getSelectedRegion(const TQPixmap &pixmap, int aspectRatioWidth, int aspectRatioHeight, TQWidget *parent = 0L ); + + /** + * Creates a modal dialog, lets the user to select a region of the @p pixmap + * and returns when the dialog is closed. + * + * @returns the selected image, or an invalid image if the user + * pressed the Cancel button. + */ + static TQImage getSelectedImage(const TQPixmap &pixmap, TQWidget *parent = 0L ); + + /** + * Creates a modal dialog, lets the user to select a region of the @p pixmap + * with the same aspect ratio than @p aspectRatioWidth x @p aspectRatioHeight + * and returns when the dialog is closed. + * + * @returns the selected image, or an invalid image if the user + * pressed the Cancel button. + */ + static TQImage getSelectedImage(const TQPixmap &pixmap, int aspectRatioWidth, int aspectRatioHeight, TQWidget *parent = 0L ); + +protected: + KPIM::KPixmapRegionSelectorWidget *m_pixmapSelectorWidget; +}; + +} + +#endif diff --git a/libtdepim/kpixmapregionselectorwidget.cpp b/libtdepim/kpixmapregionselectorwidget.cpp new file mode 100644 index 000000000..d687ae8a1 --- /dev/null +++ b/libtdepim/kpixmapregionselectorwidget.cpp @@ -0,0 +1,454 @@ +/* + This file is part of libtdepim. + + Copyright (C) 2004 Antonio Larrosa <larrosa@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +/* NOTE: There are two copies of this .h and the .cpp file, with subtle differences. + * One copy is in kdelibs/tdeui, and the other copy is in tdepim/libtdepim + * This is because tdepim has to remain backwards compatible. Any changes + * to either file should be made to the other. + */ + +#include "kpixmapregionselectorwidget.h" +#include <tqpainter.h> +#include <tqcolor.h> +#include <tqimage.h> +#include <tqlayout.h> +#include <kimageeffect.h> +#include <kdebug.h> +#include <klocale.h> +#include <kpopupmenu.h> +#include <kaction.h> +#include <stdlib.h> +#include <tqcursor.h> +#include <tqapplication.h> + +using namespace KPIM; + +KPixmapRegionSelectorWidget::KPixmapRegionSelectorWidget( TQWidget *parent, + const char *name) : TQWidget( parent, name) +{ + TQHBoxLayout * hboxLayout=new TQHBoxLayout( this ); + + hboxLayout->addStretch(); + TQVBoxLayout * vboxLayout=new TQVBoxLayout( hboxLayout ); + + vboxLayout->addStretch(); + m_label = new TQLabel(this, "pixmapHolder"); + m_label->setBackgroundMode( TQt::NoBackground ); + m_label->installEventFilter( this ); + + vboxLayout->addWidget(m_label); + vboxLayout->addStretch(); + + hboxLayout->addStretch(); + + m_forcedAspectRatio=0; + + m_zoomFactor=1.0; +} + +KPixmapRegionSelectorWidget::~KPixmapRegionSelectorWidget() +{ +} + +void KPixmapRegionSelectorWidget::setPixmap( const TQPixmap &pixmap ) +{ + Q_ASSERT(!pixmap.isNull()); //This class isn't designed to deal with null pixmaps. + m_originalPixmap = pixmap; + m_unzoomedPixmap = pixmap; + m_label->setPixmap( pixmap ); + resetSelection(); +} + +void KPixmapRegionSelectorWidget::resetSelection() +{ + m_selectedRegion = m_originalPixmap.rect(); + updatePixmap(); +} + +TQRect KPixmapRegionSelectorWidget::selectedRegion() const +{ + return m_selectedRegion; +} + +void KPixmapRegionSelectorWidget::setSelectedRegion(const TQRect &rect) +{ + if (!rect.isValid()) resetSelection(); + else + { + m_selectedRegion=rect; + updatePixmap(); + + TQRect r=unzoomedSelectedRegion(); + } +} + +void KPixmapRegionSelectorWidget::updatePixmap() +{ + Q_ASSERT(!m_originalPixmap.isNull()); if(m_originalPixmap.isNull()) { m_label->setPixmap(m_originalPixmap); return; } + if (m_selectedRegion.width()>m_originalPixmap.width()) m_selectedRegion.setWidth( m_originalPixmap.width() ); + if (m_selectedRegion.height()>m_originalPixmap.height()) m_selectedRegion.setHeight( m_originalPixmap.height() ); + + TQPainter painter; + if (m_linedPixmap.isNull()) + { + m_linedPixmap = m_originalPixmap; + + painter.begin(&m_linedPixmap); + painter.setRasterOp( TQt::XorROP ); + painter.fillRect(0,0,m_linedPixmap.width(), m_linedPixmap.height(), + TQBrush( TQColor(255,255,255), TQt::BDiagPattern) ); + painter.end(); + + TQImage image=m_linedPixmap.convertToImage(); + image=KImageEffect::fade(image, 0.4, TQColor(0,0,0)); + m_linedPixmap.convertFromImage(image); + } + + TQPixmap pixmap = m_linedPixmap; + + painter.begin(&pixmap); + painter.drawPixmap( m_selectedRegion.topLeft(), + m_originalPixmap, m_selectedRegion ); + + painter.setPen( TQColor(255,255,255) ); + painter.setRasterOp( TQt::XorROP ); + + painter.drawRect( m_selectedRegion ); + + painter.end(); + + m_label->setPixmap(pixmap); +} + +KPopupMenu *KPixmapRegionSelectorWidget::createPopupMenu() +{ + KPopupMenu *popup=new KPopupMenu(this, "PixmapRegionSelectorPopup"); + popup->insertTitle(i18n("Image Operations")); + + KAction *action = new KAction(i18n("&Rotate Clockwise"), "rotate_cw", + 0, TQT_TQOBJECT(this), TQT_SLOT(rotateClockwise()), + TQT_TQOBJECT(popup), "rotateclockwise"); + action->plug(popup); + + action = new KAction(i18n("Rotate &Counterclockwise"), "rotate_ccw", + 0, TQT_TQOBJECT(this), TQT_SLOT(rotateCounterclockwise()), + TQT_TQOBJECT(popup), "rotatecounterclockwise"); + action->plug(popup); + +/* + I wonder if it would be appropiate to have here an "Open with..." option to + edit the image (antlarr) +*/ + return popup; +} + +void KPixmapRegionSelectorWidget::rotate(KImageEffect::RotateDirection direction) +{ + int w=m_originalPixmap.width(); + int h=m_originalPixmap.height(); + TQImage img=m_unzoomedPixmap.convertToImage(); + img= KImageEffect::rotate(img, direction); + m_unzoomedPixmap.convertFromImage(img); + + img=m_originalPixmap.convertToImage(); + img= KImageEffect::rotate(img, direction); + m_originalPixmap.convertFromImage(img); + + m_linedPixmap=TQPixmap(); + + if (m_forcedAspectRatio>0 && m_forcedAspectRatio!=1) + resetSelection(); + else + { + switch (direction) + { + case ( KImageEffect::Rotate90 ): + { + int x=h-m_selectedRegion.y()-m_selectedRegion.height(); + int y=m_selectedRegion.x(); + m_selectedRegion.setRect(x, y, m_selectedRegion.height(), m_selectedRegion.width() ); + updatePixmap(); + } break; + case ( KImageEffect::Rotate270 ): + { + int x=m_selectedRegion.y(); + int y=w-m_selectedRegion.x()-m_selectedRegion.width(); + m_selectedRegion.setRect(x, y, m_selectedRegion.height(), m_selectedRegion.width() ); + updatePixmap(); + } break; + default: resetSelection(); + } + } +} + +void KPixmapRegionSelectorWidget::rotateClockwise() +{ + rotate(KImageEffect::Rotate90); +} + +void KPixmapRegionSelectorWidget::rotateCounterclockwise() +{ + rotate(KImageEffect::Rotate270); +} + + + +bool KPixmapRegionSelectorWidget::eventFilter(TQObject *obj, TQEvent *ev) +{ + if ( ev->type() == TQEvent::MouseButtonPress ) + { + TQMouseEvent *mev= (TQMouseEvent *)(ev); + //kdDebug() << TQString("click at %1,%2").tqarg( mev->x() ).tqarg( mev->y() ) << endl; + + if ( mev->button() == Qt::RightButton ) + { + KPopupMenu *popup = createPopupMenu( ); + popup->exec( mev->globalPos() ); + delete popup; + return TRUE; + }; + + TQCursor cursor; + if ( m_selectedRegion.contains( mev->pos() ) + && m_selectedRegion!=m_originalPixmap.rect() ) + { + m_state=Moving; + cursor=TQCursor(TQt::SizeAllCursor); + } + else + { + m_state=Resizing; + cursor=TQCursor(TQt::CrossCursor); + } + TQApplication::setOverrideCursor(cursor); + + m_tempFirstClick=mev->pos(); + + return TRUE; + } + + if ( ev->type() == TQEvent::MouseMove ) + { + TQMouseEvent *mev= (TQMouseEvent *)(ev); + + //kdDebug() << TQString("move to %1,%2").tqarg( mev->x() ).tqarg( mev->y() ) << endl; + + if ( m_state == Resizing ) + { + setSelectedRegion ( + calcSelectionRectangle( m_tempFirstClick, mev->pos() ) ); + } + else if (m_state == Moving ) + { + int mevx = mev->x(); + int mevy = mev->y(); + bool mouseOutside=false; + if ( mevx < 0 ) + { + m_selectedRegion.moveBy(-m_selectedRegion.x(),0); + mouseOutside=true; + } + else if ( mevx > m_originalPixmap.width() ) + { + m_selectedRegion.moveBy(m_originalPixmap.width()-m_selectedRegion.width()-m_selectedRegion.x(),0); + mouseOutside=true; + } + if ( mevy < 0 ) + { + m_selectedRegion.moveBy(0,-m_selectedRegion.y()); + mouseOutside=true; + } + else if ( mevy > m_originalPixmap.height() ) + { + m_selectedRegion.moveBy(0,m_originalPixmap.height()-m_selectedRegion.height()-m_selectedRegion.y()); + mouseOutside=true; + } + if (mouseOutside) { updatePixmap(); return TRUE; }; + + m_selectedRegion.moveBy( mev->x()-m_tempFirstClick.x(), + mev->y()-m_tempFirstClick.y() ); + + // Check that the region has not fallen outside the image + if (m_selectedRegion.x() < 0) + m_selectedRegion.moveBy(-m_selectedRegion.x(),0); + else if (m_selectedRegion.right() > m_originalPixmap.width()) + m_selectedRegion.moveBy(-(m_selectedRegion.right()-m_originalPixmap.width()),0); + + if (m_selectedRegion.y() < 0) + m_selectedRegion.moveBy(0,-m_selectedRegion.y()); + else if (m_selectedRegion.bottom() > m_originalPixmap.height()) + m_selectedRegion.moveBy(0,-(m_selectedRegion.bottom()-m_originalPixmap.height())); + + m_tempFirstClick=mev->pos(); + updatePixmap(); + } + return TRUE; + } + + if ( ev->type() == TQEvent::MouseButtonRelease ) + { + TQMouseEvent *mev= (TQMouseEvent *)(ev); + + if ( m_state == Resizing && mev->pos() == m_tempFirstClick) + resetSelection(); + + m_state=None; + TQApplication::restoreOverrideCursor(); + + return TRUE; + } + + TQWidget::eventFilter(obj, ev); + return FALSE; +} + +TQRect KPixmapRegionSelectorWidget::calcSelectionRectangle( const TQPoint & startPoint, const TQPoint & _endPoint ) +{ + TQPoint endPoint = _endPoint; + if ( endPoint.x() < 0 ) endPoint.setX(0); + else if ( endPoint.x() > m_originalPixmap.width() ) endPoint.setX(m_originalPixmap.width()); + if ( endPoint.y() < 0 ) endPoint.setY(0); + else if ( endPoint.y() > m_originalPixmap.height() ) endPoint.setY(m_originalPixmap.height()); + int w=abs(startPoint.x()-endPoint.x()); + int h=abs(startPoint.y()-endPoint.y()); + + if (m_forcedAspectRatio>0) + { + double aspectRatio=w/double(h); + + if (aspectRatio>m_forcedAspectRatio) + h=(int)(w/m_forcedAspectRatio); + else + w=(int)(h*m_forcedAspectRatio); + } + + int x,y; + if ( startPoint.x() < endPoint.x() ) + x=startPoint.x(); + else + x=startPoint.x()-w; + if ( startPoint.y() < endPoint.y() ) + y=startPoint.y(); + else + y=startPoint.y()-h; + + if (x<0) + { + w+=x; + x=0; + h=(int)(w/m_forcedAspectRatio); + + if ( startPoint.y() > endPoint.y() ) + y=startPoint.y()-h; + } + else if (x+w>m_originalPixmap.width()) + { + w=m_originalPixmap.width()-x; + h=(int)(w/m_forcedAspectRatio); + + if ( startPoint.y() > endPoint.y() ) + y=startPoint.y()-h; + } + if (y<0) + { + h+=y; + y=0; + w=(int)(h*m_forcedAspectRatio); + + if ( startPoint.x() > endPoint.x() ) + x=startPoint.x()-w; + } + else if (y+h>m_originalPixmap.height()) + { + h=m_originalPixmap.height()-y; + w=(int)(h*m_forcedAspectRatio); + + if ( startPoint.x() > endPoint.x() ) + x=startPoint.x()-w; + } + + return TQRect(x,y,w,h); +} + +TQRect KPixmapRegionSelectorWidget::unzoomedSelectedRegion() const +{ + return TQRect((int)(m_selectedRegion.x()/m_zoomFactor), + (int)(m_selectedRegion.y()/m_zoomFactor), + (int)(m_selectedRegion.width()/m_zoomFactor), + (int)(m_selectedRegion.height()/m_zoomFactor)); +} + +TQImage KPixmapRegionSelectorWidget::selectedImage() const +{ + TQImage origImage=m_unzoomedPixmap.convertToImage(); + return origImage.copy(unzoomedSelectedRegion()); +} + +void KPixmapRegionSelectorWidget::setSelectionAspectRatio(int width, int height) +{ + m_forcedAspectRatio=width/double(height); +} + +void KPixmapRegionSelectorWidget::setFreeSelectionAspectRatio() +{ + m_forcedAspectRatio=0; +} + +void KPixmapRegionSelectorWidget::setMaximumWidgetSize(int width, int height) +{ + m_maxWidth=width; + m_maxHeight=height; + + m_originalPixmap=m_unzoomedPixmap; + if (m_selectedRegion == m_originalPixmap.rect()) m_selectedRegion=TQRect(); + +// kdDebug() << TQString(" original Pixmap :") << m_originalPixmap.rect() << endl; +// kdDebug() << TQString(" unzoomed Pixmap : %1 x %2 ").tqarg(m_unzoomedPixmap.width()).tqarg(m_unzoomedPixmap.height()) << endl; + + if ( !m_originalPixmap.isNull() && + ( m_originalPixmap.width() > m_maxWidth || + m_originalPixmap.height() > m_maxHeight ) ) + { + /* We have to resize the pixmap to get it complete on the screen */ + TQImage image=m_originalPixmap.convertToImage(); + m_originalPixmap.convertFromImage( image.smoothScale( width, height, TQ_ScaleMin ) ); + //m_originalPixmap.convertFromImage( KImageEffect::sample( image, width, height ) ); + double oldZoomFactor = m_zoomFactor; + m_zoomFactor=m_originalPixmap.width()/(double)m_unzoomedPixmap.width(); + + if (m_selectedRegion.isValid()) + { + m_selectedRegion= + TQRect((int)(m_selectedRegion.x()*m_zoomFactor/oldZoomFactor), + (int)(m_selectedRegion.y()*m_zoomFactor/oldZoomFactor), + (int)(m_selectedRegion.width()*m_zoomFactor/oldZoomFactor), + (int)(m_selectedRegion.height()*m_zoomFactor/oldZoomFactor) ); + } + } + + if (!m_selectedRegion.isValid()) m_selectedRegion = m_originalPixmap.rect(); + + m_linedPixmap=TQPixmap(); + updatePixmap(); + resize(m_label->width(), m_label->height()); +} + +#include "kpixmapregionselectorwidget.moc" diff --git a/libtdepim/kpixmapregionselectorwidget.h b/libtdepim/kpixmapregionselectorwidget.h new file mode 100644 index 000000000..7b75c98b2 --- /dev/null +++ b/libtdepim/kpixmapregionselectorwidget.h @@ -0,0 +1,176 @@ +/* + This file is part of libtdepim. + + Copyright (C) 2004 Antonio Larrosa <larrosa@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef __KPIXMAPREGIONSELECTORWIDGET_H__ +#define __KPIXMAPREGIONSELECTORWIDGET_H__ + +#include <tqvbox.h> +#include <tqpixmap.h> +#include <tqrect.h> +#include <tqlabel.h> +#include <kimageeffect.h> + +class KPopupMenu; + +namespace KPIM { + +/** + * KPixmapRegionSelectorWidget is a widget that shows a picture and provides the + * user with a friendly way to select a rectangular subregion of the pixmap. + * + * NOTE: There are two copies of this .h and the .cpp file, with subtle differences. + * One copy is in kdelibs/tdeui, and the other copy is in tdepim/libtdepim + * This is because tdepim has to remain backwards compatible. Any changes + * to either file should be made to the other. + * + * @author Antonio Larrosa <larrosa@kde.org> + * @since 3.4 + */ +class KPixmapRegionSelectorWidget : public TQWidget +{ + Q_OBJECT + TQ_OBJECT + +public: + /** + * Constructor for a KPixmapRegionSelectorWidget. + */ + KPixmapRegionSelectorWidget( TQWidget *parent = 0L, const char *name=0L); + + /** + * Destructor for a KPixmapRegionSelectorWidget + */ + ~KPixmapRegionSelectorWidget(); + + /** + * Sets the pixmap which will be shown for the user to select a region from. + * @param pixmap The pixmap. Must be non-null. + */ + void setPixmap( const TQPixmap &pixmap ); + + /** + * @return the original whole pixmap that we're using in this widget as the + * pixmap the user is selecting a region from. + */ + TQPixmap pixmap() const { return m_unzoomedPixmap; } + + /** + * Sets the selected region to be @p rect (in zoomed pixmap coordinates) + */ + void setSelectedRegion(const TQRect &rect); + + /** + * Returns the selected region ( in zoomed pixmap coordinates ) + */ + TQRect selectedRegion() const; + + /** + * Returns the selected region ( in unzoomed, original pixmap coordinates ) + */ + TQRect unzoomedSelectedRegion() const; + + /** + * Resets the selection to use the whole image + */ + void resetSelection(); + + /** + * @returns a TQImage object with just the region the user selected from the + * image + */ + TQImage selectedImage() const; + + /** + * Sets the aspect ration that the selected subimage should have. The way to + * select it, is specifying an example valid @p width and @p height. + * @see setFreeSelectionAspectRatio() + */ + void setSelectionAspectRatio(int width, int height); + + /** + * Allows the user to do a selection which has any aspect ratio. This is + * the default. + * @see setSelectionAspectRatio() + */ + void setFreeSelectionAspectRatio(); + + /** + * Sets the maximum size for the widget. If the image is larger than this + * (either horizontally or vertically), it's scaled to adjust to the maximum + * size (preserving the aspect ratio) + */ + void setMaximumWidgetSize( int width, int height ); + + /** + * Rotates the image as specified by the @p direction parameter, also tries + * to rotate the selected region so that it doesn't change, as long as the + * forced aspect ratio setting is respected, in other case, the selected region + * is resetted. + */ + void rotate(KImageEffect::RotateDirection direction); + +public slots: + /** + * Rotates the current image 90º clockwise + */ + void rotateClockwise(); + /** + * Rotates the current image 90º counterclockwise + */ + void rotateCounterclockwise(); + +protected: + /** + * Creates a KPopupMenu with the menu that appears when clicking with the right button on the label + */ + virtual KPopupMenu *createPopupMenu(); + + +private: + bool eventFilter(TQObject *obj, TQEvent *ev); + + /** + * Recalculates the pixmap that is shown based on the current selected area, + * the original image, etc. + */ + void updatePixmap(); + + TQRect calcSelectionRectangle( const TQPoint &startPoint, const TQPoint & endPoint ); + + enum CursorState { None=0, Resizing, Moving }; + CursorState m_state; + + TQPixmap m_unzoomedPixmap; + TQPixmap m_originalPixmap; + TQPixmap m_linedPixmap; + TQRect m_selectedRegion; + TQLabel *m_label; + + TQPoint m_tempFirstClick; + double m_forcedAspectRatio; + + int m_maxWidth, m_maxHeight; + double m_zoomFactor; +}; + +} // for namespace + +#endif diff --git a/libtdepim/kprefsdialog.cpp b/libtdepim/kprefsdialog.cpp new file mode 100644 index 000000000..466691083 --- /dev/null +++ b/libtdepim/kprefsdialog.cpp @@ -0,0 +1,907 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org> + Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com> + Copyright (C) 2005 Allen Winter <winter@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <tqlayout.h> +#include <tqlabel.h> +#include <tqbuttongroup.h> +#include <tqlineedit.h> +#include <tqfont.h> +#include <tqspinbox.h> +#include <tqframe.h> +#include <tqcombobox.h> +#include <tqcheckbox.h> +#include <tqradiobutton.h> +#include <tqpushbutton.h> +#include <tqdatetimeedit.h> +#include <tqwhatsthis.h> + +#include <kcolorbutton.h> +#include <kdebug.h> +#include <klocale.h> +#include <kfontdialog.h> +#include <kmessagebox.h> +#include <kconfigskeleton.h> +#include <kurlrequester.h> +#include "ktimeedit.h" +#include "kdateedit.h" + +#include "kprefsdialog.h" +#include "kprefsdialog.moc" + +namespace KPrefsWidFactory { + +KPrefsWid *create( KConfigSkeletonItem *item, TQWidget *parent ) +{ + KConfigSkeleton::ItemBool *boolItem = + dynamic_cast<KConfigSkeleton::ItemBool *>( item ); + if ( boolItem ) { + return new KPrefsWidBool( boolItem, parent ); + } + + KConfigSkeleton::ItemString *stringItem = + dynamic_cast<KConfigSkeleton::ItemString *>( item ); + if ( stringItem ) { + return new KPrefsWidString( stringItem, parent ); + } + + KConfigSkeleton::ItemEnum *enumItem = + dynamic_cast<KConfigSkeleton::ItemEnum *>( item ); + if ( enumItem ) { + TQValueList<KConfigSkeleton::ItemEnum::Choice> choices = enumItem->choices(); + if ( choices.isEmpty() ) { + kdError() << "KPrefsWidFactory::create(): Enum has no choices." << endl; + return 0; + } else { + KPrefsWidRadios *radios = new KPrefsWidRadios( enumItem, parent ); + TQValueList<KConfigSkeleton::ItemEnum::Choice>::ConstIterator it; + for( it = choices.begin(); it != choices.end(); ++it ) { + radios->addRadio( (*it).label ); + } + return radios; + } + } + + KConfigSkeleton::ItemInt *intItem = + dynamic_cast<KConfigSkeleton::ItemInt *>( item ); + if ( intItem ) { + return new KPrefsWidInt( intItem, parent ); + } + + return 0; +} + +} + + +TQValueList<TQWidget *> KPrefsWid::widgets() const +{ + return TQValueList<TQWidget *>(); +} + + +KPrefsWidBool::KPrefsWidBool( KConfigSkeleton::ItemBool *item, TQWidget *parent ) + : mItem( item ) +{ + mCheck = new TQCheckBox( item->label(), parent); + connect( mCheck, TQT_SIGNAL( clicked() ), TQT_SIGNAL( changed() ) ); + if ( !item->whatsThis().isNull() ) { + TQWhatsThis::add( mCheck, item->whatsThis() ); + } +} + +void KPrefsWidBool::readConfig() +{ + mCheck->setChecked( mItem->value() ); +} + +void KPrefsWidBool::writeConfig() +{ + mItem->setValue( mCheck->isChecked() ); +} + +TQCheckBox *KPrefsWidBool::checkBox() +{ + return mCheck; +} + +TQValueList<TQWidget *> KPrefsWidBool::widgets() const +{ + TQValueList<TQWidget *> widgets; + widgets.append( mCheck ); + return widgets; +} + + +KPrefsWidInt::KPrefsWidInt( KConfigSkeleton::ItemInt *item, + TQWidget *parent ) + : mItem( item ) +{ + mLabel = new TQLabel( mItem->label()+':', parent ); + mSpin = new TQSpinBox( parent ); + if ( !item->minValue().isNull() ) { + mSpin->setMinValue( item->minValue().toInt() ); + } + if ( !item->maxValue().isNull() ) { + mSpin->setMaxValue( item->maxValue().toInt() ); + } + connect( mSpin, TQT_SIGNAL( valueChanged( int ) ), TQT_SIGNAL( changed() ) ); + mLabel->setBuddy( mSpin ); + TQString whatsThis = mItem->whatsThis(); + if ( !whatsThis.isEmpty() ) { + TQWhatsThis::add( mLabel, whatsThis ); + TQWhatsThis::add( mSpin, whatsThis ); + } +} + +void KPrefsWidInt::readConfig() +{ + mSpin->setValue( mItem->value() ); +} + +void KPrefsWidInt::writeConfig() +{ + mItem->setValue( mSpin->value() ); +} + +TQLabel *KPrefsWidInt::label() +{ + return mLabel; +} + +TQSpinBox *KPrefsWidInt::spinBox() +{ + return mSpin; +} + +TQValueList<TQWidget *> KPrefsWidInt::widgets() const +{ + TQValueList<TQWidget *> widgets; + widgets.append( mLabel ); + widgets.append( mSpin ); + return widgets; +} + + +KPrefsWidColor::KPrefsWidColor( KConfigSkeleton::ItemColor *item, + TQWidget *parent ) + : mItem( item ) +{ + mButton = new KColorButton( parent ); + connect( mButton, TQT_SIGNAL( changed( const TQColor & ) ), TQT_SIGNAL( changed() ) ); + mLabel = new TQLabel( mButton, mItem->label()+':', parent ); + mLabel->setBuddy( mButton ); + TQString whatsThis = mItem->whatsThis(); + if (!whatsThis.isNull()) { + TQWhatsThis::add(mButton, whatsThis); + } +} + +KPrefsWidColor::~KPrefsWidColor() +{ +// kdDebug(5300) << "KPrefsWidColor::~KPrefsWidColor()" << endl; +} + +void KPrefsWidColor::readConfig() +{ + mButton->setColor( mItem->value() ); +} + +void KPrefsWidColor::writeConfig() +{ + mItem->setValue( mButton->color() ); +} + +TQLabel *KPrefsWidColor::label() +{ + return mLabel; +} + +KColorButton *KPrefsWidColor::button() +{ + return mButton; +} + + +KPrefsWidFont::KPrefsWidFont( KConfigSkeleton::ItemFont *item, + TQWidget *parent, const TQString &sampleText ) + : mItem( item ) +{ + mLabel = new TQLabel( mItem->label()+':', parent ); + + mPreview = new TQLabel( sampleText, parent ); + mPreview->setFrameStyle( TQFrame::Panel | TQFrame::Sunken ); + + mButton = new TQPushButton( i18n("Choose..."), parent ); + connect( mButton, TQT_SIGNAL( clicked() ), TQT_SLOT( selectFont() ) ); + TQString whatsThis = mItem->whatsThis(); + if (!whatsThis.isNull()) { + TQWhatsThis::add(mPreview, whatsThis); + TQWhatsThis::add(mButton, whatsThis); + } +} + +KPrefsWidFont::~KPrefsWidFont() +{ +} + +void KPrefsWidFont::readConfig() +{ + mPreview->setFont( mItem->value() ); +} + +void KPrefsWidFont::writeConfig() +{ + mItem->setValue( mPreview->font() ); +} + +TQLabel *KPrefsWidFont::label() +{ + return mLabel; +} + +TQFrame *KPrefsWidFont::preview() +{ + return mPreview; +} + +TQPushButton *KPrefsWidFont::button() +{ + return mButton; +} + +void KPrefsWidFont::selectFont() +{ + TQFont myFont(mPreview->font()); + int result = KFontDialog::getFont(myFont); + if (result == KFontDialog::Accepted) { + mPreview->setFont(myFont); + emit changed(); + } +} + + +KPrefsWidTime::KPrefsWidTime( KConfigSkeleton::ItemDateTime *item, + TQWidget *parent ) + : mItem( item ) +{ + mLabel = new TQLabel( mItem->label()+':', parent ); + mTimeEdit = new KTimeEdit( parent ); + mLabel->setBuddy( mTimeEdit ); + connect( mTimeEdit, TQT_SIGNAL( timeChanged( TQTime ) ), TQT_SIGNAL( changed() ) ); + TQString whatsThis = mItem->whatsThis(); + if ( !whatsThis.isNull() ) { + TQWhatsThis::add( mTimeEdit, whatsThis ); + } +} + +void KPrefsWidTime::readConfig() +{ + mTimeEdit->setTime( mItem->value().time() ); +} + +void KPrefsWidTime::writeConfig() +{ + // Don't overwrite the date value of the TQDateTime, so we can use a + // KPrefsWidTime and a KPrefsWidDate on the same config entry! + TQDateTime dt( mItem->value() ); + dt.setTime( mTimeEdit->getTime() ); + mItem->setValue( dt ); +} + +TQLabel *KPrefsWidTime::label() +{ + return mLabel; +} + +KTimeEdit *KPrefsWidTime::timeEdit() +{ + return mTimeEdit; +} + + +KPrefsWidDuration::KPrefsWidDuration( KConfigSkeleton::ItemDateTime *item, + TQWidget *parent ) + : mItem( item ) +{ + mLabel = new TQLabel( mItem->label()+':', parent ); + mTimeEdit = new TQTimeEdit( parent ); + mLabel->setBuddy( mTimeEdit ); + mTimeEdit->setAutoAdvance( true ); + mTimeEdit->setDisplay( TQTimeEdit::Hours | TQTimeEdit::Minutes ); + mTimeEdit->setRange( TQTime( 0, 1 ), TQTime( 24, 0 ) ); // [1min, 24hr] + connect( mTimeEdit, + TQT_SIGNAL( valueChanged( const TQTime & ) ), TQT_SIGNAL( changed() ) ); + TQString whatsThis = mItem->whatsThis(); + if ( !whatsThis.isNull() ) { + TQWhatsThis::add( mTimeEdit, whatsThis ); + } +} + +void KPrefsWidDuration::readConfig() +{ + mTimeEdit->setTime( mItem->value().time() ); +} + +void KPrefsWidDuration::writeConfig() +{ + TQDateTime dt( mItem->value() ); + dt.setTime( mTimeEdit->time() ); + mItem->setValue( dt ); +} + +TQLabel *KPrefsWidDuration::label() +{ + return mLabel; +} + +TQTimeEdit *KPrefsWidDuration::timeEdit() +{ + return mTimeEdit; +} + + +KPrefsWidDate::KPrefsWidDate( KConfigSkeleton::ItemDateTime *item, + TQWidget *parent ) + : mItem( item ) +{ + mLabel = new TQLabel( mItem->label()+':', parent ); + mDateEdit = new KDateEdit( parent ); + mLabel->setBuddy( mDateEdit ); + connect( mDateEdit, TQT_SIGNAL( dateChanged( const TQDate& ) ), TQT_SIGNAL( changed() ) ); + TQString whatsThis = mItem->whatsThis(); + if ( !whatsThis.isNull() ) { + TQWhatsThis::add( mDateEdit, whatsThis ); + } +} + +void KPrefsWidDate::readConfig() +{ + mDateEdit->setDate( mItem->value().date().isValid() ? mItem->value().date() : TQDate::tqcurrentDate() ); +} + +void KPrefsWidDate::writeConfig() +{ + TQDateTime dt( mItem->value() ); + dt.setDate( mDateEdit->date() ); + mItem->setValue( dt ); +} + +TQLabel *KPrefsWidDate::label() +{ + return mLabel; +} + +KDateEdit *KPrefsWidDate::dateEdit() +{ + return mDateEdit; +} + + +KPrefsWidRadios::KPrefsWidRadios( KConfigSkeleton::ItemEnum *item, + TQWidget *parent ) + : mItem( item ) +{ + mBox = new TQButtonGroup( 1, Qt::Horizontal, mItem->label(), parent ); + connect( mBox, TQT_SIGNAL( clicked( int ) ), TQT_SIGNAL( changed() ) ); +} + +KPrefsWidRadios::~KPrefsWidRadios() +{ +} + +void KPrefsWidRadios::addRadio(const TQString &text, const TQString &whatsThis) +{ + TQRadioButton *r = new TQRadioButton(text,mBox); + if (!whatsThis.isNull()) { + TQWhatsThis::add(r, whatsThis); + } +} + +TQButtonGroup *KPrefsWidRadios::groupBox() +{ + return mBox; +} + +void KPrefsWidRadios::readConfig() +{ + mBox->setButton( mItem->value() ); +} + +void KPrefsWidRadios::writeConfig() +{ + mItem->setValue( mBox->id( mBox->selected() ) ); +} + +TQValueList<TQWidget *> KPrefsWidRadios::widgets() const +{ + TQValueList<TQWidget *> w; + w.append( mBox ); + return w; +} + +KPrefsWidCombo::KPrefsWidCombo( KConfigSkeleton::ItemEnum *item, + TQWidget *parent ) + : mItem( item ) +{ + TQHBox *hbox = new TQHBox(parent); + new TQLabel( mItem->label(), hbox ); + mCombo = new TQComboBox( hbox ); + connect( mCombo, TQT_SIGNAL( activated( int ) ), TQT_SIGNAL( changed() ) ); +} + +KPrefsWidCombo::~KPrefsWidCombo() +{ +} + +void KPrefsWidCombo::readConfig() +{ + mCombo->setCurrentItem( mItem->value() ); +} + +void KPrefsWidCombo::writeConfig() +{ + mItem->setValue( mCombo->currentItem() ); +} + +TQValueList<TQWidget *> KPrefsWidCombo::widgets() const +{ + TQValueList<TQWidget *> w; + w.append( mCombo ); + return w; +} + +TQComboBox* KPrefsWidCombo::comboBox() +{ + return mCombo; +} + +KPrefsWidString::KPrefsWidString( KConfigSkeleton::ItemString *item, + TQWidget *parent, + TQLineEdit::EchoMode echomode ) + : mItem( item ) +{ + mLabel = new TQLabel( mItem->label()+':', parent ); + mEdit = new TQLineEdit( parent ); + mLabel->setBuddy( mEdit ); + connect( mEdit, TQT_SIGNAL( textChanged( const TQString & ) ), + TQT_SIGNAL( changed() ) ); + mEdit->setEchoMode( echomode ); + TQString whatsThis = mItem->whatsThis(); + if ( !whatsThis.isNull() ) { + TQWhatsThis::add( mEdit, whatsThis ); + } +} + +KPrefsWidString::~KPrefsWidString() +{ +} + +void KPrefsWidString::readConfig() +{ + mEdit->setText( mItem->value() ); +} + +void KPrefsWidString::writeConfig() +{ + mItem->setValue( mEdit->text() ); +} + +TQLabel *KPrefsWidString::label() +{ + return mLabel; +} + +TQLineEdit *KPrefsWidString::lineEdit() +{ + return mEdit; +} + +TQValueList<TQWidget *> KPrefsWidString::widgets() const +{ + TQValueList<TQWidget *> widgets; + widgets.append( mLabel ); + widgets.append( mEdit ); + return widgets; +} + + +KPrefsWidPath::KPrefsWidPath( KConfigSkeleton::ItemPath *item, TQWidget *parent, + const TQString &filter, uint mode ) + : mItem( item ) +{ + mLabel = new TQLabel( mItem->label()+':', parent ); + mURLRequester = new KURLRequester( parent ); + mLabel->setBuddy( mURLRequester ); + mURLRequester->setMode( mode ); + mURLRequester->setFilter( filter ); + connect( mURLRequester, TQT_SIGNAL( textChanged( const TQString & ) ), + TQT_SIGNAL( changed() ) ); + TQString whatsThis = mItem->whatsThis(); + if ( !whatsThis.isNull() ) { + TQWhatsThis::add( mURLRequester, whatsThis ); + } +} + +KPrefsWidPath::~KPrefsWidPath() +{ +} + +void KPrefsWidPath::readConfig() +{ + mURLRequester->setURL( mItem->value() ); +} + +void KPrefsWidPath::writeConfig() +{ + mItem->setValue( mURLRequester->url() ); +} + +TQLabel *KPrefsWidPath::label() +{ + return mLabel; +} + +KURLRequester *KPrefsWidPath::urlRequester() +{ + return mURLRequester; +} + +TQValueList<TQWidget *> KPrefsWidPath::widgets() const +{ + TQValueList<TQWidget *> widgets; + widgets.append( mLabel ); + widgets.append( mURLRequester ); + return widgets; +} + + +KPrefsWidManager::KPrefsWidManager( KConfigSkeleton *prefs ) + : mPrefs( prefs ) +{ +} + +KPrefsWidManager::~KPrefsWidManager() +{ +} + +void KPrefsWidManager::addWid( KPrefsWid *wid ) +{ + mPrefsWids.append( wid ); +} + +KPrefsWidBool *KPrefsWidManager::addWidBool( KConfigSkeleton::ItemBool *item, + TQWidget *parent ) +{ + KPrefsWidBool *w = new KPrefsWidBool( item, parent ); + addWid( w ); + return w; +} + +KPrefsWidTime *KPrefsWidManager::addWidTime( KConfigSkeleton::ItemDateTime *item, + TQWidget *parent ) +{ + KPrefsWidTime *w = new KPrefsWidTime( item, parent ); + addWid( w ); + return w; +} + +KPrefsWidDuration *KPrefsWidManager::addWidDuration( KConfigSkeleton::ItemDateTime *item, + TQWidget *parent ) +{ + KPrefsWidDuration *w = new KPrefsWidDuration( item, parent ); + addWid( w ); + return w; +} + +KPrefsWidDate *KPrefsWidManager::addWidDate( KConfigSkeleton::ItemDateTime *item, + TQWidget *parent ) +{ + KPrefsWidDate *w = new KPrefsWidDate( item, parent ); + addWid( w ); + return w; +} + +KPrefsWidColor *KPrefsWidManager::addWidColor( KConfigSkeleton::ItemColor *item, + TQWidget *parent ) +{ + KPrefsWidColor *w = new KPrefsWidColor( item, parent ); + addWid( w ); + return w; +} + +KPrefsWidRadios *KPrefsWidManager::addWidRadios( KConfigSkeleton::ItemEnum *item, + TQWidget *parent ) +{ + KPrefsWidRadios *w = new KPrefsWidRadios( item, parent ); + TQValueList<KConfigSkeleton::ItemEnum::Choice> choices; + choices = item->choices(); + TQValueList<KConfigSkeleton::ItemEnum::Choice>::ConstIterator it; + for( it = choices.begin(); it != choices.end(); ++it ) { + w->addRadio( (*it).label, (*it).whatsThis ); + } + addWid( w ); + return w; +} + +KPrefsWidCombo *KPrefsWidManager::addWidCombo( KConfigSkeleton::ItemEnum *item, + TQWidget* parent ) +{ + KPrefsWidCombo *w = new KPrefsWidCombo( item, parent ); + TQValueList<KConfigSkeleton::ItemEnum::Choice> choices; + choices = item->choices(); + TQValueList<KConfigSkeleton::ItemEnum::Choice>::ConstIterator it; + for( it = choices.begin(); it != choices.end(); ++it ) { + w->comboBox()->insertItem( (*it).label, -1 ); + } + addWid( w ); + return w; +} + +KPrefsWidString *KPrefsWidManager::addWidString( KConfigSkeleton::ItemString *item, + TQWidget *parent ) +{ + KPrefsWidString *w = new KPrefsWidString( item, parent, + TQLineEdit::Normal ); + addWid( w ); + return w; +} + +KPrefsWidPath *KPrefsWidManager::addWidPath( KConfigSkeleton::ItemPath *item, + TQWidget *parent, const TQString &filter, uint mode ) +{ + KPrefsWidPath *w = new KPrefsWidPath( item, parent, filter, mode ); + addWid( w ); + return w; +} + +KPrefsWidString *KPrefsWidManager::addWidPassword( KConfigSkeleton::ItemString *item, + TQWidget *parent ) +{ + KPrefsWidString *w = new KPrefsWidString( item, parent, TQLineEdit::Password ); + addWid( w ); + return w; +} + +KPrefsWidFont *KPrefsWidManager::addWidFont( KConfigSkeleton::ItemFont *item, + TQWidget *parent, + const TQString &sampleText ) +{ + KPrefsWidFont *w = new KPrefsWidFont( item, parent, sampleText ); + addWid( w ); + return w; +} + +KPrefsWidInt *KPrefsWidManager::addWidInt( KConfigSkeleton::ItemInt *item, + TQWidget *parent ) +{ + KPrefsWidInt *w = new KPrefsWidInt( item, parent ); + addWid( w ); + return w; +} + +void KPrefsWidManager::setWidDefaults() +{ + kdDebug() << "KPrefsWidManager::setWidDefaults()" << endl; + + bool tmp = mPrefs->useDefaults( true ); + + readWidConfig(); + + mPrefs->useDefaults( tmp ); +} + +void KPrefsWidManager::readWidConfig() +{ + kdDebug(5310) << "KPrefsWidManager::readWidConfig()" << endl; + + KPrefsWid *wid; + for( wid = mPrefsWids.first(); wid; wid = mPrefsWids.next() ) { + wid->readConfig(); + } +} + +void KPrefsWidManager::writeWidConfig() +{ + kdDebug(5310) << "KPrefsWidManager::writeWidConfig()" << endl; + + KPrefsWid *wid; + for( wid = mPrefsWids.first(); wid; wid = mPrefsWids.next() ) { + wid->writeConfig(); + } + + mPrefs->writeConfig(); +} + + +KPrefsDialog::KPrefsDialog( KConfigSkeleton *prefs, TQWidget *parent, char *name, + bool modal ) + : KDialogBase(IconList,i18n("Preferences"),Ok|Apply|Cancel|Default,Ok,parent, + name,modal,true), + KPrefsWidManager( prefs ) +{ +// TODO: This seems to cause a crash on exit. Investigate later. +// mPrefsWids.setAutoDelete(true); + +// connect(this,TQT_SIGNAL(defaultClicked()),TQT_SLOT(setDefaults())); + connect(this,TQT_SIGNAL(cancelClicked()),TQT_SLOT(reject())); +} + +KPrefsDialog::~KPrefsDialog() +{ +} + +void KPrefsDialog::autoCreate() +{ + KConfigSkeletonItem::List items = prefs()->items(); + + TQMap<TQString,TQWidget *> mGroupPages; + TQMap<TQString,TQGridLayout *> mGroupLayouts; + TQMap<TQString,int> mCurrentRows; + + KConfigSkeletonItem::List::ConstIterator it; + for( it = items.begin(); it != items.end(); ++it ) { + TQString group = (*it)->group(); + TQString name = (*it)->name(); + + kdDebug() << "ITEMS: " << (*it)->name() << endl; + + TQWidget *page; + TQGridLayout *tqlayout; + int currentRow; + if ( !mGroupPages.contains( group ) ) { + page = addPage( group ); + tqlayout = new TQGridLayout( page ); + mGroupPages.insert( group, page ); + mGroupLayouts.insert( group, tqlayout ); + currentRow = 0; + mCurrentRows.insert( group, currentRow ); + } else { + page = mGroupPages[ group ]; + tqlayout = mGroupLayouts[ group ]; + currentRow = mCurrentRows[ group ]; + } + + KPrefsWid *wid = KPrefsWidFactory::create( *it, page ); + + if ( wid ) { + TQValueList<TQWidget *> widgets = wid->widgets(); + if ( widgets.count() == 1 ) { + tqlayout->addMultiCellWidget( widgets[ 0 ], + currentRow, currentRow, 0, 1 ); + } else if ( widgets.count() == 2 ) { + tqlayout->addWidget( widgets[ 0 ], currentRow, 0 ); + tqlayout->addWidget( widgets[ 1 ], currentRow, 1 ); + } else { + kdError() << "More widgets than expected: " << widgets.count() << endl; + } + + if ( (*it)->isImmutable() ) { + TQValueList<TQWidget *>::Iterator it2; + for( it2 = widgets.begin(); it2 != widgets.end(); ++it2 ) { + (*it2)->setEnabled( false ); + } + } + + addWid( wid ); + + mCurrentRows.replace( group, ++currentRow ); + } + } + + readConfig(); +} + +void KPrefsDialog::setDefaults() +{ + setWidDefaults(); +} + +void KPrefsDialog::readConfig() +{ + readWidConfig(); + + usrReadConfig(); +} + +void KPrefsDialog::writeConfig() +{ + writeWidConfig(); + + usrWriteConfig(); + + readConfig(); +} + + +void KPrefsDialog::slotApply() +{ + writeConfig(); + emit configChanged(); +} + +void KPrefsDialog::slotOk() +{ + slotApply(); + accept(); +} + +void KPrefsDialog::slotDefault() +{ + kdDebug() << "KPrefsDialog::slotDefault()" << endl; + + if (KMessageBox::warningContinueCancel(this, + i18n("You are about to set all preferences to default values. All " + "custom modifications will be lost."),i18n("Setting Default Preferences"), + i18n("Reset to Defaults")) + == KMessageBox::Continue) setDefaults(); +} + + +KPrefsModule::KPrefsModule( KConfigSkeleton *prefs, TQWidget *parent, + const char *name ) + : KCModule( parent, name ), + KPrefsWidManager( prefs ) +{ + emit changed( false ); +} + +void KPrefsModule::addWid( KPrefsWid *wid ) +{ + KPrefsWidManager::addWid( wid ); + + connect( wid, TQT_SIGNAL( changed() ), TQT_SLOT( slotWidChanged() ) ); +} + +void KPrefsModule::slotWidChanged() +{ + kdDebug(5310) << "KPrefsModule::slotWidChanged()" << endl; + + emit changed( true ); +} + +void KPrefsModule::load() +{ + kdDebug(5310) << "KPrefsModule::load()" << endl; + + readWidConfig(); + + usrReadConfig(); + + emit changed( false ); +} + +void KPrefsModule::save() +{ + kdDebug(5310) << "KPrefsModule::save()" << endl; + + writeWidConfig(); + + usrWriteConfig(); +} + +void KPrefsModule::defaults() +{ + setWidDefaults(); + + emit changed( true ); +} diff --git a/libtdepim/kprefsdialog.h b/libtdepim/kprefsdialog.h new file mode 100644 index 000000000..b1fffcdd7 --- /dev/null +++ b/libtdepim/kprefsdialog.h @@ -0,0 +1,798 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2001-2003 Cornelius Schumacher <schumacher@kde.org> + Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com> + Copyright (C) 2005 Allen Winter <winter@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef KPREFSDIALOG_H +#define KPREFSDIALOG_H + +#include <tqptrlist.h> +#include <tqlineedit.h> +#include <tqvaluelist.h> +#include <tqdatetimeedit.h> + +#include <kdialogbase.h> +#include <kcmodule.h> +#include <kconfigskeleton.h> +#include <kfile.h> +#include <tdepimmacros.h> + + +class KColorButton; +class TQCheckBox; +class TQComboBox; +class TQLabel; +class TQSpinBox; +class TQButtonGroup; +class TQTimeEdit; +class KTimeEdit; +class KDateEdit; +class KURLRequester; + +/** + @short Base class for GUI control elements used by @ref KPrefsDialog. + @author Cornelius Schumacher + @see KPrefsDialog + + This class provides the interface for the GUI control elements used by + KPrefsDialog. The control element consists of a set of widgets for handling + a certain type of configuration information. +*/ +class KDE_EXPORT KPrefsWid : public TQObject +{ + Q_OBJECT + TQ_OBJECT + public: + /** + This function is called to read value of the setting from the + stored configuration and display it in the widget. + */ + virtual void readConfig() = 0; + /** + This function is called to write the current setting of the widget to the + stored configuration. + */ + virtual void writeConfig() = 0; + + /** + Return a list of widgets used by this control element. + */ + virtual TQValueList<TQWidget *> widgets() const; + + signals: + /** + Emitted when widget value has changed. + */ + void changed(); +}; + +/** + @short Widgets for bool settings in @ref KPrefsDialog. + + This class provides a control element for configuring bool values. It is meant + to be used by KPrefsDialog. The user is responsible for the tqlayout management. +*/ +class KDE_EXPORT KPrefsWidBool : public KPrefsWid +{ + public: + /** + Create a bool value control element consisting of a TQCheckbox. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidBool( KConfigSkeleton::ItemBool *item, TQWidget *parent ); + + /** + Return the TQCheckbox used by this control element. + */ + TQCheckBox *checkBox(); + + void readConfig(); + void writeConfig(); + + TQValueList<TQWidget *> widgets() const; + + private: + KConfigSkeleton::ItemBool *mItem; + + TQCheckBox *mCheck; +}; + +/** + @short Widgets for int settings in @ref KPrefsDialog. + + This class provides a control element for configuring integer values. It is + meant to be used by KPrefsDialog. The user is responsible for the tqlayout + management. +*/ +class KDE_EXPORT KPrefsWidInt : public KPrefsWid +{ + public: + /** + Create a integer value control element consisting of a label and a + spinbox. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidInt( KConfigSkeleton::ItemInt *item, TQWidget *parent ); + + /** + Return TQLabel used by this control element. + */ + TQLabel *label(); + + /** + Return the TQSpinBox used by this control element. + */ + TQSpinBox *spinBox(); + + void readConfig(); + void writeConfig(); + + TQValueList<TQWidget *> widgets() const; + + private: + KConfigSkeleton::ItemInt *mItem; + + TQLabel *mLabel; + TQSpinBox *mSpin; +}; + +/** + @short Widgets for time settings in @ref KPrefsDialog. + + This class provides a control element for configuring time values. It is + meant to be used by KPrefsDialog. The user is responsible for the tqlayout + management. +*/ +class KDE_EXPORT KPrefsWidTime : public KPrefsWid +{ + public: + /** + Create a time value control element consisting of a label and a spinbox. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidTime( KConfigSkeleton::ItemDateTime *item, TQWidget *parent ); + + /** + Return TQLabel used by this widget. + */ + TQLabel *label(); + /** + Return TQSpinBox used by this widget. + */ + KTimeEdit *timeEdit(); + + void readConfig(); + void writeConfig(); + + private: + KConfigSkeleton::ItemDateTime *mItem; + + TQLabel *mLabel; + KTimeEdit *mTimeEdit; +}; + +/** + @short Widgets for duration settings in @ref KPrefsDialog. + + This class provides a control element for configuring duration values. It is + meant to be used by KPrefsDialog. The user is responsible for the tqlayout + management. +*/ +class KDE_EXPORT KPrefsWidDuration : public KPrefsWid +{ + public: + /** + Create a duration value control element consisting of a label and a + spinbox. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidDuration( KConfigSkeleton::ItemDateTime *item, TQWidget *parent ); + + /** + Return TQLabel used by this widget. + */ + TQLabel *label(); + /** + Return TQSpinBox used by this widget. + */ + TQTimeEdit *timeEdit(); + + void readConfig(); + void writeConfig(); + + private: + KConfigSkeleton::ItemDateTime *mItem; + + TQLabel *mLabel; + TQTimeEdit *mTimeEdit; +}; + +/** + @short Widgets for time settings in @ref KPrefsDialog. + + This class provides a control element for configuring date values. It is + meant to be used by KPrefsDialog. The user is responsible for the tqlayout + management. +*/ +class KDE_EXPORT KPrefsWidDate : public KPrefsWid +{ + public: + /** + Create a time value control element consisting of a label and a date box. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidDate( KConfigSkeleton::ItemDateTime *item, TQWidget *parent ); + + /** + Return TQLabel used by this widget. + */ + TQLabel *label(); + /** + Return TQSpinBox used by this widget. + */ + KDateEdit *dateEdit(); + + void readConfig(); + void writeConfig(); + + private: + KConfigSkeleton::ItemDateTime *mItem; + + TQLabel *mLabel; + KDateEdit *mDateEdit; +}; + +/** + @short Widgets for color settings in @ref KPrefsDialog. + + This class provides a control element for configuring color values. It is + meant to be used by KPrefsDialog. The user is responsible for the tqlayout + management. +*/ +class KDE_EXPORT KPrefsWidColor : public KPrefsWid +{ + Q_OBJECT + TQ_OBJECT + public: + /** + Create a color value control element consisting of a test field and a + button for opening a color dialog. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidColor( KConfigSkeleton::ItemColor *item, TQWidget *parent ); + /** + Destruct color setting widget. + */ + ~KPrefsWidColor(); + + /** + Return TQLabel for the button + */ + TQLabel *label(); + /** + Return button opening the color dialog. + */ + KColorButton *button(); + + void readConfig(); + void writeConfig(); + + private: + KConfigSkeleton::ItemColor *mItem; + + TQLabel *mLabel; + KColorButton *mButton; +}; + +/** + @short Widgets for font settings in @ref KPrefsDialog. + + This class provides a control element for configuring font values. It is meant + to be used by KPrefsDialog. The user is responsible for the tqlayout management. +*/ +class KDE_EXPORT KPrefsWidFont : public KPrefsWid +{ + Q_OBJECT + TQ_OBJECT + public: + /** + Create a font value control element consisting of a test field and a + button for opening a font dialog. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + @param sampleText Sample text for previewing the selected font. + */ + KPrefsWidFont( KConfigSkeleton::ItemFont *item, + TQWidget *parent, const TQString &sampleText ); + /** + Destruct font setting widget. + */ + ~KPrefsWidFont(); + + /** + Return TQLabel. + */ + TQLabel *label(); + /** + Return TQFrame used as preview field. + */ + TQFrame *preview(); + /** + Return button opening the font dialog. + */ + TQPushButton *button(); + + void readConfig(); + void writeConfig(); + + protected slots: + void selectFont(); + + private: + KConfigSkeleton::ItemFont *mItem; + + TQLabel *mLabel; + TQLabel *mPreview; + TQPushButton *mButton; +}; + +/** + @short Widgets for settings represented by a group of radio buttons in + @ref KPrefsDialog. + + This class provides a control element for configuring selections. It is meant + to be used by KPrefsDialog. The user is responsible for the tqlayout management. + + The setting is interpreted as an int value, corresponding to the position of + the radio button. The position of the button is defined by the sequence of + @ref addRadio() calls, starting with 0. +*/ +class KDE_EXPORT KPrefsWidRadios : public KPrefsWid +{ + public: + /** + Create a control element for selection of an option. It consists of a box + with several radio buttons. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidRadios( KConfigSkeleton::ItemEnum *item, TQWidget *parent ); + virtual ~KPrefsWidRadios(); + + /** + Add a radio button. + + @param text Text of the button. + @param whatsThis What's This help for the button. + */ + void addRadio( const TQString &text, + const TQString &whatsThis = TQString() ); + + /** + Return the box widget used by this widget. + */ + TQButtonGroup *groupBox(); + + void readConfig(); + void writeConfig(); + + TQValueList<TQWidget *> widgets() const; + + private: + KConfigSkeleton::ItemEnum *mItem; + + TQButtonGroup *mBox; +}; + +/** + @short Widgets for settings represented by a combo box in + @ref KPrefsDialog. + + This class provides a control element for configuring selections. It is meant + to be used by KPrefsDialog. The user is responsible for the tqlayout management. + + The setting is interpreted as an int value, corresponding to the index in + the combo box. +*/ +class KDE_EXPORT KPrefsWidCombo : public KPrefsWid +{ + public: + /** + Create a control element for selection of an option. It consists of a + combo box. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidCombo( KConfigSkeleton::ItemEnum *item, TQWidget *parent ); + virtual ~KPrefsWidCombo(); + + void readConfig(); + void writeConfig(); + + TQComboBox *comboBox(); + TQValueList<TQWidget *> widgets() const; + + private: + KConfigSkeleton::ItemEnum *mItem; + TQComboBox *mCombo; +}; + + + +/** + @short Widgets for string settings in @ref KPrefsDialog. + + This class provides a control element for configuring string values. It is + meant to be used by KPrefsDialog. The user is responsible for the tqlayout + management. +*/ +class KDE_EXPORT KPrefsWidString : public KPrefsWid +{ + public: + /** + Create a string value control element consisting of a test label and a + line edit. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + @param echomode Describes how a line edit should display its contents. + */ + KPrefsWidString( KConfigSkeleton::ItemString *item, TQWidget *parent, + TQLineEdit::EchoMode echomode=TQLineEdit::Normal ); + /** + Destructor. + */ + virtual ~KPrefsWidString(); + + /** + Return TQLabel used by this widget. + */ + TQLabel *label(); + /** + Return TQLineEdit used by this widget. + */ + TQLineEdit *lineEdit(); + + void readConfig(); + void writeConfig(); + + TQValueList<TQWidget *> widgets() const; + + private: + KConfigSkeleton::ItemString *mItem; + + TQLabel *mLabel; + TQLineEdit *mEdit; +}; + + +/** + @short Widgets for string settings in @ref KPrefsDialog. + + This class provides a control element for configuring string values. It is + meant to be used by KPrefsDialog. The user is responsible for the tqlayout + management. +*/ +class KDE_EXPORT KPrefsWidPath : public KPrefsWid +{ + public: + /** + Create a string value control element consisting of a test label and a + line edit. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + @param filter URLRequester filter + @param mode Describes how a line edit should display its contents. + */ + KPrefsWidPath( KConfigSkeleton::ItemPath *item, TQWidget *parent, + const TQString &filter = TQString(), uint mode = KFile::File ); + /** + Destructor. + */ + virtual ~KPrefsWidPath(); + + /** + Return TQLabel used by this widget. + */ + TQLabel *label(); + /** + Return TQLineEdit used by this widget. + */ + KURLRequester *urlRequester(); + + void readConfig(); + void writeConfig(); + + TQValueList<TQWidget *> widgets() const; + + private: + KConfigSkeleton::ItemPath *mItem; + + TQLabel *mLabel; + KURLRequester *mURLRequester; +}; + + +/** + @short Class for managing KPrefsWid objects. + + This class manages standard configuration widgets provided bz the KPrefsWid + subclasses. It handles creation, loading, saving and default values in a + transparent way. The user has to add the widgets by the corresponding addWid + functions and KPrefsWidManager handles the rest automatically. +*/ +class KDE_EXPORT KPrefsWidManager +{ + public: + /** + Create a KPrefsWidManager object for a KPrefs object. + + @param prefs KPrefs object used to access te configuration. + */ + KPrefsWidManager( KConfigSkeleton *prefs ); + /** + Destructor. + */ + virtual ~KPrefsWidManager(); + + KConfigSkeleton *prefs() const { return mPrefs; } + + /** + Register a custom KPrefsWid object. + */ + virtual void addWid( KPrefsWid * ); + + /** + Register a @ref KPrefsWidBool object. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidBool *addWidBool( KConfigSkeleton::ItemBool *item, + TQWidget *parent ); + + /** + Register a @ref KPrefsWidInt object. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidInt *addWidInt( KConfigSkeleton::ItemInt *item, + TQWidget *parent ); + + /** + Register a @ref KPrefsWidDate object. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidDate *addWidDate( KConfigSkeleton::ItemDateTime *item, + TQWidget *parent ); + + /** + Register a @ref KPrefsWidTime object. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidTime *addWidTime( KConfigSkeleton::ItemDateTime *item, + TQWidget *parent ); + + /** + Register a @ref KPrefsWidDuration object. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidDuration *addWidDuration( KConfigSkeleton::ItemDateTime *item, + TQWidget *parent ); + + /** + Register a @ref KPrefsWidColor object. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidColor *addWidColor( KConfigSkeleton::ItemColor *item, + TQWidget *parent ); + + /** + Register a @ref KPrefsWidRadios object. The choices represented by the + given item object are automatically added as radio buttons. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidRadios *addWidRadios( KConfigSkeleton::ItemEnum *item, + TQWidget *parent ); + + /** + Register a @ref KPrefsWidCombo object. The choices represented by the + given item object are automatically added to the combo box. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidCombo *addWidCombo( KConfigSkeleton::ItemEnum *item, + TQWidget *parent ); + + /** + Register a @ref KPrefsWidString object. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidString *addWidString( KConfigSkeleton::ItemString *item, + TQWidget *parent ); + + /** + Register a path @ref KPrefsWidPath object. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + @param filter URLRequester filter + @param mode URLRequester mode + */ + KPrefsWidPath *addWidPath ( KConfigSkeleton::ItemPath *item, TQWidget *parent, + const TQString &filter = TQString(), + uint mode = KFile::File ); + + /** + Register a password @ref KPrefsWidString object, with echomode set to TQLineEdit::Password. + + @param item The KConfigSkeletonItem representing the preferences entry. + @param parent Parent widget. + */ + KPrefsWidString *addWidPassword ( KConfigSkeleton::ItemString *item, + TQWidget *parent ); + + /** + Register a @ref KPrefsWidFont object. + + @param item The KConfigSkeletonItem representing the preferences + entry. + @param parent Parent widget. + @param sampleText Sample text for previewing the selected font. + */ + KPrefsWidFont *addWidFont( KConfigSkeleton::ItemFont *item, + TQWidget *parent, const TQString &sampleText ); + + /** Set all widgets to default values. */ + void setWidDefaults(); + + /** Read preferences from config file. */ + void readWidConfig(); + + /** Write preferences to config file. */ + void writeWidConfig(); + + private: + KConfigSkeleton *mPrefs; + + TQPtrList<KPrefsWid> mPrefsWids; +}; + + +/** + @short Base class for a preferences dialog. + + This class provides the framework for a preferences dialog. You have to + subclass it and add the code to create the actual configuration widgets and + do the tqlayout management. + + KPrefsDialog provides functions to add subclasses of @ref KPrefsWid via + KPrefsWidManager. For these widgets the reading, writing and setting to + default values is handled automatically. Custom widgets have to be handled in + the functions @ref usrReadConfig() and @ref usrWriteConfig(). +*/ +class KDE_EXPORT KPrefsDialog : public KDialogBase, public KPrefsWidManager +{ + Q_OBJECT + TQ_OBJECT + public: + /** + Create a KPrefsDialog for a KPrefs object. + + @param prefs KPrefs object used to access te configuration. + @param parent Parent widget. + @param name Widget name. + @param modal true, if dialog has to be modal, false for non-modal. + */ + KPrefsDialog( KConfigSkeleton *prefs, TQWidget *parent = 0, char *name = 0, + bool modal = false ); + /** + Destructor. + */ + virtual ~KPrefsDialog(); + + void autoCreate(); + + public slots: + /** Set all widgets to default values. */ + void setDefaults(); + + /** Read preferences from config file. */ + void readConfig(); + + /** Write preferences to config file. */ + void writeConfig(); + + signals: + /** Emitted when the a changed configuration has been stored. */ + void configChanged(); + + protected slots: + /** Apply changes to preferences */ + void slotApply(); + + /** Accept changes to preferences and close dialog */ + void slotOk(); + + /** Set preferences to default values */ + void slotDefault(); + + protected: + /** Implement this to read custom configuration widgets. */ + virtual void usrReadConfig() {} + /** Implement this to write custom configuration widgets. */ + virtual void usrWriteConfig() {} +}; + + +class KDE_EXPORT KPrefsModule : public KCModule, public KPrefsWidManager +{ + Q_OBJECT + TQ_OBJECT + public: + KPrefsModule( KConfigSkeleton *, TQWidget *parent = 0, const char *name = 0 ); + + virtual void addWid( KPrefsWid * ); + + void load(); + void save(); + void defaults(); + + protected slots: + void slotWidChanged(); + + protected: + /** Implement this to read custom configuration widgets. */ + virtual void usrReadConfig() {} + /** Implement this to write custom configuration widgets. */ + virtual void usrWriteConfig() {} +}; + +#endif diff --git a/libtdepim/kregexp3.cpp b/libtdepim/kregexp3.cpp new file mode 100644 index 000000000..c4efa96f1 --- /dev/null +++ b/libtdepim/kregexp3.cpp @@ -0,0 +1,183 @@ +/* -*- c++ -*- + kregexp3.cpp + + This file is part of libkdenetwork. + Copyright (c) 2001 Marc Mutz <mutz@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this library with any edition of + the TQt library by Trolltech AS, Norway (or with modified versions + of TQt that use the same license as TQt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + TQt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#include "kregexp3.h" + +// #define DEBUG_KREGEXP3 + +#ifdef DEBUG_KREGEXP3 +#include <kdebug.h> +#endif + +TQString KRegExp3::replace( const TQString & str, + const TQString & replacementStr, + int start, bool global ) +{ + int oldpos, pos; + + //-------- parsing the replacementStr into + //-------- literal parts and backreferences: + TQStringList literalStrs; + TQValueList<int> backRefs; + + // Due to LTS: The regexp in unquoted form and with spaces: + // \\ (\d) | \$ (\d) | \$ \{ (\d+) \} + TQRegExp rx( "\\\\(\\d)|\\$(\\d)|\\$\\{(\\d+)\\}" ); + TQRegExp bbrx("\\\\"); + TQRegExp brx("\\"); + +#ifdef DEBUG_KREGEXP3 + kdDebug() << "Analyzing replacementStr: \"" + replacementStr + "\"" << endl; +#endif + + oldpos = 0; + pos = 0; + while ( true ) { + pos = rx.search( replacementStr, pos ); + +#ifdef DEBUG_KREGEXP3 + kdDebug() << TQString(" Found match at pos %1").tqarg(pos) << endl; +#endif + + if ( pos < 0 ) { + literalStrs << replacementStr.mid( oldpos ) + .replace( bbrx, "\\" ) + .replace( brx, "" ); +#ifdef DEBUG_KREGEXP3 + kdDebug() << " No more matches. Last literal is \"" + literalStrs.last() + "\"" << endl; +#endif + break; + } else { + literalStrs << replacementStr.mid( oldpos, pos-oldpos ) + .replace( bbrx, "\\" ) + .replace( brx, "" ); +#ifdef DEBUG_KREGEXP3 + kdDebug() << TQString(" Inserting \"") + literalStrs.last() + "\" as literal." << endl; + kdDebug() << " Searching for corresponding digit(s):" << endl; +#endif + for ( int i = 1 ; i < 4 ; i++ ) + if ( !rx.cap(i).isEmpty() ) { + backRefs << rx.cap(i).toInt(); +#ifdef DEBUG_KREGEXP3 + kdDebug() << TQString(" Found %1 at position %2 in the capturedTexts.") + .tqarg(backRefs.last()).tqarg(i) << endl; +#endif + break; + } + pos += rx.matchedLength(); +#ifdef DEBUG_KREGEXP3 + kdDebug() << TQString(" Setting new pos to %1.").tqarg(pos) << endl; +#endif + oldpos = pos; + } + } + +#ifdef DEBUG_KREGEXP3 + kdDebug() << "Finished the analysis of replacementStr!" << endl; +#endif + Q_ASSERT( literalStrs.count() == backRefs.count() + 1 ); + + //-------- actual construction of the + //-------- resulting TQString + TQString result = ""; + oldpos = 0; + pos = start; + + TQStringList::Iterator sIt; + TQValueList<int>::Iterator iIt; + + if ( start < 0 ) + start += str.length(); + +#ifdef DEBUG_KREGEXP3 + kdDebug() << "Constructing the resultant string starts now:" << endl; +#endif + + while ( pos < (int)str.length() ) { + pos = search( str, pos ); + +#ifdef DEBUG_KREGEXP3 + kdDebug() << TQString(" Found match at pos %1").tqarg(pos) << endl; +#endif + + if ( pos < 0 ) { + result += str.mid( oldpos ); +#ifdef DEBUG_KREGEXP3 + kdDebug() << " No more matches. Adding trailing part from str:" << endl; + kdDebug() << " result == \"" + result + "\"" << endl; +#endif + break; + } else { + result += str.mid( oldpos, pos-oldpos ); +#ifdef DEBUG_KREGEXP3 + kdDebug() << " Adding unchanged part from str:" << endl; + kdDebug() << " result == \"" + result + "\"" << endl; +#endif + for ( sIt = literalStrs.begin(), iIt = backRefs.begin() ; + iIt != backRefs.end() ; ++sIt, ++iIt ) { + result += (*sIt); +#ifdef DEBUG_KREGEXP3 + kdDebug() << " Adding literal replacement part:" << endl; + kdDebug() << " result == \"" + result + "\"" << endl; +#endif + result += cap( (*iIt) ); +#ifdef DEBUG_KREGEXP3 + kdDebug() << " Adding captured string:" << endl; + kdDebug() << " result == \"" + result + "\"" << endl; +#endif + } + result += (*sIt); +#ifdef DEBUG_KREGEXP3 + kdDebug() << " Adding literal replacement part:" << endl; + kdDebug() << " result == \"" + result + "\"" << endl; +#endif + } + if (matchedLength() == 0 && pos == 0) { + // if we matched the begin of the string, then better avoid endless + // recursion + result += str.mid( oldpos ); + break; + } + pos += matchedLength(); +#ifdef DEBUG_KREGEXP3 + kdDebug() << TQString(" Setting new pos to %1.").tqarg(pos) << endl; +#endif + oldpos = pos; + + if ( !global ) { + // only replace the first occurrence, so stop here: + result += str.mid( oldpos ); + break; + } + } + + return result; +} diff --git a/libtdepim/kregexp3.h b/libtdepim/kregexp3.h new file mode 100644 index 000000000..e5013366c --- /dev/null +++ b/libtdepim/kregexp3.h @@ -0,0 +1,111 @@ +/* -*- c++ -*- + kregexp3.h + + This file is part of libkdenetwork. + Copyright (c) 2001 Marc Mutz <mutz@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this library with any edition of + the TQt library by Trolltech AS, Norway (or with modified versions + of TQt that use the same license as TQt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + TQt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#include <tqglobal.h> +#include <tqregexp.h> + +#include <tqstring.h> + +#include <tdepimmacros.h> + +/** @short A TQRegExp (TQt3.x) with a replace() method. + + This class is simply there to provide a namespace for some nice + enhancements of the mighty TQRegExp (TQt3 version) regular + expression engine, namely the method replace(), which can be + used to do search-and-replace like one is used to from perl or sed. + + It "simply" adds the ability to define a replacement string which + contains references to the captured substrings. The following + constructs are understood, which can be freely mixed in the + replacement string: + + @section Sed syntax + + Back references in the replacement string are made using \n + (backslash-digit), where @p n is a single digit. With this mode of + operation, only the first nine captured substrings can be + referenced. + + NOTE: Remember that C++ interprets the backslash in string + constants, so you have to write a backslash as "\\". + + @section Perl syntax + + Back references in the replacement string are made using $n + (dollarsign-digit), where @p n is a single digit. With this mode + of operation, only the first nine captured substrings can be + referenced. + + Additionally, Perl supports the syntax ${nn} + (dollarSign-leftCurlyBrace-digits-rightCurlyBrace), where @p nn + can be a multi-digit number. + + In all modes, counting of captured substrings starts with 1 (one)! + To reference the entire matched string, use $0, ${0} or \\0. + + @author Marc Mutz <mutz@kde.org> + @see TQRegExp +*/ + +class KDE_EXPORT KRegExp3 : public TQRegExp +{ +public: + KRegExp3() + : TQRegExp() {} + KRegExp3( const TQString & pattern, + bool caseSensitive = TRUE, + bool wildcard = FALSE ) + : TQRegExp( pattern, caseSensitive, wildcard ) {} + KRegExp3( const TQRegExp & rx ) + : TQRegExp( rx ) {} + KRegExp3( const KRegExp3 & rx ) + : TQRegExp( (TQRegExp)rx ) {} + + /** Replaces each matching subpattern in @p str with + @p replacementStr, inserting captured substrings for + \\n, $n and ${nn} as described in the class documentation. + @param str The source string. + @param replacementStr The string which replaces matched + substrings of @p str. + @param start Start position for the search. + If @p start is negative, starts @p -(start) positions + from the end of @p str. + @param global If @p TRUE, requests to replace all occurrences + of the regexp with @p replacementStr; if @p FALSE, + only the first occurrence will be replaced. + Equivalent to the /g switch to perl's s/// operator. + @return The modified string. + */ + TQString replace( const TQString & str, + const TQString & replacementStr, + int start=0, bool global=TRUE ); +}; diff --git a/libtdepim/kresourceprefs.cpp b/libtdepim/kresourceprefs.cpp new file mode 100644 index 000000000..c343f806d --- /dev/null +++ b/libtdepim/kresourceprefs.cpp @@ -0,0 +1,38 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <tqstring.h> + +#include "kresourceprefs.h" + +KResourcePrefs::KResourcePrefs( const TQString &name ) + : KConfigSkeleton( name ) +{ +} + +void KResourcePrefs::addGroupPrefix( const TQString &prefix ) +{ + KConfigSkeletonItem::List itemList = items(); + KConfigSkeletonItem::List::Iterator it; + + for ( it = itemList.begin(); it != itemList.end(); ++it ) + (*it)->setGroup( prefix + ":" + (*it)->group() ); +} diff --git a/libtdepim/kresourceprefs.h b/libtdepim/kresourceprefs.h new file mode 100644 index 000000000..6d96deadb --- /dev/null +++ b/libtdepim/kresourceprefs.h @@ -0,0 +1,49 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef KRESOURCEPREFS_H +#define KRESOURCEPREFS_H + +#include <kconfigskeleton.h> +#include <tdepimmacros.h> + +class TQString; + +/** + This is a base class for all KPrefs objects, where multiple instances want + to work on the same config file. + By calling addGroupPrefix( "foobar" ), 'foobar' as a prefix is added to the + group names in the configuration file. + The prefix should be an unique identifier to avoid name clashes and the method + has to be called before readConfig(), otherwise the wrong entries are read. + */ +class KDE_EXPORT KResourcePrefs : public KConfigSkeleton +{ + public: + KResourcePrefs( const TQString &name = TQString() ); + + /** + Adds a prefix to all groups of this prefs object. + */ + void addGroupPrefix( const TQString &prefix ); +}; + +#endif diff --git a/libtdepim/krsqueezedtextlabel.cpp b/libtdepim/krsqueezedtextlabel.cpp new file mode 100644 index 000000000..ef9746ee0 --- /dev/null +++ b/libtdepim/krsqueezedtextlabel.cpp @@ -0,0 +1,86 @@ +/* This file has been copied from the KDE libraries and slightly modified. + This can be removed as soon as kdelibs provides the same functionality. + + Copyright (C) 2000 Ronny Standtke <Ronny.Standtke@gmx.de> + + 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 "krsqueezedtextlabel.h" +#include "kstringhandler.h" +#include <tqtooltip.h> + +KRSqueezedTextLabel::KRSqueezedTextLabel( const TQString &text , TQWidget *parent, const char *name ) + : TQLabel ( parent, name ) { + tqsetSizePolicy(TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed )); + fullText = text; + squeezeTextToLabel(); +} + +KRSqueezedTextLabel::KRSqueezedTextLabel( TQWidget *parent, const char *name ) + : TQLabel ( parent, name ) { + tqsetSizePolicy(TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed )); +} + +void KRSqueezedTextLabel::resizeEvent( TQResizeEvent * ) { + squeezeTextToLabel(); +} + +TQSize KRSqueezedTextLabel::tqminimumSizeHint() const +{ + TQSize sh = TQLabel::tqminimumSizeHint(); + sh.setWidth(-1); + return sh; +} + +TQSize KRSqueezedTextLabel::tqsizeHint() const +{ + return TQSize(contentsRect().width(), TQLabel::tqsizeHint().height()); +} + +void KRSqueezedTextLabel::setText( const TQString &text ) { + fullText = text; + squeezeTextToLabel(); +} + +void KRSqueezedTextLabel::squeezeTextToLabel() { + TQFontMetrics fm(fontMetrics()); + int labelWidth = size().width(); + int textWidth = fm.width(fullText); + if (textWidth > labelWidth) { + TQString squeezedText = KStringHandler::rPixelSqueeze(fullText, fm, labelWidth); + TQLabel::setText(squeezedText); + + TQToolTip::remove( this ); + TQToolTip::add( this, fullText ); + + } else { + TQLabel::setText(fullText); + + TQToolTip::remove( this ); + TQToolTip::hide(); + + } +} + +void KRSqueezedTextLabel::tqsetAlignment( int tqalignment ) +{ + // save fullText and restore it + TQString tmpFull(fullText); + TQLabel::tqsetAlignment(tqalignment); + fullText = tmpFull; +} + +#include "krsqueezedtextlabel.moc" diff --git a/libtdepim/krsqueezedtextlabel.h b/libtdepim/krsqueezedtextlabel.h new file mode 100644 index 000000000..36bd209b6 --- /dev/null +++ b/libtdepim/krsqueezedtextlabel.h @@ -0,0 +1,75 @@ +/* This file has been copied from the KDE libraries and slightly modified. + This can be removed as soon as kdelibs provides the same functionality. + + Copyright (C) 2000 Ronny Standtke <Ronny.Standtke@gmx.de> + + 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. +*/ + +#ifndef KRSTQUEEZEDTEXTLABEL_H +#define KRSTQUEEZEDTEXTLABEL_H + +#include <tqlabel.h> +#include <tdepimmacros.h> + +/** + * @short A replacement for TQLabel that squeezes its text + * + * A label class that squeezes its text into the label + * + * If the text is too long to fit into the label it is divided into + * remaining left and right parts which are separated by three dots. + * + * @author Ronny Standtke <Ronny.Standtke@gmx.de> + */ + +/* + * TQLabel + */ +class KDE_EXPORT KRSqueezedTextLabel : public TQLabel { + Q_OBJECT + TQ_OBJECT + +public: + /** + * Default constructor. + */ + KRSqueezedTextLabel( TQWidget *parent, const char *name = 0 ); + KRSqueezedTextLabel( const TQString &text, TQWidget *parent, const char *name = 0 ); + + virtual TQSize tqminimumSizeHint() const; + virtual TQSize tqsizeHint() const; + /** + * Overridden for internal reasons; the API remains unaffected. + */ + virtual void tqsetAlignment( int ); + +public slots: + void setText( const TQString & ); + +protected: + /** + * used when widget is resized + */ + void resizeEvent( TQResizeEvent * ); + /** + * does the dirty work + */ + void squeezeTextToLabel(); + TQString fullText; + +}; + +#endif // KRSTQUEEZEDTEXTLABEL_H diff --git a/libtdepim/kscoring.cpp b/libtdepim/kscoring.cpp new file mode 100644 index 000000000..cf02d88f4 --- /dev/null +++ b/libtdepim/kscoring.cpp @@ -0,0 +1,1234 @@ +/* + kscoring.cpp + + Copyright (c) 2001 Mathias Waack + Copyright (C) 2005 by Volker Krause <volker.krause@rwth-aachen.de> + + Author: Mathias Waack <mathias@atoll-net.de> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US +*/ +#ifdef KDE_USE_FINAL +#undef TQT_NO_ASCII_CAST +#endif + +#undef TQT_NO_COMPAT + +#include <iostream> + +#include <tqfile.h> +#include <tqdom.h> +#include <tqlayout.h> +#include <tqlabel.h> +#include <tqcheckbox.h> +#include <tqtextview.h> + +#include <klocale.h> +#include <kstandarddirs.h> +#include <kdebug.h> +#include <kinputdialog.h> + +#include "kscoring.h" +#include "kscoringeditor.h" + + +//---------------------------------------------------------------------------- +// a small function to encode attribute values, code stolen from TQDom +static TQString toXml(const TQString& str) +{ + TQString tmp(str); + uint len = tmp.length(); + uint i = 0; + while ( i < len ) { + if (tmp[(int)i] == '<') { + tmp.replace(i, 1, "<"); + len += 3; + i += 4; + } else if (tmp[(int)i] == '"') { + tmp.replace(i, 1, """); + len += 5; + i += 6; + } else if (tmp[(int)i] == '&') { + tmp.replace(i, 1, "&"); + len += 4; + i += 5; + } else if (tmp[(int)i] == '>') { + tmp.replace(i, 1, ">"); + len += 3; + i += 4; + } else { + ++i; + } + } + + return tmp; +} + + +// small dialog to display the messages from NotifyAction +NotifyDialog* NotifyDialog::me = 0; +NotifyDialog::NotesMap NotifyDialog::dict; + +NotifyDialog::NotifyDialog(TQWidget* p) + : KDialogBase(p,"notify action dialog",true,"Notify Message",Close,Close,true) +{ + TQFrame *f = makeMainWidget(); + TQVBoxLayout *topL = new TQVBoxLayout(f); + note = new TQLabel(f); + note->setTextFormat(RichText); + topL->addWidget(note); + TQCheckBox *check = new TQCheckBox(i18n("Do not show this message again"),f); + check->setChecked(true); + topL->addWidget(check); + connect(check,TQT_SIGNAL(toggled(bool)),TQT_SLOT(slotShowAgainToggled(bool))); +} + +void NotifyDialog::slotShowAgainToggled(bool flag) +{ + dict.replace(msg,!flag); + kdDebug(5100) << "note \"" << note << "\" will popup again: " << flag << endl; +} + +void NotifyDialog::display(ScorableArticle& a, const TQString& s) +{ + kdDebug(5100) << "displaying message" << endl; + if (!me) me = new NotifyDialog(); + me->msg = s; + + NotesMap::Iterator i = dict.find(s); + if (i == dict.end() || i.data()) { + TQString msg = i18n("Article\n<b>%1</b><br><b>%2</b><br>caused the following" + " note to appear:<br>%3"). + arg(a.from()). + arg(a.subject()). + arg(s); + me->note->setText(msg); + if ( i == dict.end() ) i = dict.replace(s,false); + me->adjustSize(); + me->exec(); + } +} + + +//---------------------------------------------------------------------------- +ScorableArticle::~ScorableArticle() +{ +} + +void ScorableArticle::displayMessage(const TQString& note) +{ + NotifyDialog::display(*this,note); +} + +//---------------------------------------------------------------------------- +ScorableGroup::~ScorableGroup() +{ +} + +// the base class for all actions +ActionBase::ActionBase() +{ + kdDebug(5100) << "new Action " << this << endl; +} + +ActionBase::~ActionBase() +{ + kdDebug(5100) << "delete Action " << this << endl; +} + + +TQStringList ActionBase::userNames() +{ + TQStringList l; + l << userName(SETSCORE); + l << userName(NOTIFY); + l << userName(COLOR); + l << userName(MARKASREAD); + return l; +} + +ActionBase* ActionBase::factory(int type, const TQString &value) +{ + switch (type) { + case SETSCORE: return new ActionSetScore(value); + case NOTIFY: return new ActionNotify(value); + case COLOR: return new ActionColor(value); + case MARKASREAD: return new ActionMarkAsRead(); + default: + kdWarning(5100) << "unknown type " << type << " in ActionBase::factory()" << endl; + return 0; + } +} + +TQString ActionBase::userName(int type) +{ + switch (type) { + case SETSCORE: return i18n("Adjust Score"); + case NOTIFY: return i18n("Display Message"); + case COLOR: return i18n("Colorize Header"); + case MARKASREAD: return i18n("Mark As Read"); + default: + kdWarning(5100) << "unknown type " << type << " in ActionBase::userName()" << endl; + return 0; + } +} + +int ActionBase::getTypeForName(const TQString& name) +{ + if (name == "SETSCORE") return SETSCORE; + else if (name == "NOTIFY") return NOTIFY; + else if (name == "COLOR") return COLOR; + else if (name == "MARKASREAD") return MARKASREAD; + else { + kdWarning(5100) << "unknown type string " << name + << " in ActionBase::getTypeForName()" << endl; + return -1; + } +} + +int ActionBase::getTypeForUserName(const TQString& name) +{ + if (name == userName(SETSCORE)) return SETSCORE; + else if (name == userName(NOTIFY)) return NOTIFY; + else if (name == userName(COLOR)) return COLOR; + else if ( name == userName(MARKASREAD) ) return MARKASREAD; + else { + kdWarning(5100) << "unknown type string " << name + << " in ActionBase::getTypeForUserName()" << endl; + return -1; + } +} + +// the set score action +ActionSetScore::ActionSetScore(short v) + : val(v) +{ +} + +ActionSetScore::ActionSetScore(const TQString& s) +{ + val = s.toShort(); +} + +ActionSetScore::ActionSetScore(const ActionSetScore& as) + : ActionBase(), + val(as.val) +{ +} + +ActionSetScore::~ActionSetScore() +{ +} + +TQString ActionSetScore::toString() const +{ + TQString a; + a += "<Action type=\"SETSCORE\" value=\"" + TQString::number(val) + "\" />"; + return a; +} + +void ActionSetScore::apply(ScorableArticle& a) const +{ + a.addScore(val); +} + +ActionSetScore* ActionSetScore::clone() const +{ + return new ActionSetScore(*this); +} + +// the color action +ActionColor::ActionColor(const TQColor& c) + : ActionBase(), color(c) +{ +} + +ActionColor::ActionColor(const TQString& s) + : ActionBase() +{ + setValue(s); +} + +ActionColor::ActionColor(const ActionColor& a) + : ActionBase(), color(a.color) +{ +} + +ActionColor::~ActionColor() +{} + +TQString ActionColor::toString() const +{ + TQString a; + a += "<Action type=\"COLOR\" value=\"" + toXml(color.name()) + "\" />"; + return a; +} + +void ActionColor::apply(ScorableArticle& a) const +{ + a.changeColor(color); +} + +ActionColor* ActionColor::clone() const +{ + return new ActionColor(*this); +} + + +// the notify action +ActionNotify::ActionNotify(const TQString& s) +{ + note = s; +} + +ActionNotify::ActionNotify(const ActionNotify& an) + : ActionBase() +{ + note = an.note; +} + +TQString ActionNotify::toString() const +{ + return "<Action type=\"NOTIFY\" value=\"" + toXml(note) + "\" />"; +} + +void ActionNotify::apply(ScorableArticle& a) const +{ + a.displayMessage(note); +} + +ActionNotify* ActionNotify::clone() const +{ + return new ActionNotify(*this); +} + + +// mark as read action +ActionMarkAsRead::ActionMarkAsRead() : + ActionBase() +{ +} + +ActionMarkAsRead::ActionMarkAsRead( const ActionMarkAsRead &action ) : + ActionBase() +{ + Q_UNUSED( action ); +} + +TQString ActionMarkAsRead::toString() const +{ + return "<Action type=\"MARKASREAD\"/>"; +} + +void ActionMarkAsRead::apply( ScorableArticle &article ) const +{ + article.markAsRead(); +} + +ActionMarkAsRead* ActionMarkAsRead::clone() const +{ + return new ActionMarkAsRead(*this); +} + +//---------------------------------------------------------------------------- +NotifyCollection::NotifyCollection() +{ + notifyList.setAutoDelete(true); +} + +NotifyCollection::~NotifyCollection() +{ +} + +void NotifyCollection::addNote(const ScorableArticle& a, const TQString& note) +{ + article_list *l = notifyList.find(note); + if (!l) { + notifyList.insert(note,new article_list); + l = notifyList.find(note); + } + article_info i; + i.from = a.from(); + i.subject = a.subject(); + l->append(i); +} + +TQString NotifyCollection::collection() const +{ + TQString notifyCollection = i18n("<h1>List of collected notes</h1>"); + notifyCollection += "<p><ul>"; + // first look thru the notes and create one string + TQDictIterator<article_list> it(notifyList); + for(;it.current();++it) { + const TQString& note = it.currentKey(); + notifyCollection += "<li>" + note + "<ul>"; + article_list* alist = it.current(); + article_list::Iterator ait; + for(ait = alist->begin(); ait != alist->end(); ++ait) { + notifyCollection += "<li><b>From: </b>" + (*ait).from + "<br>"; + notifyCollection += "<b>Subject: </b>" + (*ait).subject; + } + notifyCollection += "</ul>"; + } + notifyCollection += "</ul>"; + + return notifyCollection; +} + +void NotifyCollection::displayCollection(TQWidget *p) const +{ + //KMessageBox::information(p,collection(),i18n("Collected Notes")); + KDialogBase *dlg = new KDialogBase( p, 0, false, i18n("Collected Notes"), + KDialogBase::Close, KDialogBase::Close ); + TQTextView *text = new TQTextView(dlg); + text->setText(collection()); + dlg->setMainWidget(text); + dlg->setMinimumWidth(300); + dlg->setMinimumHeight(300); + dlg->show(); +} + +//---------------------------------------------------------------------------- +KScoringExpression::KScoringExpression(const TQString& h, const TQString& t, const TQString& n, const TQString& ng) + : header(h), expr_str(n) +{ + if (t == "MATCH" ) { + cond = MATCH; + expr.setPattern(expr_str); + expr.setCaseSensitive(false); + } + else if ( t == "MATCHCS" ) { + cond = MATCHCS; + expr.setPattern( expr_str ); + expr.setCaseSensitive( true ); + } + else if (t == "CONTAINS" ) cond = CONTAINS; + else if (t == "ETQUALS" ) cond = ETQUALS; + else if (t == "GREATER") { + cond = GREATER; + expr_int = expr_str.toInt(); + } + else if (t == "SMALLER") { + cond = SMALLER; + expr_int = expr_str.toInt(); + } + else { + kdDebug(5100) << "unknown match type in new expression" << endl; + } + + neg = ng.toInt(); + c_header = header.latin1(); + + kdDebug(5100) << "new expr: " << c_header << " " << t << " " + << expr_str << " " << neg << endl; +} + +// static +int KScoringExpression::getConditionForName(const TQString& s) +{ + if (s == getNameForCondition(CONTAINS)) return CONTAINS; + else if (s == getNameForCondition(MATCH)) return MATCH; + else if (s == getNameForCondition(MATCHCS)) return MATCHCS; + else if (s == getNameForCondition(ETQUALS)) return ETQUALS; + else if (s == getNameForCondition(SMALLER)) return SMALLER; + else if (s == getNameForCondition(GREATER)) return GREATER; + else { + kdWarning(5100) << "unknown condition name " << s + << " in KScoringExpression::getConditionForName()" << endl; + return -1; + } +} + +// static +TQString KScoringExpression::getNameForCondition(int cond) +{ + switch (cond) { + case CONTAINS: return i18n("Contains Substring"); + case MATCH: return i18n("Matches Regular Expression"); + case MATCHCS: return i18n("Matches Regular Expression (Case Sensitive)"); + case ETQUALS: return i18n("Is Exactly the Same As"); + case SMALLER: return i18n("Less Than"); + case GREATER: return i18n("Greater Than"); + default: + kdWarning(5100) << "unknown condition " << cond + << " in KScoringExpression::getNameForCondition()" << endl; + return ""; + } +} + +// static +TQStringList KScoringExpression::conditionNames() +{ + TQStringList l; + l << getNameForCondition(CONTAINS); + l << getNameForCondition(MATCH); + l << getNameForCondition(MATCHCS); + l << getNameForCondition(ETQUALS); + l << getNameForCondition(SMALLER); + l << getNameForCondition(GREATER); + return l; +} + +// static +TQStringList KScoringExpression::headerNames() +{ + TQStringList l; + l.append("From"); + l.append("Message-ID"); + l.append("Subject"); + l.append("Date"); + l.append("References"); + l.append("NNTP-Posting-Host"); + l.append("Bytes"); + l.append("Lines"); + l.append("Xref"); + return l; +} + +KScoringExpression::~KScoringExpression() +{ +} + +bool KScoringExpression::match(ScorableArticle& a) const +{ + //kdDebug(5100) << "matching against header " << c_header << endl; + bool res = true; + TQString head; + + if (header == "From") + head = a.from(); + else if (header == "Subject") + head = a.subject(); + else + head = a.getHeaderByType(c_header); + + if (!head.isEmpty()) { + switch (cond) { + case ETQUALS: + res = (head.lower() == expr_str.lower()); + break; + case CONTAINS: + res = (head.lower().find(expr_str.lower()) >= 0); + break; + case MATCH: + case MATCHCS: + res = (expr.search(head)!=-1); + break; + case GREATER: + res = (head.toInt() > expr_int); + break; + case SMALLER: + res = (head.toInt() < expr_int); + break; + default: + kdDebug(5100) << "unknown match" << endl; + res = false; + } + } + else res = false; +// kdDebug(5100) << "matching returns " << res << endl; + return (neg)?!res:res; +} + +void KScoringExpression::write(TQTextStream& st) const +{ + st << toString(); +} + +TQString KScoringExpression::toString() const +{ +// kdDebug(5100) << "KScoringExpression::toString() starts" << endl; +// kdDebug(5100) << "header is " << header << endl; +// kdDebug(5100) << "expr is " << expr_str << endl; +// kdDebug(5100) << "neg is " << neg << endl; +// kdDebug(5100) << "type is " << getType() << endl; + TQString e; + e += "<Expression neg=\"" + TQString::number(neg?1:0) + + "\" header=\"" + header + + "\" type=\"" + getTypeString() + + "\" expr=\"" + toXml(expr_str) + + "\" />"; +// kdDebug(5100) << "KScoringExpression::toString() finished" << endl; + return e; +} + +TQString KScoringExpression::getTypeString() const +{ + return KScoringExpression::getTypeString(cond); +} + +TQString KScoringExpression::getTypeString(int cond) +{ + switch (cond) { + case CONTAINS: return "CONTAINS"; + case MATCH: return "MATCH"; + case MATCHCS: return "MATCHCS"; + case ETQUALS: return "ETQUALS"; + case SMALLER: return "SMALLER"; + case GREATER: return "GREATER"; + default: + kdWarning(5100) << "unknown cond " << cond << " in KScoringExpression::getTypeString()" << endl; + return ""; + } +} + +int KScoringExpression::getType() const +{ + return cond; +} + +//---------------------------------------------------------------------------- +KScoringRule::KScoringRule(const TQString& n ) + : name(n), link(AND) +{ + expressions.setAutoDelete(true); + actions.setAutoDelete(true); +} + +KScoringRule::KScoringRule(const KScoringRule& r) +{ + kdDebug(5100) << "copying rule " << r.getName() << endl; + name = r.getName(); + expressions.setAutoDelete(true); + actions.setAutoDelete(true); + // copy expressions + expressions.clear(); + const ScoreExprList& rexpr = r.expressions; + TQPtrListIterator<KScoringExpression> it(rexpr); + for ( ; it.current(); ++it ) { + KScoringExpression *t = new KScoringExpression(**it); + expressions.append(t); + } + // copy actions + actions.clear(); + const ActionList& ract = r.actions; + TQPtrListIterator<ActionBase> ait(ract); + for ( ; ait.current(); ++ait ) { + ActionBase *t = *ait; + actions.append(t->clone()); + } + // copy groups, servers, linkmode and expires + groups = r.groups; + expires = r.expires; + link = r.link; +} + +KScoringRule::~KScoringRule() +{ + cleanExpressions(); + cleanActions(); +} + +void KScoringRule::cleanExpressions() +{ + // the expressions is setAutoDelete(true) + expressions.clear(); +} + +void KScoringRule::cleanActions() +{ + // the actions is setAutoDelete(true) + actions.clear(); +} + +void KScoringRule::addExpression( KScoringExpression* expr) +{ + kdDebug(5100) << "KScoringRule::addExpression" << endl; + expressions.append(expr); +} + +void KScoringRule::addAction(int type, const TQString& val) +{ + ActionBase *action = ActionBase::factory(type,val); + addAction(action); +} + +void KScoringRule::addAction(ActionBase* a) +{ + kdDebug(5100) << "KScoringRule::addAction() " << a->toString() << endl; + actions.append(a); +} + +void KScoringRule::setLinkMode(const TQString& l) +{ + if (l == "OR") link = OR; + else link = AND; +} + +void KScoringRule::setExpire(const TQString& e) +{ + if (e != "never") { + TQStringList l = TQStringList::split("-",e); + Q_ASSERT( l.count() == 3 ); + expires.setYMD( (*(l.at(0))).toInt(), + (*(l.at(1))).toInt(), + (*(l.at(2))).toInt()); + } + kdDebug(5100) << "Rule " << getName() << " expires at " << getExpireDateString() << endl; +} + +bool KScoringRule::matchGroup(const TQString& group) const +{ + for(GroupList::ConstIterator i=groups.begin(); i!=groups.end();++i) { + TQRegExp e(*i); + if (e.search(group, 0) != -1 && + (uint)e.matchedLength() == group.length()) + return true; + } + return false; +} + +void KScoringRule::applyAction(ScorableArticle& a) const +{ + TQPtrListIterator<ActionBase> it(actions); + for(; it.current(); ++it) { + it.current()->apply(a); + } +} + +void KScoringRule::applyRule(ScorableArticle& a) const +{ + // kdDebug(5100) << "checking rule " << name << endl; + // kdDebug(5100) << " for article from " + // << a->from()->asUnicodeString() + // << endl; + bool oper_and = (link == AND); + bool res = true; + TQPtrListIterator<KScoringExpression> it(expressions); + //kdDebug(5100) << "checking " << expressions.count() << " expressions" << endl; + for (; it.current(); ++it) { + Q_ASSERT( it.current() ); + res = it.current()->match(a); + if (!res && oper_and) return; + else if (res && !oper_and) break; + } + if (res) applyAction(a); +} + +void KScoringRule::applyRule(ScorableArticle& a /*, const TQString& s*/, const TQString& g) const +{ + // check if one of the groups match + for (TQStringList::ConstIterator i = groups.begin(); i != groups.end(); ++i) { + if (TQRegExp(*i).search(g) != -1) { + applyRule(a); + return; + } + } +} + +void KScoringRule::write(TQTextStream& s) const +{ + s << toString(); +} + +TQString KScoringRule::toString() const +{ + //kdDebug(5100) << "KScoringRule::toString() starts" << endl; + TQString r; + r += "<Rule name=\"" + toXml(name) + "\" linkmode=\"" + getLinkModeName(); + r += "\" expires=\"" + getExpireDateString() + "\">"; + //kdDebug(5100) << "building grouplist..." << endl; + for(GroupList::ConstIterator i=groups.begin();i!=groups.end();++i) { + r += "<Group name=\"" + toXml(*i) + "\" />"; + } + //kdDebug(5100) << "building expressionlist..." << endl; + TQPtrListIterator<KScoringExpression> eit(expressions); + for (; eit.current(); ++eit) { + r += eit.current()->toString(); + } + //kdDebug(5100) << "building actionlist..." << endl; + TQPtrListIterator<ActionBase> ait(actions); + for (; ait.current(); ++ait) { + r += ait.current()->toString(); + } + r += "</Rule>"; + //kdDebug(5100) << "KScoringRule::toString() finished" << endl; + return r; +} + +TQString KScoringRule::getLinkModeName() const +{ + switch (link) { + case AND: return "AND"; + case OR: return "OR"; + default: return "AND"; + } +} + +TQString KScoringRule::getExpireDateString() const +{ + if (expires.isNull()) return "never"; + else { + return TQString::number(expires.year()) + TQString("-") + + TQString::number(expires.month()) + TQString("-") + + TQString::number(expires.day()); + } +} + +bool KScoringRule::isExpired() const +{ + return (expires.isValid() && (expires < TQDate::tqcurrentDate())); +} + + + +//---------------------------------------------------------------------------- +KScoringManager::KScoringManager(const TQString& appName) + : cacheValid(false)//, _s(0) +{ + allRules.setAutoDelete(true); + // determine filename of the scorefile + if(appName.isEmpty()) + mFilename = KGlobal::dirs()->saveLocation("appdata") + "/scorefile"; + else + mFilename = KGlobal::dirs()->saveLocation("data") + "/" + appName + "/scorefile"; + // open the score file + load(); +} + + +KScoringManager::~KScoringManager() +{ +} + +void KScoringManager::load() +{ + TQDomDocument sdoc("Scorefile"); + TQFile f( mFilename ); + if ( !f.open( IO_ReadOnly ) ) + return; + if ( !sdoc.setContent( &f ) ) { + f.close(); + kdDebug(5100) << "loading the scorefile failed" << endl; + return; + } + f.close(); + kdDebug(5100) << "loaded the scorefile, creating internal representation" << endl; + allRules.clear(); + createInternalFromXML(sdoc); + expireRules(); + kdDebug(5100) << "ready, got " << allRules.count() << " rules" << endl; +} + +void KScoringManager::save() +{ + kdDebug(5100) << "KScoringManager::save() starts" << endl; + TQFile f( mFilename ); + if ( !f.open( IO_WriteOnly ) ) + return; + TQTextStream stream(&f); + stream.setEncoding(TQTextStream::Unicode); + kdDebug(5100) << "KScoringManager::save() creating xml" << endl; + createXMLfromInternal().save(stream,2); + kdDebug(5100) << "KScoringManager::save() finished" << endl; +} + +TQDomDocument KScoringManager::createXMLfromInternal() +{ + // I was'nt able to create a TQDomDocument in memory:( + // so I write the content into a string, which is really stupid + TQDomDocument sdoc("Scorefile"); + TQString ss; // scorestring + ss += "<?xml version = '1.0'?><!DOCTYPE Scorefile >"; + ss += toString(); + ss += "</Scorefile>\n"; + kdDebug(5100) << "KScoringManager::createXMLfromInternal():" << endl << ss << endl; + sdoc.setContent(ss); + return sdoc; +} + +TQString KScoringManager::toString() const +{ + TQString s; + s += "<Scorefile>\n"; + TQPtrListIterator<KScoringRule> it(allRules); + for( ; it.current(); ++it) { + s += it.current()->toString(); + } + return s; +} + +void KScoringManager::expireRules() +{ + for ( KScoringRule *cR = allRules.first(); cR; cR=allRules.next()) { + if (cR->isExpired()) { + kdDebug(5100) << "Rule " << cR->getName() << " is expired, deleting it" << endl; + allRules.remove(); + } + } +} + +void KScoringManager::createInternalFromXML(TQDomNode n) +{ + static KScoringRule *cR = 0; // the currentRule + // the XML file was parsed and now we simply traverse the resulting tree + if ( !n.isNull() ) { + kdDebug(5100) << "inspecting node of type " << n.nodeType() + << " named " << n.toElement().tagName() << endl; + + switch (n.nodeType()) { + case TQDomNode::DocumentNode: { + // the document itself + break; + } + case TQDomNode::ElementNode: { + // Server, Newsgroup, Rule, Expression, Action + TQDomElement e = n.toElement(); + //kdDebug(5100) << "The name of the element is " + //<< e.tagName().latin1() << endl; + TQString s = e.tagName(); + if (s == "Rule") { + cR = new KScoringRule(e.attribute("name")); + cR->setLinkMode(e.attribute("linkmode")); + cR->setExpire(e.attribute("expires")); + addRuleInternal(cR); + } + else if (s == "Group") { + Q_CHECK_PTR(cR); + cR->addGroup( e.attribute("name") ); + } + else if (s == "Expression") { + cR->addExpression(new KScoringExpression(e.attribute("header"), + e.attribute("type"), + e.attribute("expr"), + e.attribute("neg"))); + } + else if (s == "Action") { + Q_CHECK_PTR(cR); + cR->addAction(ActionBase::getTypeForName(e.attribute("type")), + e.attribute("value")); + } + break; + } + default: // kdDebug(5100) << "unknown DomNode::type" << endl; + ; + } + TQDomNodeList nodelist = n.childNodes(); + unsigned cnt = nodelist.count(); + //kdDebug(5100) << "recursive checking " << cnt << " nodes" << endl; + for (unsigned i=0;i<cnt;++i) + createInternalFromXML(nodelist.item(i)); + } +} + +KScoringRule* KScoringManager::addRule(const ScorableArticle& a, TQString group, short score) +{ + KScoringRule *rule = new KScoringRule(findUniqueName()); + rule->addGroup( group ); + rule->addExpression( + new KScoringExpression("From","CONTAINS", + a.from(),"0")); + if (score) rule->addAction(new ActionSetScore(score)); + rule->setExpireDate(TQDate::tqcurrentDate().addDays(30)); + addRule(rule); + KScoringEditor *edit = KScoringEditor::createEditor(this); + edit->setRule(rule); + edit->show(); + setCacheValid(false); + return rule; +} + +KScoringRule* KScoringManager::addRule(KScoringRule* expr) +{ + int i = allRules.findRef(expr); + if (i == -1) { + // only add a rule we don't know + addRuleInternal(expr); + } + else { + emit changedRules(); + } + return expr; +} + +KScoringRule* KScoringManager::addRule() +{ + KScoringRule *rule = new KScoringRule(findUniqueName()); + addRule(rule); + return rule; +} + +void KScoringManager::addRuleInternal(KScoringRule *e) +{ + allRules.append(e); + setCacheValid(false); + emit changedRules(); + kdDebug(5100) << "KScoringManager::addRuleInternal " << e->getName() << endl; +} + +void KScoringManager::cancelNewRule(KScoringRule *r) +{ + // if e was'nt previously added to the list of rules, we delete it + int i = allRules.findRef(r); + if (i == -1) { + kdDebug(5100) << "deleting rule " << r->getName() << endl; + deleteRule(r); + } + else { + kdDebug(5100) << "rule " << r->getName() << " not deleted" << endl; + } +} + +void KScoringManager::setRuleName(KScoringRule *r, const TQString& s) +{ + bool cont = true; + TQString text = s; + TQString oldName = r->getName(); + while (cont) { + cont = false; + TQPtrListIterator<KScoringRule> it(allRules); + for (; it.current(); ++it) { + if ( it.current() != r && it.current()->getName() == text ) { + kdDebug(5100) << "rule name " << text << " is not unique" << endl; + text = KInputDialog::getText(i18n("Choose Another Rule Name"), + i18n("The rule name is already assigned, please choose another name:"), + text); + cont = true; + break; + } + } + } + if (text != oldName) { + r->setName(text); + emit changedRuleName(oldName,text); + } +} + +void KScoringManager::deleteRule(KScoringRule *r) +{ + int i = allRules.findRef(r); + if (i != -1) { + allRules.remove(); + emit changedRules(); + } +} + +void KScoringManager::editRule(KScoringRule *e, TQWidget *w) +{ + KScoringEditor *edit = KScoringEditor::createEditor(this, w); + edit->setRule(e); + edit->show(); + delete edit; +} + +void KScoringManager::moveRuleAbove( KScoringRule *above, KScoringRule *below ) +{ + int aindex = allRules.findRef( above ); + int bindex = allRules.findRef( below ); + if ( aindex <= 0 || bindex < 0 ) + return; + if ( aindex < bindex ) + --bindex; + allRules.take( aindex ); + allRules.insert( bindex, above ); +} + +void KScoringManager::moveRuleBelow( KScoringRule *below, KScoringRule *above ) +{ + int bindex = allRules.findRef( below ); + int aindex = allRules.findRef( above ); + if ( bindex < 0 || bindex >= (int)allRules.count() - 1 || aindex < 0 ) + return; + if ( bindex < aindex ) + --aindex; + allRules.take( bindex ); + allRules.insert( aindex + 1, below ); +} + +void KScoringManager::editorReady() +{ + kdDebug(5100) << "emitting signal finishedEditing" << endl; + save(); + emit finishedEditing(); +} + +KScoringRule* KScoringManager::copyRule(KScoringRule *r) +{ + KScoringRule *rule = new KScoringRule(*r); + rule->setName(findUniqueName()); + addRuleInternal(rule); + return rule; +} + +void KScoringManager::applyRules(ScorableGroup* ) +{ + kdWarning(5100) << "KScoringManager::applyRules(ScorableGroup* ) isn't implemented" << endl; +} + +void KScoringManager::applyRules(ScorableArticle& article, const TQString& group) +{ + setGroup(group); + applyRules(article); +} + +void KScoringManager::applyRules(ScorableArticle& a) +{ + TQPtrListIterator<KScoringRule> it(isCacheValid()? ruleList : allRules); + for( ; it.current(); ++it) { + it.current()->applyRule(a); + } +} + +void KScoringManager::initCache(const TQString& g) +{ + group = g; + ruleList.clear(); + TQPtrListIterator<KScoringRule> it(allRules); + for (; it.current(); ++it) { + if ( it.current()->matchGroup(group) ) { + ruleList.append(it.current()); + } + } + kdDebug(5100) << "created cache for group " << group + << " with " << ruleList.count() << " rules" << endl; + setCacheValid(true); +} + +void KScoringManager::setGroup(const TQString& g) +{ + if (group != g) initCache(g); +} + +bool KScoringManager::hasRulesForCurrentGroup() +{ + return ruleList.count() != 0; +} + + +TQStringList KScoringManager::getRuleNames() +{ + TQStringList l; + TQPtrListIterator<KScoringRule> it(allRules); + for( ; it.current(); ++it) { + l << it.current()->getName(); + } + return l; +} + +KScoringRule* KScoringManager::findRule(const TQString& ruleName) +{ + TQPtrListIterator<KScoringRule> it(allRules); + for (; it.current(); ++it) { + if ( it.current()->getName() == ruleName ) { + return it; + } + } + return 0; +} + +bool KScoringManager::setCacheValid(bool v) +{ + bool res = cacheValid; + cacheValid = v; + return res; +} + +TQString KScoringManager::findUniqueName() const +{ + int nr = 0; + TQString ret; + bool duplicated=false; + + while (nr < 99999999) { + nr++; + ret = i18n("rule %1").tqarg(nr); + + duplicated=false; + TQPtrListIterator<KScoringRule> it(allRules); + for( ; it.current(); ++it) { + if (it.current()->getName() == ret) { + duplicated = true; + break; + } + } + + if (!duplicated) + return ret; + } + + return ret; +} + +bool KScoringManager::hasFeature(int p) +{ + switch (p) { + case ActionBase::SETSCORE: return canScores(); + case ActionBase::NOTIFY: return canNotes(); + case ActionBase::COLOR: return canColors(); + case ActionBase::MARKASREAD: return canMarkAsRead(); + default: return false; + } +} + +TQStringList KScoringManager::getDefaultHeaders() const +{ + TQStringList l; + l.append("Subject"); + l.append("From"); + l.append("Date"); + l.append("Message-ID"); + return l; +} + +void KScoringManager::pushRuleList() +{ + stack.push(allRules); +} + +void KScoringManager::popRuleList() +{ + stack.pop(allRules); +} + +void KScoringManager::removeTOS() +{ + stack.drop(); +} + +RuleStack::RuleStack() +{ +} + +RuleStack::~RuleStack() +{} + +void RuleStack::push(TQPtrList<KScoringRule>& l) +{ + kdDebug(5100) << "RuleStack::push pushing list with " << l.count() << " rules" << endl; + KScoringManager::ScoringRuleList *l1 = new KScoringManager::ScoringRuleList; + for ( KScoringRule *r=l.first(); r != 0; r=l.next() ) { + l1->append(new KScoringRule(*r)); + } + stack.push(l1); + kdDebug(5100) << "now there are " << stack.count() << " lists on the stack" << endl; +} + +void RuleStack::pop(TQPtrList<KScoringRule>& l) +{ + top(l); + drop(); + kdDebug(5100) << "RuleStack::pop pops list with " << l.count() << " rules" << endl; + kdDebug(5100) << "now there are " << stack.count() << " lists on the stack" << endl; +} + +void RuleStack::top(TQPtrList<KScoringRule>& l) +{ + l.clear(); + KScoringManager::ScoringRuleList *l1 = stack.top(); + l = *l1; +} + +void RuleStack::drop() +{ + kdDebug(5100) << "drop: now there are " << stack.count() << " lists on the stack" << endl; + stack.remove(); +} + + +#include "kscoring.moc" diff --git a/libtdepim/kscoring.h b/libtdepim/kscoring.h new file mode 100644 index 000000000..7225eb93f --- /dev/null +++ b/libtdepim/kscoring.h @@ -0,0 +1,429 @@ +/* + kscoring.h + + Copyright (c) 2001 Mathias Waack + Copyright (C) 2005 by Volker Krause <volker.krause@rwth-aachen.de> + + Author: Mathias Waack <mathias@atoll-net.de> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US +*/ + +#ifndef KSCORING_H +#define KSCORING_H + +#include <unistd.h> + +#include <tqglobal.h> +#include <tqptrlist.h> +#include <tqptrstack.h> +#include <tqregexp.h> + +#include <tqobject.h> +#include <tqstring.h> +#include <tqstringlist.h> +#include <tqdatetime.h> +#include <tqcolor.h> +#include <tqtable.h> +#include <tqmap.h> +#include <tqdict.h> + +#include <kdialogbase.h> +#include <klineedit.h> +#include <knuminput.h> + +#include <tdepimmacros.h> + +class TQDomNode; +class TQDomDocument; +class TQDomElement; +class TQTextStream; +class TQLabel; + + +/** + The following classes ScorableArticle, ScorableGroup define + the interface for the scoring. Any application using this mechanism should + create its own subclasses of this classes. The scoring itself will be handled + by the ScoringManager class. + */ + +//---------------------------------------------------------------------------- +class KDE_EXPORT ScorableGroup +{ +public: + virtual ~ScorableGroup(); +}; + +class KDE_EXPORT ScorableArticle +{ +public: + virtual ~ScorableArticle(); + + virtual void addScore(short) {} + virtual void displayMessage(const TQString&); + virtual void changeColor(const TQColor&) {} + virtual void markAsRead() {} + virtual TQString from() const = 0; + virtual TQString subject() const = 0; + virtual TQString getHeaderByType(const TQString&) const = 0; + //virtual ScorableGroup group() const =0; +}; + + +//---------------------------------------------------------------------------- +/** + Base class for other Action classes. + */ +class KDE_EXPORT ActionBase { + public: + ActionBase(); + virtual ~ActionBase(); + virtual TQString toString() const =0; + virtual void apply(ScorableArticle&) const =0; + virtual ActionBase* clone() const =0; + virtual int getType() const =0; + virtual TQString getValueString() const { return TQString(); } + virtual void setValue(const TQString&) {} + static ActionBase* factory(int type, const TQString &value); + static TQStringList userNames(); + static TQString userName(int type); + static int getTypeForName(const TQString& name); + static int getTypeForUserName(const TQString& name); + TQString userName() { return userName(getType()); } + enum ActionTypes { SETSCORE, NOTIFY, COLOR, MARKASREAD }; +}; + +class KDE_EXPORT ActionColor : public ActionBase { +public: + ActionColor(const TQColor&); + ActionColor(const TQString&); + ActionColor(const ActionColor&); + virtual ~ActionColor(); + virtual TQString toString() const; + virtual int getType() const { return COLOR; } + virtual TQString getValueString() const { return color.name(); } + virtual void setValue(const TQString& s) { color.setNamedColor(s); } + void setValue(const TQColor& c) { color = c; } + TQColor value() const { return color; } + virtual void apply(ScorableArticle&) const; + virtual ActionColor* clone() const; +private: + TQColor color; +}; + +class KDE_EXPORT ActionSetScore : public ActionBase { + public: + ActionSetScore(short); + ActionSetScore(const ActionSetScore&); + ActionSetScore(const TQString&); + virtual ~ActionSetScore(); + virtual TQString toString() const; + virtual int getType() const { return SETSCORE; } + virtual TQString getValueString() const { return TQString::number(val); } + virtual void setValue(const TQString& s) { val = s.toShort(); } + void setValue(short v) { val = v; } + short value() const { return val; } + virtual void apply(ScorableArticle&) const; + virtual ActionSetScore* clone() const; + private: + short val; +}; + +class KDE_EXPORT ActionNotify : public ActionBase { + public: + ActionNotify(const TQString&); + ActionNotify(const ActionNotify&); + virtual ~ActionNotify() {} + virtual TQString toString() const; + virtual int getType() const { return NOTIFY; } + virtual TQString getValueString() const { return note; } + virtual void setValue(const TQString& s) { note = s; } + virtual void apply(ScorableArticle&) const; + virtual ActionNotify* clone() const; + private: + TQString note; +}; + +class KDE_EXPORT ActionMarkAsRead : public ActionBase { + public: + ActionMarkAsRead(); + ActionMarkAsRead( const ActionMarkAsRead& ); + virtual ~ActionMarkAsRead() {} + virtual TQString toString() const; + virtual int getType() const { return MARKASREAD; } + virtual void apply( ScorableArticle &article ) const; + virtual ActionMarkAsRead* clone() const; +}; + +class KDE_EXPORT NotifyCollection +{ +public: + NotifyCollection(); + ~NotifyCollection(); + void addNote(const ScorableArticle&, const TQString&); + TQString collection() const; + void displayCollection(TQWidget *p=0) const; +private: + struct article_info { + TQString from; + TQString subject; + }; + typedef TQValueList<article_info> article_list; + typedef TQDict<article_list> note_list; + note_list notifyList; +}; + + +//---------------------------------------------------------------------------- +class KDE_EXPORT KScoringExpression +{ + friend class KScoringRule; + public: + enum Condition { CONTAINS, MATCH, ETQUALS, SMALLER, GREATER, MATCHCS }; + + KScoringExpression(const TQString&,const TQString&,const TQString&, const TQString&); + ~KScoringExpression(); + + bool match(ScorableArticle& a) const ; + TQString getTypeString() const; + static TQString getTypeString(int); + int getType() const; + TQString toString() const; + void write(TQTextStream& ) const; + + bool isNeg() const { return neg; } + Condition getCondition() const { return cond; } + TQString getExpression() const { return expr_str; } + TQString getHeader() const { return header; } + static TQStringList conditionNames(); + static TQStringList headerNames(); + static int getConditionForName(const TQString&); + static TQString getNameForCondition(int); + private: + bool neg; + TQString header; + const char* c_header; + Condition cond; + TQRegExp expr; + TQString expr_str; + int expr_int; +}; + +//---------------------------------------------------------------------------- +class KDE_EXPORT KScoringRule +{ + friend class KScoringManager; + public: + KScoringRule(const TQString& name); + KScoringRule(const KScoringRule& r); + ~KScoringRule(); + + typedef TQPtrList<KScoringExpression> ScoreExprList; + typedef TQPtrList<ActionBase> ActionList; + typedef TQStringList GroupList; + enum LinkMode { AND, OR }; + + TQString getName() const { return name; } + TQStringList getGroups() const { return groups; } + void setGroups(const TQStringList &l) { groups = l; } + LinkMode getLinkMode() const { return link; } + TQString getLinkModeName() const; + TQString getExpireDateString() const; + TQDate getExpireDate() const { return expires; } + void setExpireDate(const TQDate &d) { expires = d; } + bool isExpired() const; + ScoreExprList getExpressions() const { return expressions; } + ActionList getActions() const { return actions; } + void cleanExpressions(); + void cleanActions(); + + bool matchGroup(const TQString& group) const ; + void applyRule(ScorableArticle& a) const; + void applyRule(ScorableArticle& a, const TQString& group) const; + void applyAction(ScorableArticle& a) const; + + void setLinkMode(const TQString& link); + void setLinkMode(LinkMode m) { link = m; } + void setExpire(const TQString& exp); + void addExpression( KScoringExpression* ); + void addGroup( const TQString& group) { groups.append(group); } + //void addServer(const TQString& server) { servers.append(server); } + void addAction(int, const TQString& ); + void addAction(ActionBase*); + + void updateXML(TQDomElement& e, TQDomDocument& d); + TQString toString() const; + + // writes the rule in XML format into the textstream + void write(TQTextStream& ) const; +protected: + //! assert that the name is unique + void setName(const TQString &n) { name = n; } +private: + TQString name; + GroupList groups; + //ServerList servers; + LinkMode link; + ScoreExprList expressions; + ActionList actions; + TQDate expires; +}; + +/** this helper class implements a stack for lists of lists of rules. + With the help of this class its very easy for the KScoringManager + to temporary drop lists of rules and restore them afterwards +*/ +class KDE_EXPORT RuleStack +{ +public: + RuleStack(); + ~RuleStack(); + //! puts the list on the stack, doesn't change the list + void push(TQPtrList<KScoringRule>&); + //! clears the argument list and copy the content of the TOS into it + //! after that the TOS gets dropped + void pop(TQPtrList<KScoringRule>&); + //! like pop but without dropping the TOS + void top(TQPtrList<KScoringRule>&); + //! drops the TOS + void drop(); +private: + TQPtrStack< TQPtrList<KScoringRule> > stack; +}; + +//---------------------------------------------------------------------------- +// Manages the score rules. +class KDE_EXPORT KScoringManager : public TQObject +{ + Q_OBJECT + TQ_OBJECT + + public: + //* this is the container for all rules + typedef TQPtrList<KScoringRule> ScoringRuleList; + + KScoringManager(const TQString& appName = TQString()); + virtual ~KScoringManager(); + + //* returns a list of all available groups, must be overridden + virtual TQStringList getGroups() const =0; + + //! returns a list of common (or available) headers + //! defaults to returning { Subject, From, Message-ID, Date } + virtual TQStringList getDefaultHeaders() const; + + //* setting current server and group and calling applyRules(ScorableArticle&) + void applyRules(ScorableArticle& article, const TQString& group/*, const TQString& server*/); + //* assuming a properly set group + void applyRules(ScorableArticle&); + //* same as above + void applyRules(ScorableGroup* group); + + //* pushes the current rule list onto a stack + void pushRuleList(); + //* restores the current rule list from list stored on a stack + //* by a previous call to pushRuleList (this implicitly deletes the + //* current rule list) + void popRuleList(); + //* removes the TOS from the stack of rule lists + void removeTOS(); + + KScoringRule* addRule(KScoringRule *); + KScoringRule* addRule(const ScorableArticle&, TQString group, short =0); + KScoringRule* addRule(); + void cancelNewRule(KScoringRule *); + void deleteRule(KScoringRule *); + void editRule(KScoringRule *e, TQWidget *w=0); + KScoringRule* copyRule(KScoringRule *); + void moveRuleAbove( KScoringRule *above, KScoringRule *below ); + void moveRuleBelow( KScoringRule *below, KScoringRule *above ); + void setGroup(const TQString& g); + // has to be called after setGroup() or initCache() + bool hasRulesForCurrentGroup(); + TQString findUniqueName() const; + + /** called from an editor whenever it finishes editing the rule base, + causes the finishedEditing signal to be emitted */ + void editorReady(); + + ScoringRuleList getAllRules() const { return allRules; } + KScoringRule *findRule(const TQString&); + TQStringList getRuleNames(); + void setRuleName(KScoringRule *, const TQString&); + int getRuleCount() const { return allRules.count(); } + TQString toString() const; + + bool setCacheValid(bool v); + bool isCacheValid() { return cacheValid; } + void initCache(const TQString& group/*, const TQString& server*/); + + void load(); + void save(); + + //--------------- Properties + virtual bool canScores() const { return true; } + virtual bool canNotes() const { return true; } + virtual bool canColors() const { return false; } + virtual bool canMarkAsRead() const { return false; } + virtual bool hasFeature(int); + + signals: + void changedRules(); + void changedRuleName(const TQString& oldName, const TQString& newName); + void finishedEditing(); + + private: + void addRuleInternal(KScoringRule *e); + void expireRules(); + + TQDomDocument createXMLfromInternal(); + void createInternalFromXML(TQDomNode); + + // list of all Rules + ScoringRuleList allRules; + + // the stack for temporary storing rule lists + RuleStack stack; + + // for the cache + bool cacheValid; + // current rule set, ie the cache + ScoringRuleList ruleList; + //TQString server; + TQString group; + + //ScorableServer* _s; + + // filename of the scorefile + TQString mFilename; +}; + + +//---------------------------------------------------------------------------- +class KDE_EXPORT NotifyDialog : public KDialogBase +{ + Q_OBJECT + TQ_OBJECT +public: + static void display(ScorableArticle&,const TQString&); +protected slots: + void slotShowAgainToggled(bool); +private: + NotifyDialog(TQWidget* p =0); + static NotifyDialog *me; + + TQLabel *note; + TQString msg; + typedef TQMap<TQString,bool> NotesMap; + static NotesMap dict; +}; + + +#endif diff --git a/libtdepim/kscoringeditor.cpp b/libtdepim/kscoringeditor.cpp new file mode 100644 index 000000000..6e520e983 --- /dev/null +++ b/libtdepim/kscoringeditor.cpp @@ -0,0 +1,1029 @@ +/* + kscoringeditor.cpp + + Copyright (c) 2001 Mathias Waack + Copyright (C) 2005 by Volker Krause <volker.krause@rwth-aachen.de> + + Author: Mathias Waack <mathias@atoll-net.de> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US +*/ + +#undef TQT_NO_COMPAT + +#include "kscoring.h" +#include "kscoringeditor.h" + +#include <kdebug.h> +#include <klocale.h> +#include <kcombobox.h> +#include <kcolorcombo.h> +#include <kiconloader.h> +#include <kregexpeditorinterface.h> +#include <ktrader.h> +#include <kparts/componentfactory.h> + + +#include <tqlabel.h> +#include <tqpushbutton.h> +#include <tqlayout.h> +#include <tqtooltip.h> +#include <tqcheckbox.h> +#include <tqbuttongroup.h> +#include <tqradiobutton.h> +#include <tqwidgetstack.h> +#include <tqapplication.h> +#include <tqtimer.h> +#include <tqhbox.h> + +// works for both ListBox and ComboBox +template <class T> static int setCurrentItem(T *box, const TQString& s) +{ + int cnt = box->count(); + for (int i=0;i<cnt;++i) { + if (box->text(i) == s) { + box->setCurrentItem(i); + return i; + } + } + return -1; +} + + +//============================================================================ +// +// class SingleConditionWidget (editor for one condition, used in ConditionEditWidget) +// +//============================================================================ +SingleConditionWidget::SingleConditionWidget(KScoringManager *m,TQWidget *p, const char *n) + : TQFrame(p,n), manager(m) +{ + TQBoxLayout *topL = new TQVBoxLayout(this,5); + TQBoxLayout *firstRow = new TQHBoxLayout(topL); + neg = new TQCheckBox(i18n("Not"),this); + TQToolTip::add(neg,i18n("Negate this condition")); + firstRow->addWidget(neg); + headers = new KComboBox(this); + headers->insertStringList(manager->getDefaultHeaders()); + headers->setEditable( true ); + TQToolTip::add(headers,i18n("Select the header to match this condition against")); + firstRow->addWidget(headers,1); + matches = new KComboBox(this); + matches->insertStringList(KScoringExpression::conditionNames()); + TQToolTip::add(matches,i18n("Select the type of match")); + firstRow->addWidget(matches,1); + connect( matches, TQT_SIGNAL( activated( int ) ), TQT_SLOT( toggleRegExpButton( int ) ) ); + TQHBoxLayout *secondRow = new TQHBoxLayout( topL ); + secondRow->setSpacing( 1 ); + expr = new KLineEdit( this ); + TQToolTip::add(expr,i18n("The condition for the match")); + // reserve space for at least 20 characters + expr->setMinimumWidth(fontMetrics().maxWidth()*20); + secondRow->addWidget( expr ); + regExpButton = new TQPushButton( i18n("Edit..."), this ); + secondRow->addWidget( regExpButton ); + connect( regExpButton, TQT_SIGNAL( clicked() ), TQT_SLOT( showRegExpDialog() ) ); + + // occupy at much width as possible + tqsetSizePolicy(TQSizePolicy(TQSizePolicy::Expanding,TQSizePolicy::Fixed)); + setFrameStyle(Box | Sunken); + setLineWidth(1); +} + +SingleConditionWidget::~SingleConditionWidget() +{} + +void SingleConditionWidget::setCondition(KScoringExpression *e) +{ + neg->setChecked(e->isNeg()); + headers->setCurrentText( e->getHeader() ); + setCurrentItem(matches,KScoringExpression::getNameForCondition(e->getCondition())); + toggleRegExpButton( matches->currentItem() ); + expr->setText(e->getExpression()); +} + +KScoringExpression* SingleConditionWidget::createCondition() const +{ + TQString head = headers->currentText(); + TQString match = matches->currentText(); + int condType = KScoringExpression::getConditionForName(match); + match = KScoringExpression::getTypeString(condType); + TQString cond = expr->text(); + TQString negs = (neg->isChecked())?"1":"0"; + return new KScoringExpression(head,match,cond,negs); +} + +void SingleConditionWidget::clear() +{ + neg->setChecked(false); + expr->clear(); +} + +void SingleConditionWidget::toggleRegExpButton( int selected ) +{ + bool isRegExp = (KScoringExpression::MATCH == selected || + KScoringExpression::MATCHCS == selected) && + !KTrader::self()->query("KRegExpEditor/KRegExpEditor").isEmpty(); + regExpButton->setEnabled( isRegExp ); +} + +void SingleConditionWidget::showRegExpDialog() +{ + TQDialog *editorDialog = KParts::ComponentFactory::createInstanceFromQuery<TQDialog>( "KRegExpEditor/KRegExpEditor" ); + if ( editorDialog ) { + KRegExpEditorInterface *editor = static_cast<KRegExpEditorInterface *>( editorDialog->qt_cast( "KRegExpEditorInterface" ) ); + Q_ASSERT( editor ); // This should not fail! + editor->setRegExp( expr->text() ); + editorDialog->exec(); + expr->setText( editor->regExp() ); + } +} + +//============================================================================ +// +// class ConditionEditWidget (the widget to edit the conditions of a rule) +// +//============================================================================ +ConditionEditWidget::ConditionEditWidget(KScoringManager *m, TQWidget *p, const char *n) + : KWidgetLister(1,8,p,n), manager(m) +{ + // create one initial widget + addWidgetAtEnd(); +} + +ConditionEditWidget::~ConditionEditWidget() +{} + +TQWidget* ConditionEditWidget::createWidget(TQWidget *parent) +{ + return new SingleConditionWidget(manager,parent); +} + +void ConditionEditWidget::clearWidget(TQWidget *w) +{ + Q_ASSERT( w->isA("SingleConditionWidget") ); + SingleConditionWidget *sw = dynamic_cast<SingleConditionWidget*>(w); + if (sw) + sw->clear(); +} + +void ConditionEditWidget::slotEditRule(KScoringRule *rule) +{ + KScoringRule::ScoreExprList l; + if (rule) l = rule->getExpressions(); + if (!rule || l.count() == 0) { + slotClear(); + } else { + setNumberOfShownWidgetsTo(l.count()); + KScoringExpression *e = l.first(); + SingleConditionWidget *scw = static_cast<SingleConditionWidget*>(mWidgetList.first()); + while (e && scw) { + scw->setCondition(e); + e = l.next(); + scw = static_cast<SingleConditionWidget*>(mWidgetList.next()); + } + } +} + +void ConditionEditWidget::updateRule(KScoringRule *rule) +{ + rule->cleanExpressions(); + for(TQWidget *w = mWidgetList.first(); w; w = mWidgetList.next()) { + if (! w->isA("SingleConditionWidget")) { + kdWarning(5100) << "there is a widget in ConditionEditWidget " + << "which isn't a SingleConditionWidget" << endl; + } else { + SingleConditionWidget *saw = dynamic_cast<SingleConditionWidget*>(w); + if (saw) + rule->addExpression(saw->createCondition()); + } + } +} + +//============================================================================ +// +// class SingleActionWidget (editor for one action, used in ActionEditWidget) +// +//============================================================================ +SingleActionWidget::SingleActionWidget(KScoringManager *m,TQWidget *p, const char *n) + : TQWidget(p,n), notifyEditor(0), scoreEditor(0), colorEditor(0),manager(m) +{ + TQHBoxLayout *topL = new TQHBoxLayout(this,0,5); + types = new KComboBox(this); + types->setEditable(false); + topL->addWidget(types); + stack = new TQWidgetStack(this); + topL->addWidget(stack); + + dummyLabel = new TQLabel(i18n("Select an action."), stack); + stack->addWidget(dummyLabel, 0); + + // init widget stack and the types combo box + int index = 1; + types->insertItem(TQString()); + TQStringList l = ActionBase::userNames(); + for ( TQStringList::Iterator it = l.begin(); it != l.end(); ++it ) { + TQString name = *it; + int feature = ActionBase::getTypeForUserName(name); + if (manager->hasFeature(feature)) { + types->insertItem(name); + TQWidget *w=0; + switch (feature) { + case ActionBase::SETSCORE: + w = scoreEditor = new KIntSpinBox(-99999,99999,1,0,10, stack); + break; + case ActionBase::NOTIFY: + w = notifyEditor = new KLineEdit(stack); + break; + case ActionBase::COLOR: + w = colorEditor = new KColorCombo(stack); + break; + case ActionBase::MARKASREAD: + w = new TQLabel( stack ); // empty dummy + break; + } + if ( w ) + stack->addWidget(w,index++); + } + } + + connect(types,TQT_SIGNAL(activated(int)),stack,TQT_SLOT(raiseWidget(int))); + + // raise the dummy label + types->setCurrentItem(0); + stack->raiseWidget(dummyLabel); +} + +SingleActionWidget::~SingleActionWidget() +{ +} + +void SingleActionWidget::setAction(ActionBase *act) +{ + kdDebug(5100) << "SingleActionWidget::setAction()" << endl; + setCurrentItem(types,ActionBase::userName(act->getType())); + int index = types->currentItem(); + stack->raiseWidget(index); + switch (act->getType()) { + case ActionBase::SETSCORE: + scoreEditor->setValue(act->getValueString().toInt()); + break; + case ActionBase::NOTIFY: + notifyEditor->setText(act->getValueString()); + break; + case ActionBase::COLOR: + colorEditor->setColor(TQColor(act->getValueString())); + break; + case ActionBase::MARKASREAD: + // nothing + break; + default: + kdWarning(5100) << "unknown action type in SingleActionWidget::setAction()" << endl; + } +} + +ActionBase* SingleActionWidget::createAction() const +{ + // no action selected... + if (types->currentText().isEmpty()) + return 0; + + int type = ActionBase::getTypeForUserName(types->currentText()); + switch (type) { + case ActionBase::SETSCORE: + return new ActionSetScore(scoreEditor->value()); + case ActionBase::NOTIFY: + return new ActionNotify(notifyEditor->text()); + case ActionBase::COLOR: + return new ActionColor(TQString(colorEditor->color().name())); + case ActionBase::MARKASREAD: + return new ActionMarkAsRead(); + default: + kdWarning(5100) << "unknown action type in SingleActionWidget::getValue()" << endl; + return 0; + } +} + +void SingleActionWidget::clear() +{ + if (scoreEditor) scoreEditor->setValue(0); + if (notifyEditor) notifyEditor->clear(); + if (colorEditor) colorEditor->setCurrentItem(0); + types->setCurrentItem(0); + stack->raiseWidget(dummyLabel); +} + +//============================================================================ +// +// class ActionEditWidget (the widget to edit the actions of a rule) +// +//============================================================================ +ActionEditWidget::ActionEditWidget(KScoringManager *m,TQWidget *p, const char *n) + : KWidgetLister(1,8,p,n), manager(m) +{ + // create one initial widget + addWidgetAtEnd(); +} + +ActionEditWidget::~ActionEditWidget() +{} + +TQWidget* ActionEditWidget::createWidget( TQWidget *parent ) +{ + return new SingleActionWidget(manager,parent); +} + +void ActionEditWidget::slotEditRule(KScoringRule *rule) +{ + KScoringRule::ActionList l; + if (rule) l = rule->getActions(); + if (!rule || l.count() == 0) { + slotClear(); + } else { + setNumberOfShownWidgetsTo(l.count()); + ActionBase *act = l.first(); + SingleActionWidget *saw = static_cast<SingleActionWidget*>(mWidgetList.first()); + while (act && saw) { + saw->setAction(act); + act = l.next(); + saw = static_cast<SingleActionWidget*>(mWidgetList.next()); + } + } +} + +void ActionEditWidget::updateRule(KScoringRule *rule) +{ + rule->cleanActions(); + for(TQWidget *w = mWidgetList.first(); w; w = mWidgetList.next()) { + if (! w->isA("SingleActionWidget")) { + kdWarning(5100) << "there is a widget in ActionEditWidget " + << "which isn't a SingleActionWidget" << endl; + } else { + SingleActionWidget *saw = dynamic_cast<SingleActionWidget*>(w); + if (saw) + { + ActionBase *act = saw->createAction(); + if (act) + rule->addAction(act); + } + } + } +} + +void ActionEditWidget::clearWidget(TQWidget *w) +{ + Q_ASSERT( w->isA("SingleActionWidget") ); + SingleActionWidget *sw = dynamic_cast<SingleActionWidget*>(w); + if (sw) + sw->clear(); +} + +//============================================================================ +// +// class RuleEditWidget (the widget to edit one rule) +// +//============================================================================ +RuleEditWidget::RuleEditWidget(KScoringManager *m,TQWidget *p, const char *n) + : TQWidget(p,n), dirty(false), manager(m), oldRuleName(TQString()) +{ + kdDebug(5100) << "RuleEditWidget::RuleEditWidget()" << endl; + if ( !n ) setName( "RuleEditWidget" ); + TQVBoxLayout *topLayout = new TQVBoxLayout( this, 5, KDialog::spacingHint() ); + + //------------- Name, Servers, Groups --------------------- + TQGroupBox *groupB = new TQGroupBox(i18n("Properties"),this); + topLayout->addWidget(groupB); + TQGridLayout* groupL = new TQGridLayout(groupB, 6,2, 8,5); + groupL->addRowSpacing(0, fontMetrics().lineSpacing()-4); + + // name + ruleNameEdit = new KLineEdit( groupB, "ruleNameEdit" ); + groupL->addWidget( ruleNameEdit, 1, 1 ); + TQLabel *ruleNameLabel = new TQLabel(ruleNameEdit, i18n("&Name:"), groupB, "ruleNameLabel"); + groupL->addWidget( ruleNameLabel, 1, 0 ); + + // groups + groupsEdit = new KLineEdit( groupB, "groupsEdit" ); + groupL->addWidget( groupsEdit, 2, 1 ); + TQLabel *groupsLabel = new TQLabel(groupsEdit, i18n("&Groups:"), groupB, "groupsLabel"); + groupL->addWidget( groupsLabel, 2, 0 ); + + TQPushButton *groupsBtn = new TQPushButton(i18n("A&dd Group"), groupB); + connect(groupsBtn,TQT_SIGNAL(clicked()),TQT_SLOT(slotAddGroup())); + groupL->addWidget( groupsBtn, 3, 0 ); + + groupsBox = new KComboBox( false, groupB, "groupsBox" ); + groupsBox->setDuplicatesEnabled(false); + groupsBox->insertStringList(manager->getGroups()); + groupsBox->tqsetSizePolicy(TQSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Fixed)); + groupL->addWidget( groupsBox, 3, 1 ); + + // expires + expireCheck = new TQCheckBox(i18n("&Expire rule automatically"), groupB); + groupL->addMultiCellWidget( expireCheck, 4,4, 0,1 ); + expireEdit = new KIntSpinBox(1,99999,1,30,10, groupB, "expireWidget"); + //Init suffix + slotExpireEditChanged(30); + connect(expireEdit, TQT_SIGNAL(valueChanged(int)), TQT_SLOT(slotExpireEditChanged(int))); + groupL->addWidget( expireEdit, 5, 1 ); + expireLabel = new TQLabel(expireEdit, i18n("&Rule is valid for:"), groupB, "expireLabel"); + groupL->addWidget( expireLabel, 5, 0 ); + expireLabel->setEnabled(false); + expireEdit->setEnabled(false); + + connect(expireCheck, TQT_SIGNAL(toggled(bool)), expireLabel, TQT_SLOT(setEnabled(bool))); + connect(expireCheck, TQT_SIGNAL(toggled(bool)), expireEdit, TQT_SLOT(setEnabled(bool))); + + //------------- Conditions --------------------- + TQGroupBox *groupConds = new TQGroupBox(i18n("Conditions"), this); + topLayout->addWidget(groupConds); + TQGridLayout *condL = new TQGridLayout(groupConds, 3,2, 8,5); + + condL->addRowSpacing(0, fontMetrics().lineSpacing()-4); + + TQButtonGroup *buttonGroup = new TQButtonGroup(groupConds); + buttonGroup->hide(); + linkModeAnd = new TQRadioButton(i18n("Match a&ll conditions"), groupConds); + buttonGroup->insert(linkModeAnd); + condL->addWidget(linkModeAnd, 1,0); + linkModeOr = new TQRadioButton(i18n("Matc&h any condition"), groupConds); + buttonGroup->insert(linkModeOr); + condL->addWidget(linkModeOr, 1,1); + linkModeAnd->setChecked(true); + + condEditor = new ConditionEditWidget(manager,groupConds); + condL->addMultiCellWidget(condEditor, 2,2, 0,1); + connect(condEditor,TQT_SIGNAL(widgetRemoved()),this,TQT_SLOT(slotShrink())); + + //------------- Actions --------------------- + TQGroupBox *groupActions = new TQGroupBox(i18n("Actions"), this); + topLayout->addWidget(groupActions); + TQBoxLayout *actionL = new TQVBoxLayout(groupActions,8,5); + actionL->addSpacing(fontMetrics().lineSpacing()-4); + actionEditor = new ActionEditWidget(manager,groupActions); + actionL->addWidget(actionEditor); + connect(actionEditor,TQT_SIGNAL(widgetRemoved()),this,TQT_SLOT(slotShrink())); + + topLayout->addStretch(1); + + kdDebug(5100) << "constructed RuleEditWidget" << endl; +} + +RuleEditWidget::~RuleEditWidget() +{ +} + +void RuleEditWidget::slotEditRule(const TQString& ruleName) +{ + kdDebug(5100) << "RuleEditWidget::slotEditRule(" << ruleName << ")" << endl; +// // first update the old rule if there is one +// kdDebug(5100) << "let see if we have a rule with name " << oldRuleName << endl; +// KScoringRule *rule; +// if (!oldRuleName.isNull() && oldRuleName != ruleName) { +// rule = manager->findRule(oldRuleName); +// if (rule) { +// kdDebug(5100) << "updating rule " << rule->getName() << endl; +// updateRule(rule); +// } +// } + + KScoringRule* rule = manager->findRule(ruleName); + if (!rule) { + kdDebug(5100) << "no rule for ruleName " << ruleName << endl; + clearContents(); + return; + } + oldRuleName = rule->getName(); + ruleNameEdit->setText(rule->getName()); + groupsEdit->setText(rule->getGroups().join(";")); + + bool b = rule->getExpireDate().isValid(); + expireCheck->setChecked(b); + expireEdit->setEnabled(b); + expireLabel->setEnabled(b); + if (b) + expireEdit->setValue(TQDate::tqcurrentDate().daysTo(rule->getExpireDate())); + else + expireEdit->setValue(30); + if (rule->getLinkMode() == KScoringRule::AND) { + linkModeAnd->setChecked(true); + } + else { + linkModeOr->setChecked(true); + } + + condEditor->slotEditRule(rule); + actionEditor->slotEditRule(rule); + + kdDebug(5100) << "RuleEditWidget::slotEditRule() ready" << endl; +} + +void RuleEditWidget::clearContents() +{ + ruleNameEdit->setText(""); + groupsEdit->setText(""); + expireCheck->setChecked(false); + expireEdit->setValue(30); + expireEdit->setEnabled(false); + condEditor->slotEditRule(0); + actionEditor->slotEditRule(0); + oldRuleName = TQString(); +} + +void RuleEditWidget::updateRule(KScoringRule *rule) +{ + oldRuleName = TQString(); + TQString groups = groupsEdit->text(); + if (groups.isEmpty()) + rule->setGroups(TQStringList(".*")); + else + rule->setGroups(TQStringList::split(";",groups)); + bool b = expireCheck->isChecked(); + if (b) + rule->setExpireDate(TQDate::tqcurrentDate().addDays(expireEdit->value())); + else + rule->setExpireDate(TQDate()); + actionEditor->updateRule(rule); + rule->setLinkMode(linkModeAnd->isChecked()?KScoringRule::AND:KScoringRule::OR); + condEditor->updateRule(rule); + if (rule->getName() != ruleNameEdit->text()) + manager->setRuleName(rule,ruleNameEdit->text()); +} + +void RuleEditWidget::updateRule() +{ + KScoringRule *rule = manager->findRule(oldRuleName); + if (rule) updateRule(rule); +} + +void RuleEditWidget::slotAddGroup() +{ + TQString grp = groupsBox->currentText(); + if ( grp.isEmpty() ) + return; + TQString txt = groupsEdit->text().stripWhiteSpace(); + if ( txt == ".*" || txt.isEmpty() ) groupsEdit->setText(grp); + else groupsEdit->setText(txt + ";" + grp); +} + +void RuleEditWidget::setDirty() +{ + kdDebug(5100) << "RuleEditWidget::setDirty()" << endl; + if (dirty) return; + dirty = true; +} + +void RuleEditWidget::slotShrink() +{ + emit(shrink()); +} + +void RuleEditWidget::slotExpireEditChanged(int value) +{ + expireEdit->setSuffix(i18n(" day", " days", value)); +} + +//============================================================================ +// +// class RuleListWidget (the widget for managing a list of rules) +// +//============================================================================ +RuleListWidget::RuleListWidget(KScoringManager *m, bool standalone, TQWidget *p, const char *n) + : TQWidget(p,n), alone(standalone), manager(m) +{ + kdDebug(5100) << "RuleListWidget::RuleListWidget()" << endl; + if (!n) setName("RuleListWidget"); + TQVBoxLayout *topL = new TQVBoxLayout(this,standalone? 0:5,KDialog::spacingHint()); + ruleList = new KListBox(this); + if (standalone) { + connect(ruleList,TQT_SIGNAL(doubleClicked(TQListBoxItem*)), + this,TQT_SLOT(slotEditRule(TQListBoxItem*))); + connect(ruleList,TQT_SIGNAL(returnPressed(TQListBoxItem*)), + this,TQT_SLOT(slotEditRule(TQListBoxItem*))); + } + connect(ruleList, TQT_SIGNAL(currentChanged(TQListBoxItem*)), + this, TQT_SLOT(slotRuleSelected(TQListBoxItem*))); + topL->addWidget(ruleList); + + TQHBoxLayout *btnL = new TQHBoxLayout( topL, KDialog::spacingHint() ); + mRuleUp = new TQPushButton( this ); + mRuleUp->setPixmap( BarIcon( "up", KIcon::SizeSmall ) ); + TQToolTip::add( mRuleUp, i18n("Move rule up") ); + btnL->addWidget( mRuleUp ); + connect( mRuleUp, TQT_SIGNAL( clicked() ), TQT_SLOT( slotRuleUp() ) ); + mRuleDown = new TQPushButton( this ); + mRuleDown->setPixmap( BarIcon( "down", KIcon::SizeSmall ) ); + TQToolTip::add( mRuleDown, i18n("Move rule down") ); + btnL->addWidget( mRuleDown ); + connect( mRuleDown, TQT_SIGNAL( clicked() ), TQT_SLOT( slotRuleDown() ) ); + + btnL = new TQHBoxLayout( topL, KDialog::spacingHint() ); + editRule=0L; + newRule = new TQPushButton(this); + newRule->setPixmap( BarIcon( "filenew", KIcon::SizeSmall ) ); + TQToolTip::add(newRule,i18n("New rule")), + btnL->addWidget(newRule); + connect(newRule, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotNewRule())); + // if we're standalone, we need an additional edit button + if (standalone) { + editRule = new TQPushButton(this); + editRule->setIconSet( BarIconSet("edit", KIcon::SizeSmall) ); + TQToolTip::add(editRule,i18n("Edit rule")); + btnL->addWidget(editRule); + connect(editRule,TQT_SIGNAL(clicked()),this,TQT_SLOT(slotEditRule())); + } + delRule = new TQPushButton(this); + delRule->setIconSet( BarIconSet( "editdelete", KIcon::SizeSmall ) ); + TQToolTip::add(delRule,i18n("Remove rule")); + btnL->addWidget(delRule); + connect(delRule, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotDelRule())); + copyRule = new TQPushButton(this); + copyRule->setIconSet(BarIconSet("editcopy", KIcon::SizeSmall)); + TQToolTip::add(copyRule,i18n("Copy rule")); + btnL->addWidget(copyRule); + connect(copyRule, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotCopyRule())); + + // the group filter + TQBoxLayout *filterL = new TQVBoxLayout(topL,KDialog::spacingHint()); + KComboBox *filterBox = new KComboBox(this); + TQStringList l = m->getGroups(); + filterBox->insertItem(i18n("<all groups>")); + filterBox->insertStringList(l); + filterBox->tqsetSizePolicy(TQSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Fixed)); + connect(filterBox,TQT_SIGNAL(activated(const TQString&)), + this,TQT_SLOT(slotGroupFilter(const TQString&))); + slotGroupFilter(i18n("<all groups>")); + TQLabel *lab = new TQLabel(filterBox,i18n("Sho&w only rules for group:"),this); + filterL->addWidget(lab); + filterL->addWidget(filterBox); + + connect(manager,TQT_SIGNAL(changedRules()), + this,TQT_SLOT(updateRuleList())); + connect(manager,TQT_SIGNAL(changedRuleName(const TQString&,const TQString&)), + this,TQT_SLOT(slotRuleNameChanged(const TQString&,const TQString&))); + + updateRuleList(); + updateButton(); +} + +RuleListWidget::~RuleListWidget() +{ +} + +void RuleListWidget::updateButton() +{ + bool state = ruleList->count() > 0; + if(editRule) + editRule->setEnabled(state); + delRule->setEnabled(state); + copyRule->setEnabled(state); + + TQListBoxItem *item = ruleList->item( ruleList->currentItem() ); + if ( item ) { + mRuleUp->setEnabled( item->prev() != 0 ); + mRuleDown->setEnabled( item->next() != 0 ); + } +} + +void RuleListWidget::updateRuleList() +{ + emit leavingRule(); + kdDebug(5100) << "RuleListWidget::updateRuleList()" << endl; + TQString curr = ruleList->currentText(); + ruleList->clear(); + if (group == i18n("<all groups>")) { + TQStringList l = manager->getRuleNames(); + ruleList->insertStringList(l); + } else { + KScoringManager::ScoringRuleList l = manager->getAllRules(); + for (KScoringRule* rule = l.first(); rule; rule = l.next() ) { + if (rule->matchGroup(group)) ruleList->insertItem(rule->getName()); + } + } + int index = setCurrentItem(ruleList,curr); + if (index <0) { + ruleList->setCurrentItem(0); + slotRuleSelected(ruleList->currentText()); + } + else { + slotRuleSelected(curr); + } +} + +void RuleListWidget::updateRuleList(const KScoringRule *rule) +{ + kdDebug(5100) << "RuleListWidget::updateRuleList(" << rule->getName() << ")" << endl; + TQString name = rule->getName(); + updateRuleList(); + slotRuleSelected(name); +} + +void RuleListWidget::slotRuleNameChanged(const TQString& oldName, const TQString& newName) +{ + int ind = ruleList->currentItem(); + for (uint i=0;i<ruleList->count();++i) + if (ruleList->text(i) == oldName) { + ruleList->changeItem(newName,i); + ruleList->setCurrentItem(ind); + return; + } +} + +void RuleListWidget::slotEditRule(const TQString& s) +{ + emit ruleEdited(s); +} + +void RuleListWidget::slotEditRule() +{ + if (ruleList->currentItem() >= 0) { + emit ruleEdited(ruleList->currentText()); + } + else if (ruleList->count() == 0) + emit ruleEdited(TQString()); +} + +void RuleListWidget::slotEditRule(TQListBoxItem* item) +{ + slotEditRule(item->text()); +} + +void RuleListWidget::slotGroupFilter(const TQString& s) +{ + group = s; + updateRuleList(); +} + +void RuleListWidget::slotRuleSelected(const TQString& ruleName) +{ + emit leavingRule(); + kdDebug(5100) << "RuleListWidget::slotRuleSelected(" << ruleName << ")" << endl; + if (ruleName != ruleList->currentText()) { + setCurrentItem(ruleList,ruleName); + } + updateButton(); + emit ruleSelected(ruleName); +} + +void RuleListWidget::slotRuleSelected(TQListBoxItem *item) +{ + if (!item) return; + TQString ruleName = item->text(); + slotRuleSelected(ruleName); +} + +void RuleListWidget::slotRuleSelected(int index) +{ + uint idx = index; + if (idx >= ruleList->count()) return; + TQString ruleName = ruleList->text(index); + slotRuleSelected(ruleName); +} + +void RuleListWidget::slotNewRule() +{ + emit leavingRule(); + KScoringRule *rule = manager->addRule(); + updateRuleList(rule); + if (alone) slotEditRule(rule->getName()); + updateButton(); +} + +void RuleListWidget::slotDelRule() +{ + KScoringRule *rule = manager->findRule(ruleList->currentText()); + if (rule) + manager->deleteRule(rule); + // goto the next rule + if (!alone) slotEditRule(); + updateButton(); +} + +void RuleListWidget::slotCopyRule() +{ + emit leavingRule(); + TQString ruleName = ruleList->currentText(); + KScoringRule *rule = manager->findRule(ruleName); + if (rule) { + KScoringRule *nrule = manager->copyRule(rule); + updateRuleList(nrule); + slotEditRule(nrule->getName()); + } + updateButton(); +} + +void RuleListWidget::slotRuleUp() +{ + KScoringRule *rule = 0, *below = 0; + TQListBoxItem *item = ruleList->item( ruleList->currentItem() ); + if ( item ) { + rule = manager->findRule( item->text() ); + item = item->prev(); + if ( item ) + below = manager->findRule( item->text() ); + } + if ( rule && below ) + manager->moveRuleAbove( rule, below ); + updateRuleList(); + updateButton(); +} + +void RuleListWidget::slotRuleDown() +{ + KScoringRule *rule = 0, *above = 0; + TQListBoxItem *item = ruleList->item( ruleList->currentItem() ); + if ( item ) { + rule = manager->findRule( item->text() ); + item = item->next(); + if ( item ) + above = manager->findRule( item->text() ); + } + if ( rule && above ) + manager->moveRuleBelow( rule, above ); + updateRuleList(); + updateButton(); +} + +//============================================================================ +// +// class KScoringEditor (the score edit dialog) +// +//============================================================================ +KScoringEditor* KScoringEditor::scoreEditor = 0; + +KScoringEditor::KScoringEditor(KScoringManager* m, + TQWidget *parent, const char *name) + : KDialogBase(parent,name,false,i18n("Rule Editor"),Ok|Apply|Cancel,Ok,true), manager(m) +{ + manager->pushRuleList(); + if (!scoreEditor) scoreEditor = this; + kdDebug(5100) << "KScoringEditor::KScoringEditor()" << endl; + if (!name) setName("KScoringEditor"); + // the left side gives an overview about all rules, the right side + // shows a detailed view of an selected rule + TQWidget *w = new TQWidget(this); + setMainWidget(w); + TQHBoxLayout *hbl = new TQHBoxLayout(w,0,spacingHint()); + ruleLister = new RuleListWidget(manager,false,w); + hbl->addWidget(ruleLister); + ruleEditor = new RuleEditWidget(manager,w); + hbl->addWidget(ruleEditor); + connect(ruleLister,TQT_SIGNAL(ruleSelected(const TQString&)), + ruleEditor, TQT_SLOT(slotEditRule(const TQString&))); + connect(ruleLister, TQT_SIGNAL(leavingRule()), + ruleEditor, TQT_SLOT(updateRule())); + connect(ruleEditor, TQT_SIGNAL(shrink()), TQT_SLOT(slotShrink())); + connect(this,TQT_SIGNAL(finished()),TQT_SLOT(slotFinished())); + ruleLister->slotRuleSelected(0); + resize(550, tqsizeHint().height()); +} + +void KScoringEditor::setDirty() +{ + TQPushButton *applyBtn = actionButton(Apply); + applyBtn->setEnabled(true); +} + +KScoringEditor::~KScoringEditor() +{ + scoreEditor = 0; +} + +KScoringEditor* KScoringEditor::createEditor(KScoringManager* m, + TQWidget *parent, const char *name) +{ + if (scoreEditor) return scoreEditor; + else return new KScoringEditor(m,parent,name); +} + +void KScoringEditor::setRule(KScoringRule* r) +{ + kdDebug(5100) << "KScoringEditor::setRule(" << r->getName() << ")" << endl; + TQString ruleName = r->getName(); + ruleLister->slotRuleSelected(ruleName); +} + +void KScoringEditor::slotShrink() +{ + TQTimer::singleShot(5, this, TQT_SLOT(slotDoShrink())); +} + +void KScoringEditor::slotDoShrink() +{ + updateGeometry(); + TQApplication::sendPostedEvents(); + resize(width(),tqsizeHint().height()); +} + +void KScoringEditor::slotApply() +{ + TQString ruleName = ruleLister->currentRule(); + KScoringRule *rule = manager->findRule(ruleName); + if (rule) { + ruleEditor->updateRule(rule); + ruleLister->updateRuleList(rule); + } + manager->removeTOS(); + manager->pushRuleList(); +} + +void KScoringEditor::slotOk() +{ + slotApply(); + manager->removeTOS(); + KDialogBase::slotOk(); + manager->editorReady(); +} + +void KScoringEditor::slotCancel() +{ + manager->popRuleList(); + KDialogBase::slotCancel(); +} + +void KScoringEditor::slotFinished() +{ + delayedDestruct(); +} + +//============================================================================ +// +// class KScoringEditorWidgetDialog (a dialog for the KScoringEditorWidget) +// +//============================================================================ +KScoringEditorWidgetDialog::KScoringEditorWidgetDialog(KScoringManager *m, const TQString& r, TQWidget *p, const char *n) + : KDialogBase(p,n,true,i18n("Edit Rule"), + KDialogBase::Ok|KDialogBase::Apply|KDialogBase::Close, + KDialogBase::Ok,true), + manager(m), ruleName(r) +{ + TQFrame *f = makeMainWidget(); + TQBoxLayout *topL = new TQVBoxLayout(f); + ruleEditor = new RuleEditWidget(manager,f); + connect(ruleEditor, TQT_SIGNAL(shrink()), TQT_SLOT(slotShrink())); + topL->addWidget(ruleEditor); + ruleEditor->slotEditRule(ruleName); + resize(0,0); +} + +void KScoringEditorWidgetDialog::slotApply() +{ + KScoringRule *rule = manager->findRule(ruleName); + if (rule) { + ruleEditor->updateRule(rule); + ruleName = rule->getName(); + } +} + +void KScoringEditorWidgetDialog::slotOk() +{ + slotApply(); + KDialogBase::slotOk(); +} + +void KScoringEditorWidgetDialog::slotShrink() +{ + TQTimer::singleShot(5, this, TQT_SLOT(slotDoShrink())); +} + +void KScoringEditorWidgetDialog::slotDoShrink() +{ + updateGeometry(); + TQApplication::sendPostedEvents(); + resize(width(),tqsizeHint().height()); +} + +//============================================================================ +// +// class KScoringEditorWidget (a reusable widget for config dialog...) +// +//============================================================================ +KScoringEditorWidget::KScoringEditorWidget(KScoringManager *m,TQWidget *p, const char *n) + : TQWidget(p,n), manager(m) +{ + TQBoxLayout *topL = new TQVBoxLayout(this); + ruleLister = new RuleListWidget(manager,true,this); + topL->addWidget(ruleLister); + connect(ruleLister,TQT_SIGNAL(ruleEdited(const TQString&)), + this,TQT_SLOT(slotRuleEdited(const TQString &))); +} + +KScoringEditorWidget::~KScoringEditorWidget() +{ + manager->editorReady(); +} + +void KScoringEditorWidget::slotRuleEdited(const TQString& ruleName) +{ + KScoringEditorWidgetDialog dlg(manager,ruleName,this); + dlg.exec(); + ruleLister->updateRuleList(); +} + +#include "kscoringeditor.moc" diff --git a/libtdepim/kscoringeditor.h b/libtdepim/kscoringeditor.h new file mode 100644 index 000000000..ba0ee190e --- /dev/null +++ b/libtdepim/kscoringeditor.h @@ -0,0 +1,317 @@ +/* + kscoringeditor.h + + Copyright (c) 2001 Mathias Waack + Copyright (C) 2005 by Volker Krause <volker.krause@rwth-aachen.de> + + Author: Mathias Waack <mathias@atoll-net.de> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US +*/ + +#ifndef SCOREEDITWIDGET_H +#define SCOREEDITWIDGET_H + +#include <tqmap.h> +#include <kdialogbase.h> +#include <tqtable.h> +#include <tqframe.h> + +#include "kwidgetlister.h" + +#include <tdepimmacros.h> + +class KComboBox; +class KLineEdit; +class KIntSpinBox; +class KListBox; +class TQFrame; +class TQLabel; +class TQListBoxItem; +class TQPushButton; +class TQCheckBox; +class TQRadioButton; + +class KScoringRule; +class KScoringExpression; +class KScoringManager; +class ActionBase; +class KScoringEditor; +class ScoreEditWidget; +class KColorCombo; + +/** this widget implements an editor for one condition. + It is used in ExpressionEditWidget +*/ +class KDE_EXPORT SingleConditionWidget : public TQFrame +{ + Q_OBJECT + TQ_OBJECT + friend class ConditionEditWidget; +public: + SingleConditionWidget(KScoringManager *,TQWidget *p =0, const char *n =0); + ~SingleConditionWidget(); + void setCondition(KScoringExpression*); + KScoringExpression *createCondition() const; + void clear(); + +protected slots: + void toggleRegExpButton( int ); + void showRegExpDialog(); + +private: + /** marks a condition as negated */ + TQCheckBox *neg; + /** list of possible headers */ + KComboBox *headers; + /** list of match types */ + KComboBox *matches; + /** the expression which will be matched */ + KLineEdit *expr; + /** the button to open the regexp-editor */ + TQPushButton *regExpButton; + + KScoringManager *manager; +}; + +/** this widget implements the conditions editor + */ +class ConditionEditWidget: public KWidgetLister +{ + Q_OBJECT + TQ_OBJECT +public: + ConditionEditWidget(KScoringManager *,TQWidget *p =0, const char *n =0); + ~ConditionEditWidget(); + TQWidget* createWidget(TQWidget*); + void updateRule(KScoringRule*); + void clearWidget(TQWidget*); +public slots: + void slotEditRule(KScoringRule*); +private: + KScoringManager *manager; +}; + +/** this widget implements an editor for one action. + It is used in ActionEditWidget +*/ +class SingleActionWidget : public TQWidget +{ + Q_OBJECT + TQ_OBJECT + friend class ActionEditWidget; +public: + SingleActionWidget(KScoringManager *m,TQWidget *p =0, const char *n =0); + ~SingleActionWidget(); + void setAction(ActionBase*); + ActionBase *createAction() const; + void clear(); +private: + /** the list of available action */ + KComboBox *types; + /** the stack of the edit widgets for each action type */ + TQWidgetStack *stack; + /** the notify action editor */ + KLineEdit *notifyEditor; + /** the score acton editor */ + KIntSpinBox *scoreEditor; + /** the color action editor */ + KColorCombo *colorEditor; + /** the dummy label */ + TQLabel *dummyLabel; + + KScoringManager *manager; +}; + +/** this widget implements the action editor + */ +class KDE_EXPORT ActionEditWidget : public KWidgetLister +{ + Q_OBJECT + TQ_OBJECT +public: + ActionEditWidget(KScoringManager *m,TQWidget *p =0, const char *n =0); + ~ActionEditWidget(); + TQWidget* createWidget(TQWidget *parent); + void updateRule(KScoringRule*); + void clearWidget(TQWidget *); +public slots: + void slotEditRule(KScoringRule *); +private: + KScoringManager *manager; +}; + +/** This widget implements the rule editor + */ +class RuleEditWidget : public TQWidget +{ + Q_OBJECT + TQ_OBJECT +public: + RuleEditWidget(KScoringManager *m,TQWidget *p =0, const char *n =0); + ~RuleEditWidget(); +public slots: + void setDirty(); + void slotEditRule(const TQString&); + void updateRule(KScoringRule*); + void updateRule(); +signals: + void shrink(); +protected slots: + void slotAddGroup(); + void slotShrink(); +private slots: + void slotExpireEditChanged(int value); +private: + void clearContents(); + + bool dirty; + /** the name of the rule */ + KLineEdit *ruleNameEdit; + /** the list of groups this rule applies to */ + KLineEdit *groupsEdit; + /** list of all available groups */ + KComboBox *groupsBox; + /** the expire enable */ + TQCheckBox *expireCheck; + /** the label to the expireCheck */ + TQLabel *expireLabel; + /** the expire delay */ + KIntSpinBox *expireEdit; + /** the link modes of the conditions */ + TQRadioButton *linkModeOr, *linkModeAnd; + /** the actions editor */ + ActionEditWidget *actionEditor; + /** the conditions editor */ + ConditionEditWidget *condEditor; + + KScoringManager *manager; + + // the old name of the current rule + TQString oldRuleName; +}; + +/** This widget shows a list of rules with buttons for + copy, delete aso. +*/ +class KDE_EXPORT RuleListWidget : public TQWidget +{ + Q_OBJECT + TQ_OBJECT +public: + RuleListWidget(KScoringManager *m, bool =false, TQWidget *p =0, const char *n =0); + ~RuleListWidget(); + TQString currentRule() const { return ruleList->currentText(); } +protected: + void updateButton(); + +signals: + void ruleSelected(const TQString&); + void ruleEdited(const TQString&); + void leavingRule(); +public slots: + void slotRuleSelected(const TQString&); + void slotRuleSelected(TQListBoxItem *); + void slotRuleSelected(int); + void updateRuleList(); + void updateRuleList(const KScoringRule*); + void slotRuleNameChanged(const TQString&,const TQString&); +protected slots: + void slotGroupFilter(const TQString&); + void slotEditRule(TQListBoxItem*); + void slotEditRule(const TQString&); + void slotEditRule(); + void slotDelRule(); + void slotNewRule(); + void slotCopyRule(); + void slotRuleUp(); + void slotRuleDown(); + +private: + /** the list of rules */ + KListBox *ruleList; + /** the current group */ + TQString group; + /** marks if we're alone or together with the edit widget */ + bool alone; + + KScoringManager *manager; + + TQPushButton *editRule; + TQPushButton *newRule; + TQPushButton *delRule; + TQPushButton *copyRule; + TQPushButton *mRuleUp; + TQPushButton *mRuleDown; +}; + +class KDE_EXPORT KScoringEditor : public KDialogBase +{ + Q_OBJECT + TQ_OBJECT +public: + ~KScoringEditor(); + void setRule(KScoringRule*); + static KScoringEditor *createEditor(KScoringManager* m, TQWidget *parent=0, const char *name=0); + static KScoringEditor *editor() { return scoreEditor; } + void setDirty(); +protected: + KScoringEditor(KScoringManager* m, TQWidget *parent=0, const char *name=0); +private: + /** the editor for the current rule */ + RuleEditWidget* ruleEditor; + /** the list of known rules */ + RuleListWidget *ruleLister; +protected slots: + void slotShrink(); + void slotDoShrink(); + void slotApply(); + void slotOk(); + void slotCancel(); + void slotFinished(); +private: + KScoringManager *manager; + ScoreEditWidget *edit; + /** make sure that there is only one instance of ourselve */ + static KScoringEditor *scoreEditor; +}; + +class KScoringEditorWidgetDialog : public KDialogBase +{ + Q_OBJECT + TQ_OBJECT +public: + KScoringEditorWidgetDialog(KScoringManager *m, const TQString& rName, TQWidget *parent=0, const char *name=0); +protected slots: + void slotApply(); + void slotOk(); + void slotShrink(); + void slotDoShrink(); +private: + RuleEditWidget *ruleEditor; + KScoringManager *manager; + TQString ruleName; +}; + +class KDE_EXPORT KScoringEditorWidget : public TQWidget +{ + Q_OBJECT + TQ_OBJECT +public: + KScoringEditorWidget(KScoringManager *m,TQWidget *p =0, const char *n =0); + ~KScoringEditorWidget(); +protected slots: + void slotRuleEdited(const TQString&); +private: + RuleListWidget *ruleLister; + KScoringManager *manager; +}; + + +#endif // SCOREEDITWIDGET_H diff --git a/libtdepim/ksubscription.cpp b/libtdepim/ksubscription.cpp new file mode 100644 index 000000000..b611e1260 --- /dev/null +++ b/libtdepim/ksubscription.cpp @@ -0,0 +1,793 @@ +/* + ksubscription.cpp + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US +*/ + +#include "ksubscription.h" +#include "kaccount.h" + +#include <tqlayout.h> +#include <tqtimer.h> +#include <tqlabel.h> +#include <tqpushbutton.h> +#include <tqheader.h> +#include <tqtoolbutton.h> + +#include <kseparator.h> +#include <kapplication.h> +#include <kiconloader.h> +#include <klocale.h> +#include <kdebug.h> +#include <klineedit.h> + + +//============================================================================= + +KGroupInfo::KGroupInfo(const TQString &name, const TQString &description, + bool newGroup, bool subscribed, + tqStatus status, TQString path) + : name(name), description(description), + newGroup(newGroup), subscribed(subscribed), + status(status), path(path) +{ +} + +//----------------------------------------------------------------------------- +bool KGroupInfo::operator== (const KGroupInfo &gi2) +{ + return (name == gi2.name); +} + +//----------------------------------------------------------------------------- +bool KGroupInfo::operator< (const KGroupInfo &gi2) +{ + return (name < gi2.name); +} + +//============================================================================= + +GroupItem::GroupItem( TQListView *v, const KGroupInfo &gi, KSubscription* browser, + bool isCheckItem ) + : TQCheckListItem( v, gi.name, isCheckItem ? CheckBox : CheckBoxController ), + mInfo( gi ), mBrowser( browser ), mIsCheckItem( isCheckItem ), + mIgnoreStateChange( false ) +{ + if (listView()->columns() > 1) + setDescription(); +} + +//----------------------------------------------------------------------------- +GroupItem::GroupItem( TQListViewItem *i, const KGroupInfo &gi, KSubscription* browser, + bool isCheckItem ) + : TQCheckListItem( i, gi.name, isCheckItem ? CheckBox : CheckBoxController ), + mInfo( gi ), mBrowser( browser ), mIsCheckItem( isCheckItem ), + mIgnoreStateChange( false ) +{ + if (listView()->columns() > 1) + setDescription(); +} + +//----------------------------------------------------------------------------- +void GroupItem::setInfo( KGroupInfo info ) +{ + mInfo = info; + setText(0, mInfo.name); + if (listView()->columns() > 1) + setDescription(); +} + +//----------------------------------------------------------------------------- +void GroupItem::setDescription() +{ + setText(1, mInfo.description); +} + +//----------------------------------------------------------------------------- +void GroupItem::setOn( bool on ) +{ + if (mBrowser->isLoading()) + { + // set this only if we're loading/creating items + // otherwise changes are only permanent when the dialog is saved + mInfo.subscribed = on; + } + if (isCheckItem()) + TQCheckListItem::setOn(on); +} + +//------------------------------------------------------------------------------ +void GroupItem::stateChange( bool on ) +{ + // delegate to parent + if ( !mIgnoreStateChange ) + mBrowser->changeItemState(this, on); +} + +//------------------------------------------------------------------------------ +void GroupItem::setVisible( bool b ) +{ + if (b) + { + TQListViewItem::setVisible(b); + setEnabled(true); + } + else + { + if (isCheckItem()) + { + bool setInvisible = true; + for (TQListViewItem * lvchild = firstChild(); lvchild != 0; + lvchild = lvchild->nextSibling()) + { + if (lvchild->isVisible()) // item has a visible child + setInvisible = false; + } + if (setInvisible) + TQListViewItem::setVisible(b); + else + { + // leave it visible so that tqchildren remain visible + setOpen(true); + setEnabled(false); + } + } + else + { + // non-checkable item + TQPtrList<TQListViewItem> moveItems; + + for (TQListViewItem * lvchild = firstChild(); lvchild != 0; + lvchild = lvchild->nextSibling()) + { + if (static_cast<GroupItem*>(lvchild)->isCheckItem()) + { + // remember the items + moveItems.append(lvchild); + } + } + TQPtrListIterator<TQListViewItem> it( moveItems ); + for ( ; it.current(); ++it) + { + // move the checkitem to top + TQListViewItem* parent = it.current()->parent(); + if (parent) parent->takeItem(it.current()); + listView()->insertItem(it.current()); + } + TQListViewItem::setVisible(false); + } + } +} + +//----------------------------------------------------------------------------- +void GroupItem::paintCell( TQPainter * p, const TQColorGroup & cg, + int column, int width, int align ) +{ + if (mIsCheckItem) + return TQCheckListItem::paintCell( p, cg, column, width, align ); + else + return TQListViewItem::paintCell( p, cg, column, width, align ); +} + +//----------------------------------------------------------------------------- +void GroupItem::paintFocus( TQPainter * p, const TQColorGroup & cg, + const TQRect & r ) +{ + if (mIsCheckItem) + TQCheckListItem::paintFocus(p, cg, r); + else + TQListViewItem::paintFocus(p, cg, r); +} + +//----------------------------------------------------------------------------- +int GroupItem::width( const TQFontMetrics& fm, const TQListView* lv, int column) const +{ + if (mIsCheckItem) + return TQCheckListItem::width(fm, lv, column); + else + return TQListViewItem::width(fm, lv, column); +} + +//----------------------------------------------------------------------------- +void GroupItem::setup() +{ + if (mIsCheckItem) + TQCheckListItem::setup(); + else + TQListViewItem::setup(); +} + + +//============================================================================= + +KSubscription::KSubscription( TQWidget *parent, const TQString &caption, + KAccount * acct, int buttons, const TQString &user1, bool descriptionColumn ) + : KDialogBase( parent, 0, true, caption, buttons | Help | Ok | Cancel, Ok, + true, i18n("Reload &List"), user1 ), + mAcct( acct ) +{ + mLoading = true; + setWFlags( getWFlags() | WDestructiveClose ); + + // create Widgets + page = new TQWidget(this); + setMainWidget(page); + + TQLabel *comment = new TQLabel("<p>"+ + i18n("Manage which mail folders you want to see in your folder view") + "</p>", page); + + TQToolButton *clearButton = new TQToolButton( page ); + clearButton->setIconSet( KGlobal::iconLoader()->loadIconSet( + KApplication::reverseLayout() ? "clear_left":"locationbar_erase", KIcon::Small, 0 ) ); + filterEdit = new KLineEdit(page); + TQLabel *l = new TQLabel(filterEdit,i18n("S&earch:"), page); + connect( clearButton, TQT_SIGNAL( clicked() ), filterEdit, TQT_SLOT( clear() ) ); + + // checkboxes + noTreeCB = new TQCheckBox(i18n("Disable &tree view"), page); + noTreeCB->setChecked(false); + subCB = new TQCheckBox(i18n("&Subscribed only"), page); + subCB->setChecked(false); + newCB = new TQCheckBox(i18n("&New only"), page); + newCB->setChecked(false); + + + KSeparator *sep = new KSeparator(KSeparator::HLine, page); + + // init the labels + TQFont fnt = font(); + fnt.setBold(true); + leftLabel = new TQLabel(i18n("Loading..."), page); + rightLabel = new TQLabel(i18n("Current changes:"), page); + leftLabel->setFont(fnt); + rightLabel->setFont(fnt); + + // icons + pmRight = BarIconSet("forward"); + pmLeft = BarIconSet("back"); + + arrowBtn1 = new TQPushButton(page); + arrowBtn1->setEnabled(false); + arrowBtn2 = new TQPushButton(page); + arrowBtn2->setEnabled(false); + arrowBtn1->setIconSet(pmRight); + arrowBtn2->setIconSet(pmRight); + arrowBtn1->setFixedSize(35,30); + arrowBtn2->setFixedSize(35,30); + + // the main listview + groupView = new TQListView(page); + groupView->setRootIsDecorated(true); + groupView->addColumn(i18n("Name")); + groupView->setAllColumnsShowFocus(true); + if (descriptionColumn) + mDescrColumn = groupView->addColumn(i18n("Description")); + else + groupView->header()->setStretchEnabled(true, 0); + + // tqlayout + TQGridLayout *topL = new TQGridLayout(page,4,1,0, KDialog::spacingHint()); + TQHBoxLayout *filterL = new TQHBoxLayout(KDialog::spacingHint()); + TQVBoxLayout *arrL = new TQVBoxLayout(KDialog::spacingHint()); + listL = new TQGridLayout(2, 3, KDialog::spacingHint()); + + topL->addWidget(comment, 0,0); + topL->addLayout(filterL, 1,0); + topL->addWidget(sep,2,0); + topL->addLayout(listL, 3,0); + + filterL->addWidget(clearButton); + filterL->addWidget(l); + filterL->addWidget(filterEdit, 1); + filterL->addWidget(noTreeCB); + filterL->addWidget(subCB); + filterL->addWidget(newCB); + + listL->addWidget(leftLabel, 0,0); + listL->addWidget(rightLabel, 0,2); + listL->addWidget(groupView, 1,0); + listL->addLayout(arrL, 1,1); + listL->setRowStretch(1,1); + listL->setColStretch(0,5); + listL->setColStretch(2,2); + + arrL->addWidget(arrowBtn1, AlignCenter); + arrL->addWidget(arrowBtn2, AlignCenter); + + // listviews + subView = new TQListView(page); + subView->addColumn(i18n("Subscribe To")); + subView->header()->setStretchEnabled(true, 0); + unsubView = new TQListView(page); + unsubView->addColumn(i18n("Unsubscribe From")); + unsubView->header()->setStretchEnabled(true, 0); + + TQVBoxLayout *protL = new TQVBoxLayout(3); + listL->addLayout(protL, 1,2); + protL->addWidget(subView); + protL->addWidget(unsubView); + + // disable some widgets as long we're loading + enableButton(User1, false); + enableButton(User2, false); + newCB->setEnabled(false); + noTreeCB->setEnabled(false); + subCB->setEnabled(false); + + filterEdit->setFocus(); + + // items clicked + connect(groupView, TQT_SIGNAL(clicked(TQListViewItem *)), + this, TQT_SLOT(slotChangeButtonState(TQListViewItem*))); + connect(subView, TQT_SIGNAL(clicked(TQListViewItem *)), + this, TQT_SLOT(slotChangeButtonState(TQListViewItem*))); + connect(unsubView, TQT_SIGNAL(clicked(TQListViewItem *)), + this, TQT_SLOT(slotChangeButtonState(TQListViewItem*))); + + // connect buttons + connect(arrowBtn1, TQT_SIGNAL(clicked()), TQT_SLOT(slotButton1())); + connect(arrowBtn2, TQT_SIGNAL(clicked()), TQT_SLOT(slotButton2())); + connect(this, TQT_SIGNAL(user1Clicked()), TQT_SLOT(slotLoadFolders())); + + // connect checkboxes + connect(subCB, TQT_SIGNAL(clicked()), TQT_SLOT(slotCBToggled())); + connect(newCB, TQT_SIGNAL(clicked()), TQT_SLOT(slotCBToggled())); + connect(noTreeCB, TQT_SIGNAL(clicked()), TQT_SLOT(slotCBToggled())); + + // connect textfield + connect(filterEdit, TQT_SIGNAL(textChanged(const TQString&)), + TQT_SLOT(slotFilterTextChanged(const TQString&))); + + // update status + connect(this, TQT_SIGNAL(listChanged()), TQT_SLOT(slotUpdateStatusLabel())); +} + +//----------------------------------------------------------------------------- +KSubscription::~KSubscription() +{ +} + +//----------------------------------------------------------------------------- +void KSubscription::setStartItem( const KGroupInfo &info ) +{ + TQListViewItemIterator it(groupView); + + for ( ; it.current(); ++it) + { + if (static_cast<GroupItem*>(it.current())->info() == info) + { + it.current()->setSelected(true); + it.current()->setOpen(true); + } + } +} + +//----------------------------------------------------------------------------- +void KSubscription::removeListItem( TQListView *view, const KGroupInfo &gi ) +{ + if(!view) return; + TQListViewItemIterator it(view); + + for ( ; it.current(); ++it) + { + if (static_cast<GroupItem*>(it.current())->info() == gi) + { + delete it.current(); + break; + } + } + if (view == groupView) + emit listChanged(); +} + +//----------------------------------------------------------------------------- +TQListViewItem* KSubscription::getListItem( TQListView *view, const KGroupInfo &gi ) +{ + if(!view) return 0; + TQListViewItemIterator it(view); + + for ( ; it.current(); ++it) + { + if (static_cast<GroupItem*>(it.current())->info() == gi) + return (it.current()); + } + return 0; +} + +//----------------------------------------------------------------------------- +bool KSubscription::itemInListView( TQListView *view, const KGroupInfo &gi ) +{ + if(!view) return false; + TQListViewItemIterator it(view); + + for ( ; it.current(); ++it) + if (static_cast<GroupItem*>(it.current())->info() == gi) + return true; + + return false; +} + +//------------------------------------------------------------------------------ +void KSubscription::setDirectionButton1( Direction dir ) +{ + mDirButton1 = dir; + if (dir == Left) + arrowBtn1->setIconSet(pmLeft); + else + arrowBtn1->setIconSet(pmRight); +} + +//------------------------------------------------------------------------------ +void KSubscription::setDirectionButton2( Direction dir ) +{ + mDirButton2 = dir; + if (dir == Left) + arrowBtn2->setIconSet(pmLeft); + else + arrowBtn2->setIconSet(pmRight); +} + +//------------------------------------------------------------------------------ +void KSubscription::changeItemState( GroupItem* item, bool on ) +{ + // is this a checkable item + if (!item->isCheckItem()) return; + + // if we're currently loading the items ignore changes + if (mLoading) return; + if (on) + { + if (!itemInListView(unsubView, item->info())) + { + TQListViewItem *p = item->parent(); + while (p) + { + // make sure all parents are subscribed + GroupItem* pi = static_cast<GroupItem*>(p); + if (pi->isCheckItem() && !pi->isOn()) + { + pi->setIgnoreStateChange(true); + pi->setOn(true); + pi->setIgnoreStateChange(false); + new GroupItem(subView, pi->info(), this); + } + p = p->parent(); + } + new GroupItem(subView, item->info(), this); + } + // eventually remove it from the other listview + removeListItem(unsubView, item->info()); + } + else { + if (!itemInListView(subView, item->info())) + { + new GroupItem(unsubView, item->info(), this); + } + // eventually remove it from the other listview + removeListItem(subView, item->info()); + } + // update the buttons + slotChangeButtonState(item); +} + +//------------------------------------------------------------------------------ +void KSubscription::filterChanged( TQListViewItem* item, const TQString & text ) +{ + if ( !item && groupView ) + item = groupView->firstChild(); + if ( !item ) + return; + + do + { + if ( item->firstChild() ) // recursive descend + filterChanged(item->firstChild(), text); + + GroupItem* gr = static_cast<GroupItem*>(item); + if (subCB->isChecked() || newCB->isChecked() || !text.isEmpty() || + noTreeCB->isChecked()) + { + // set it invisible + if ( subCB->isChecked() && + (!gr->isCheckItem() || + (gr->isCheckItem() && !gr->info().subscribed)) ) + { + // only subscribed + gr->setVisible(false); + continue; + } + if ( newCB->isChecked() && + (!gr->isCheckItem() || + (gr->isCheckItem() && !gr->info().newGroup)) ) + { + // only new + gr->setVisible(false); + continue; + } + if ( !text.isEmpty() && + gr->text(0).find(text, 0, false) == -1) + { + // searchfield + gr->setVisible(false); + continue; + } + if ( noTreeCB->isChecked() && + !gr->isCheckItem() ) + { + // disable treeview + gr->setVisible(false); + continue; + } + + gr->setVisible(true); + + } else { + gr->setVisible(true); + } + + } while ((item = item->nextSibling())); + +} + +//------------------------------------------------------------------------------ +uint KSubscription::activeItemCount() +{ + TQListViewItemIterator it(groupView); + + uint count = 0; + for ( ; it.current(); ++it) + { + if (static_cast<GroupItem*>(it.current())->isCheckItem() && + it.current()->isVisible() && it.current()->isEnabled()) + count++; + } + + return count; +} + +//------------------------------------------------------------------------------ +void KSubscription::restoreOriginalParent() +{ + TQPtrList<TQListViewItem> move; + TQListViewItemIterator it(groupView); + for ( ; it.current(); ++it) + { + TQListViewItem* origParent = static_cast<GroupItem*>(it.current())-> + originalParent(); + if (origParent && origParent != it.current()->parent()) + { + // remember this to avoid messing up the iterator + move.append(it.current()); + } + } + TQPtrListIterator<TQListViewItem> it2( move ); + for ( ; it2.current(); ++it2) + { + // restore the original parent + TQListViewItem* origParent = static_cast<GroupItem*>(it2.current())-> + originalParent(); + groupView->takeItem(it2.current()); + origParent->insertItem(it2.current()); + } +} + +//----------------------------------------------------------------------------- +void KSubscription::saveOpenStates() +{ + TQListViewItemIterator it(groupView); + + for ( ; it.current(); ++it) + { + static_cast<GroupItem*>(it.current())->setLastOpenState( + it.current()->isOpen() ); + } +} + +//----------------------------------------------------------------------------- +void KSubscription::restoreOpenStates() +{ + TQListViewItemIterator it(groupView); + + for ( ; it.current(); ++it) + { + it.current()->setOpen( + static_cast<GroupItem*>(it.current())->lastOpenState() ); + } +} + +//----------------------------------------------------------------------------- +void KSubscription::slotLoadingComplete() +{ + mLoading = false; + + enableButton(User1, true); + enableButton(User2, true); + newCB->setEnabled(true); + noTreeCB->setEnabled(true); + subCB->setEnabled(true); + + // remember the correct parent + TQListViewItemIterator it(groupView); + for ( ; it.current(); ++it) + { + static_cast<GroupItem*>(it.current())-> + setOriginalParent( it.current()->parent() ); + } + + emit listChanged(); +} + +//------------------------------------------------------------------------------ +void KSubscription::slotChangeButtonState( TQListViewItem *item ) +{ + if (!item || + (item->listView() == groupView && + !static_cast<GroupItem*>(item)->isCheckItem())) + { + // disable and return + arrowBtn1->setEnabled(false); + arrowBtn2->setEnabled(false); + return; + } + // set the direction of the buttons and enable/disable them + TQListView* currentView = item->listView(); + if (currentView == groupView) + { + setDirectionButton1(Right); + setDirectionButton2(Right); + if (static_cast<GroupItem*>(item)->isOn()) + { + // already subscribed + arrowBtn1->setEnabled(false); + arrowBtn2->setEnabled(true); + } else { + // unsubscribed + arrowBtn1->setEnabled(true); + arrowBtn2->setEnabled(false); + } + } else if (currentView == subView) + { + // undo possible + setDirectionButton1(Left); + + arrowBtn1->setEnabled(true); + arrowBtn2->setEnabled(false); + } else if (currentView == unsubView) + { + // undo possible + setDirectionButton2(Left); + + arrowBtn1->setEnabled(false); + arrowBtn2->setEnabled(true); + } +} + +//------------------------------------------------------------------------------ +void KSubscription::slotButton1() +{ + if (mDirButton1 == Right) + { + if (groupView->currentItem() && + static_cast<GroupItem*>(groupView->currentItem())->isCheckItem()) + { + // activate + GroupItem* item = static_cast<GroupItem*>(groupView->currentItem()); + item->setOn(true); + } + } + else { + if (subView->currentItem()) + { + GroupItem* item = static_cast<GroupItem*>(subView->currentItem()); + // get the corresponding item from the groupView + TQListViewItem* listitem = getListItem(groupView, item->info()); + if (listitem) + { + // deactivate + GroupItem* chk = static_cast<GroupItem*>(listitem); + chk->setOn(false); + } + } + } +} + +//------------------------------------------------------------------------------ +void KSubscription::slotButton2() +{ + if (mDirButton2 == Right) + { + if (groupView->currentItem() && + static_cast<GroupItem*>(groupView->currentItem())->isCheckItem()) + { + // deactivate + GroupItem* item = static_cast<GroupItem*>(groupView->currentItem()); + item->setOn(false); + } + } + else { + if (unsubView->currentItem()) + { + GroupItem* item = static_cast<GroupItem*>(unsubView->currentItem()); + // get the corresponding item from the groupView + TQListViewItem* listitem = getListItem(groupView, item->info()); + if (listitem) + { + // activate + GroupItem* chk = static_cast<GroupItem*>(listitem); + chk->setOn(true); + } + } + } +} + +//------------------------------------------------------------------------------ +void KSubscription::slotCBToggled() +{ + if (!noTreeCB->isChecked() && !newCB->isChecked() && !subCB->isChecked()) + { + restoreOriginalParent(); + } + // set items {in}visible + filterChanged(groupView->firstChild()); + emit listChanged(); +} + +//------------------------------------------------------------------------------ +void KSubscription::slotFilterTextChanged( const TQString & text ) +{ + // remember is the items are open + if (mLastText.isEmpty()) + saveOpenStates(); + + if (!mLastText.isEmpty() && text.length() < mLastText.length()) + { + // reset + restoreOriginalParent(); + TQListViewItemIterator it(groupView); + for ( ; it.current(); ++it) + { + it.current()->setVisible(true); + it.current()->setEnabled(true); + } + } + // set items {in}visible + filterChanged(groupView->firstChild(), text); + // restore the open-states + if (text.isEmpty()) + restoreOpenStates(); + + emit listChanged(); + mLastText = text; +} + +//------------------------------------------------------------------------------ +void KSubscription::slotUpdateStatusLabel() +{ + TQString text; + if (mLoading) + text = i18n("Loading... (1 matching)", "Loading... (%n matching)", + activeItemCount()); + else + text = i18n("%1: (1 matching)", "%1: (%n matching)", activeItemCount()) + .tqarg(account()->name()); + + leftLabel->setText(text); +} + +//------------------------------------------------------------------------------ +void KSubscription::slotLoadFolders() +{ + enableButton(User1, false); + mLoading = true; + subView->clear(); + unsubView->clear(); + groupView->clear(); +} + +#include "ksubscription.moc" diff --git a/libtdepim/ksubscription.h b/libtdepim/ksubscription.h new file mode 100644 index 000000000..c819701d1 --- /dev/null +++ b/libtdepim/ksubscription.h @@ -0,0 +1,382 @@ +/* + This file is part of libtdepim. + + Copyright (C) 2002 Carsten Burghardt <burghardt@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. +*/ + +/** @file +* This file defines a generic subscription widget and some support classes. +*/ + +#ifndef __KSUBSCRIPTION +#define __KSUBSCRIPTION + +#include <tqlistview.h> +#include <tqcheckbox.h> + +#include <kdialogbase.h> +#include <tdepimmacros.h> +#include "kfoldertree.h" + +class KSubscription; + +class KLineEdit; +class TQLayout; +class TQLabel; +class TQGridLayout; +class KAccount; + +//========================================================================== + +class KDE_EXPORT KGroupInfo +{ + public: + enum tqStatus { + unknown, + readOnly, + postingAllowed, + moderated + }; + + KGroupInfo( const TQString &name, const TQString &description = TQString(), + bool newGroup = false, bool subscribed = false, + tqStatus status = unknown, TQString path = TQString() ); + + TQString name, description; + bool newGroup, subscribed; + tqStatus status; + TQString path; + + bool operator== (const KGroupInfo &gi2); + bool operator< (const KGroupInfo &gi2); + +}; + +//========================================================================== + +/** A class representing a single group item (what's that?) */ +class KDE_EXPORT GroupItem : public TQCheckListItem +{ + public: + GroupItem( TQListView *v, const KGroupInfo &gi, KSubscription* browser, + bool isCheckItem = false ); + GroupItem( TQListViewItem *i, const KGroupInfo &gi, KSubscription* browser, + bool isCheckItem = false ); + + /** + * Get/Set the KGroupInfo + */ + KGroupInfo info() { return mInfo; } + void setInfo( KGroupInfo info ); + + /** + * Get/Set the original parent + */ + TQListViewItem* originalParent() { return mOriginalParent; } + void setOriginalParent( TQListViewItem* parent ) { mOriginalParent = parent; } + + /** + * Get/Set the last open state + */ + bool lastOpenState() { return mLastOpenState; } + void setLastOpenState( bool last ) { mLastOpenState = last; } + + /** + * Sets the description from the KGroupInfo + * Reimplement this for special cases + */ + virtual void setDescription(); + + /** + * Get if this is a checkable item + */ + bool isCheckItem() const { return mIsCheckItem; } + + /** + * Get/Set if state changes should be ignored + */ + bool ignoreStateChange() { return mIgnoreStateChange; } + void setIgnoreStateChange( bool ignore ) { mIgnoreStateChange = ignore; } + + /** + * Reimplemented + * Sets the subscribed property (only while items are loaded) + */ + virtual void setOn( bool on ); + + /** + * Reimlemented + * Calls KSubscription::changeItemState if mIgnoreStateChange == false + */ + virtual void stateChange( bool on ); + + /** + * Reimplemented + * Sets items invisible or disabled or even moves them + */ + void setVisible( bool b ); + + /** + * Reimplemented + * Calls TQListViewItem or TQCheckListItem + */ + virtual void paintCell( TQPainter * p, const TQColorGroup & cg, + int column, int width, int align ); + + /** + * Reimplemented + * Calls TQListViewItem or TQCheckListItem + */ + virtual void paintFocus( TQPainter *, const TQColorGroup & cg, + const TQRect & r ); + + /** + * Reimplemented + * Calls TQListViewItem or TQCheckListItem + */ + virtual int width( const TQFontMetrics&, const TQListView*, int column) const; + + /** + * Reimplemented + * Calls TQListViewItem or TQCheckListItem + */ + virtual void setup(); + + /** Reimplemented */ + virtual int rtti () const { return 15689; } + + protected: + KGroupInfo mInfo; + KSubscription* mBrowser; + TQListViewItem* mOriginalParent; + // remember last open state + bool mLastOpenState; + // is this a checkable item + bool mIsCheckItem; + // ignore state changes + bool mIgnoreStateChange; +}; + +//========================================================================== + +/** + * This class provides a generic subscription widget + * The dialog itself has a main listview that holds all items and two listviews that + * show all changes. The user can change the state of the items via checkable items. + * When you construct a new instance you need to provide an account and a caption + * After inserting your items (checkable or not) you need to call slotLoadingComplete() + * You should at least connect slots to the signals okClicked() (to save your changes) + * and user1Clicked() (to reload the list) + * You can hide unwanted checkboxes via the respective hide<checkboxname> methods + * + */ + +class KDE_EXPORT KSubscription : public KDialogBase +{ + Q_OBJECT + TQ_OBJECT + + public: + /** + * The direction of the buttons + */ + enum Direction { + Left, + Right + }; + + KSubscription( TQWidget *parent, const TQString &caption, KAccount* acct, + int buttons = 0, const TQString &user1 = TQString(), + bool descriptionColumn = true ); + + ~KSubscription(); + + /** + * Get/Set the account + */ + KAccount* account() { return mAcct; } + void setAccount( KAccount * acct ) { mAcct = acct; } + + /** + * Access to the treewidget that holds the GroupItems + */ + TQListView* folderTree() { return groupView; } + + /** + * Access to the searchfield + */ + KLineEdit* searchField() { return filterEdit; } + + /** + * The item that should be selected on startup + */ + void setStartItem( const KGroupInfo &info ); + + /** + * Removes the item from the listview + */ + void removeListItem( TQListView *view, const KGroupInfo &gi ); + + /** + * Gets the item from the listview + * Returns 0 if the item can't be found + */ + TQListViewItem* getListItem( TQListView *view, const KGroupInfo &gi ); + + /** + * Is the item in the given listview + */ + bool itemInListView( TQListView *view, const KGroupInfo &gi ); + + /** + * Makes all changes after an item is toggled + * called by the item's stateChange-method + */ + void changeItemState( GroupItem* item, bool on ); + + /** + * Get/Set the direction of button1 + */ + Direction directionButton1() { return mDirButton1; } + void setDirectionButton1( Direction dir ); + + /** + * Get/Set the direction of button2 + */ + Direction directionButton2() { return mDirButton2; } + void setDirectionButton2( Direction dir ); + + /** + * Returns true if items are being constructed + * Call 'slotLoadingComplete' to switch this + */ + bool isLoading() { return mLoading; } + + /** + * Hide 'Disable tree view' checkbox + */ + void hideTreeCheckbox() { noTreeCB->hide(); } + + /** + * Hide 'New Only' checkbox + */ + void hideNewOnlyCheckbox() { newCB->hide(); } + + /** + * Update the item-states (visible, enabled) when a filter + * criteria changed + */ + void filterChanged( TQListViewItem* item = 0, + const TQString & text = TQString() ); + + /** + * The amount of items that are visible and enabled + */ + uint activeItemCount(); + + /** + * Moves all items from toplevel back to their original position + */ + void restoreOriginalParent(); + + /** + * Saves the open states + */ + void saveOpenStates(); + + /** + * Restores the saved open state + */ + void restoreOpenStates(); + + + public slots: + /** + * Call this slot when you have created all items + */ + void slotLoadingComplete(); + + /** + * Changes the current state of the buttons + */ + void slotChangeButtonState( TQListViewItem* ); + + /** + * Buttons are clicked + */ + void slotButton1(); + void slotButton2(); + + /** + * Updates the status-label + */ + void slotUpdateStatusLabel(); + + /** + * The reload-button is pressed + */ + void slotLoadFolders(); + + protected slots: + /** + * Slot for the checkboxes + */ + void slotCBToggled(); + + /** + * Filter text changed + */ + void slotFilterTextChanged( const TQString & text ); + + signals: + /** + * Emitted when the amount of items in the + * groupView changes (e.g. on filtering) + */ + void listChanged(); + + + protected: + // current account + KAccount* mAcct; + + // widgets + TQWidget *page; + TQListView *groupView; + TQListView *subView, *unsubView; + KLineEdit *filterEdit; + TQCheckBox *noTreeCB, *subCB, *newCB; + TQPushButton *arrowBtn1, *arrowBtn2; + TQIconSet pmRight, pmLeft; + TQGridLayout *listL; + TQLabel *leftLabel, *rightLabel; + + // false if all items are loaded + bool mLoading; + + // directions + Direction mDirButton1; + Direction mDirButton2; + + // remember last searchtext + TQString mLastText; + + // remember description column + int mDescrColumn; +}; + +#endif diff --git a/libtdepim/ktimeedit.cpp b/libtdepim/ktimeedit.cpp new file mode 100644 index 000000000..c968f235c --- /dev/null +++ b/libtdepim/ktimeedit.cpp @@ -0,0 +1,292 @@ +/* + This file is part of libtdepim. + + Copyright (c) 1999 Preston Brown <pbrown@kde.org> + Copyright (c) 1999 Ian Dawes <iadawes@globalserve.net> + Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + As a special exception, permission is given to link this program + with any edition of TQt, and distribute the resulting executable, + without including the source code for TQt in the source distribution. +*/ + +#include <tqkeycode.h> +#include <tqcombobox.h> +#include <tqdatetime.h> +#include <tqlineedit.h> + +#include <kmessagebox.h> +#include <kglobal.h> +#include <kdebug.h> +#include <klocale.h> + +#include "ktimeedit.h" +#include <tqvalidator.h> +#include "ktimeedit.moc" + +// Validator for a time value with only hours and minutes (no seconds) +// Mostly locale aware. Author: David Faure <faure@kde.org> +class KOTimeValidator : public TQValidator +{ +public: + KOTimeValidator(TQWidget* parent, const char* name=0) : TQValidator(TQT_TQOBJECT(parent), name) {} + + virtual State validate(TQString& str, int& /*cursorPos*/) const + { + int length = str.length(); + // empty string is intermediate so one can clear the edit line and start from scratch + if ( length <= 0 ) + return Intermediate; + + bool ok = false; + /*TQTime time =*/ KGlobal::locale()->readTime(str, KLocale::WithoutSeconds, &ok); + if ( ok ) + return Acceptable; +// kdDebug(5300)<<"Time "<<str<<" not directly acceptable, trying military format "<<endl; + // Also try to accept times in "military format", i.e. no delimiter, like 1200 + int tm = str.toInt( &ok ); + if ( ok && ( 0 <= tm ) ) { + if ( ( tm < 2400 ) && ( tm%100 < 60 ) ) + return Acceptable; + else + return Intermediate; + } +// kdDebug(5300)<<str<<" not acceptable or intermediate for military format, either "<<str<<endl; + + // readTime doesn't help knowing when the string is "Intermediate". + // HACK. Not fully locale aware etc. (esp. the separator is '.' in sv_SE...) + TQChar sep = ':'; + // I want to allow "HH:", ":MM" and ":" to make editing easier + if ( str[0] == sep ) + { + if ( length == 1 ) // just ":" + return Intermediate; + TQString minutes = str.mid(1); + int m = minutes.toInt(&ok); + if ( ok && m >= 0 && m < 60 ) + return Intermediate; + } else if ( str[str.length()-1] == sep ) + { + TQString hours = str.left(length-1); + int h = hours.toInt(&ok); + if ( ok && h >= 0 && h < 24 ) + return Intermediate; + } +// return Invalid; + return Intermediate; + } + virtual void fixup ( TQString & input ) const { + bool ok = false; + KGlobal::locale()->readTime( input, KLocale::WithoutSeconds, &ok ); + if ( !ok ) { + // Also try to accept times in "military format", i.e. no delimiter, like 1200 + int tm = input.toInt( &ok ); + if ( ( 0 <= tm ) && ( tm < 2400 ) && ( tm%100 < 60 ) && ok ) { + input = KGlobal::locale()->formatTime( TQTime( tm / 100, tm % 100, 0 ) ); + } + } + } +}; + +// KTimeWidget/TQTimeEdit provide nicer editing, but don't provide a combobox. +// Difficult to get all in one... +// But TQt-3.2 will offer TQLineEdit::setMask, so a "99:99" mask would help. +KTimeEdit::KTimeEdit( TQWidget *parent, TQTime qt, const char *name ) + : TQComboBox( true, parent, name ) +{ + setInsertionPolicy( NoInsertion ); + setValidator( new KOTimeValidator( this ) ); + + mTime = qt; + +// mNoTimeString = i18n("No Time"); +// insertItem( mNoTimeString ); + + // Fill combo box with selection of times in localized format. + TQTime timeEntry(0,0,0); + do { + insertItem(KGlobal::locale()->formatTime(timeEntry)); + timeEntry = timeEntry.addSecs(60*15); + } while (!timeEntry.isNull()); + // Add end of day. + insertItem( KGlobal::locale()->formatTime( TQTime( 23, 59, 59 ) ) ); + + updateText(); + setFocusPolicy(TQ_StrongFocus); + + connect(this, TQT_SIGNAL(activated(int)), this, TQT_SLOT(active(int))); + connect(this, TQT_SIGNAL(highlighted(int)), this, TQT_SLOT(hilit(int))); + connect(this, TQT_SIGNAL(textChanged(const TQString&)),this,TQT_SLOT(changedText())); +} + +KTimeEdit::~KTimeEdit() +{ +} + +bool KTimeEdit::hasTime() const +{ + // Can't happen + if ( currentText().isEmpty() ) return false; + //if ( currentText() == mNoTimeString ) return false; + + return true; // always +} + +TQTime KTimeEdit::getTime() const +{ + //kdDebug(5300) << "KTimeEdit::getTime(), currentText() = " << currentText() << endl; + // TODO use KLocale::WithoutSeconds in HEAD + bool ok = false; + TQTime time = KGlobal::locale()->readTime( currentText(), KLocale::WithoutSeconds, &ok ); + if ( !ok ) { + // Also try to accept times in "military format", i.e. no delimiter, like 1200 + int tm = currentText().toInt( &ok ); + if ( ( 0 <= tm ) && ( tm < 2400 ) && ( tm%100 < 60 ) && ok ) { + time.setHMS( tm / 100, tm % 100, 0 ); + } else { + ok = false; + } + } + // kdDebug(5300) << "KTimeEdit::getTime(): " << time.toString() << endl; + return time; +} + +TQSizePolicy KTimeEdit::sizePolicy() const +{ + // Set size policy to Fixed, because edit cannot contain more text than the + // string representing the time. It doesn't make sense to provide more space. + TQSizePolicy sizePolicy(TQSizePolicy::Fixed,TQSizePolicy::Fixed); + + return sizePolicy; +} + +void KTimeEdit::setTime(TQTime newTime) +{ + if ( mTime != newTime ) + { + kdDebug(5300) << "KTimeEdit::setTime(): " << TQString(newTime.toString()) << endl; + + mTime = newTime; + updateText(); + } +} + +void KTimeEdit::active(int i) +{ + // The last entry, 23:59, is a special case + if( i == count() - 1 ) + mTime = TQTime( 23, 59, 0 ); + else + mTime = TQTime(0,0,0).addSecs(i*15*60); + emit timeChanged(mTime); +} + +void KTimeEdit::hilit(int ) +{ + // we don't currently need to do anything here. +} + +void KTimeEdit::addTime(TQTime qt) +{ + // Calculate the new time. + mTime = qt.addSecs(mTime.minute()*60+mTime.hour()*3600); + updateText(); + emit timeChanged(mTime); +} + +void KTimeEdit::subTime(TQTime qt) +{ + int h, m; + + // Note that we cannot use the same method for determining the new + // time as we did in addTime, because TQTime does not handle adding + // negative seconds well at all. + h = mTime.hour()-qt.hour(); + m = mTime.minute()-qt.minute(); + + if(m < 0) { + m += 60; + h -= 1; + } + + if(h < 0) { + h += 24; + } + + // store the newly calculated time. + mTime.setHMS(h, m, 0); + updateText(); + emit timeChanged(mTime); +} + +void KTimeEdit::keyPressEvent(TQKeyEvent *qke) +{ + switch(qke->key()) { + case Key_Down: + addTime(TQTime(0,1,0)); + break; + case Key_Up: + subTime(TQTime(0,1,0)); + break; + case Key_Prior: + subTime(TQTime(1,0,0)); + break; + case Key_Next: + addTime(TQTime(1,0,0)); + break; + default: + TQComboBox::keyPressEvent(qke); + break; + } // switch +} + +void KTimeEdit::updateText() +{ +// kdDebug(5300) << "KTimeEdit::updateText() " << endl; + TQString s = KGlobal::locale()->formatTime(mTime); + // Set the text but without emitting signals, nor losing the cursor position + TQLineEdit *line = lineEdit(); + line->blockSignals(true); + int pos = line->cursorPosition(); + + // select item with nearest time, must be done while line edit is blocked + // as setCurrentItem() calls setText() with triggers KTimeEdit::changedText() + setCurrentItem((mTime.hour()*4)+((mTime.minute()+7)/15)); + + line->setText(s); + line->setCursorPosition(pos); + line->blockSignals(false); + +// kdDebug(5300) << "KTimeEdit::updateText(): " << s << endl; +} + +bool KTimeEdit::inputIsValid() const +{ + int cursorPos = lineEdit()->cursorPosition(); + TQString str = currentText(); + return validator()->validate( str, cursorPos ) == TQValidator::Acceptable; +} + +void KTimeEdit::changedText() +{ + //kdDebug(5300) << "KTimeEdit::changedText()" << endl; + if ( inputIsValid() ) + { + mTime = getTime(); + emit timeChanged(mTime); + } +} diff --git a/libtdepim/ktimeedit.h b/libtdepim/ktimeedit.h new file mode 100644 index 000000000..11190842a --- /dev/null +++ b/libtdepim/ktimeedit.h @@ -0,0 +1,96 @@ +/* + This file is part of libtdepim. + + Copyright (c) 1999 Preston Brown <pbrown@kde.org> + Copyright (c) 1999 Ian Dawes <iadawes@globalserve.net> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + As a special exception, permission is given to link this program + with any edition of TQt, and distribute the resulting executable, + without including the source code for TQt in the source distribution. +*/ +#ifndef _KTIMEEDIT_H +#define _KTIMEEDIT_H + +#include <tqevent.h> +#include <tqkeycode.h> +#include <tqstring.h> +#include <tqdatetime.h> +#include <tqcombobox.h> + +#include <kapplication.h> +#include <tdepimmacros.h> + +/** + This is a class that provides an easy, user friendly way to edit times. + up/down/ increase or decrease time, respectively. + + @short Provides a way to edit times in a user-friendly manner. + @author Preston Brown, Ian Dawes +*/ +class KDE_EXPORT KTimeEdit : public TQComboBox +{ + Q_OBJECT + TQ_OBJECT + public: + /** constructs a new time edit. */ + KTimeEdit(TQWidget *parent=0, TQTime qt=TQTime(12,0), const char *name=0); + + virtual ~KTimeEdit(); + + /** + Returns, if a time is selected. Can not return false anymore.... + */ + bool hasTime() const; + + /** returns the time that is currently set in the timeLineEdit. */ + TQTime getTime() const; + + /** returns the preferred size policy of the KTimeEdit */ + TQSizePolicy sizePolicy() const; + + /** return true if input is a valid time and false if not */ + bool inputIsValid() const; + + signals: + /** + Emitted every time the time displayed changes. "newt" is the new + time. + */ + void timeChanged(TQTime newt); + + public slots: + /** used to set the time which is displayed to a specific value. */ + void setTime(TQTime qt); + + protected slots: + void active(int); + void hilit(int); + void changedText(); + + protected: + virtual void keyPressEvent(TQKeyEvent *qke); + void addTime(TQTime qt); + void subTime(TQTime qt); + // Update the lineedit text from mTime + void updateText(); + +private: + TQTime mTime; // the widget's displayed time. + //TQString mNoTimeString; +}; + +#endif diff --git a/libtdepim/kvcarddrag.cpp b/libtdepim/kvcarddrag.cpp new file mode 100644 index 000000000..054a424e9 --- /dev/null +++ b/libtdepim/kvcarddrag.cpp @@ -0,0 +1,101 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2002 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "kvcarddrag.h" + +#include <kabc/vcardconverter.h> + +static const char vcard_mime_string[] = "text/x-vcard"; + +#if defined(KABC_VCARD_ENCODING_FIX) +KVCardDrag::KVCardDrag( const TQByteArray &content, TQWidget *dragsource, const char *name ) +#else +KVCardDrag::KVCardDrag( const TQString &content, TQWidget *dragsource, const char *name ) +#endif + : TQStoredDrag( vcard_mime_string, dragsource, name ) +{ + setVCard( content ); +} + +KVCardDrag::KVCardDrag( TQWidget *dragsource, const char *name ) + : TQStoredDrag( vcard_mime_string, dragsource, name ) +{ +#if defined(KABC_VCARD_ENCODING_FIX) + setVCard( TQByteArray() ); +#else + setVCard( TQString() ); +#endif +} + +#if defined(KABC_VCARD_ENCODING_FIX) +void KVCardDrag::setVCard( const TQByteArray &content ) +{ + setEncodedData( content ); +} +#else +void KVCardDrag::setVCard( const TQString &content ) +{ + setEncodedData( content.utf8() ); +} +#endif + +bool KVCardDrag::canDecode( TQMimeSource *e ) +{ + return e->provides( vcard_mime_string ); +} + +#if defined(KABC_VCARD_ENCODING_FIX) +bool KVCardDrag::decode( TQMimeSource *e, TQByteArray &content ) +{ + if ( !canDecode( e ) ) { + return false; + } + content = e->tqencodedData( vcard_mime_string ); + return true; +} +#else +bool KVCardDrag::decode( TQMimeSource *e, TQString &content ) +{ + if ( !canDecode( e ) ) { + return false; + } + content = TQString::fromUtf8( e->tqencodedData( vcard_mime_string ) ); + return true; +} +#endif + +bool KVCardDrag::decode( TQMimeSource *e, KABC::Addressee::List& addressees ) +{ + if ( !canDecode( e ) ) { + return false; + } +#if defined(KABC_VCARD_ENCODING_FIX) + addressees = KABC::VCardConverter().parseVCardsRaw( e->tqencodedData( vcard_mime_string ).data() ); +#else + addressees = KABC::VCardConverter().parseVCards( e->tqencodedData( vcard_mime_string ) ); +#endif + return true; +} + +void KVCardDrag::virtual_hook( int, void* ) +{ /*BASE::virtual_hook( id, data );*/ } + +#include "kvcarddrag.moc" diff --git a/libtdepim/kvcarddrag.h b/libtdepim/kvcarddrag.h new file mode 100644 index 000000000..31885f6ad --- /dev/null +++ b/libtdepim/kvcarddrag.h @@ -0,0 +1,95 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2002 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef KVCARDDRAG_H +#define KVCARDDRAG_H + +#include <tqdragobject.h> +#include <tqstring.h> + +#include <kabc/addressee.h> +#include <kabc/vcardparser.h> // for KABC_VCARD_ENCODING_FIX define +#include <tdepimmacros.h> + +class KVCardDragPrivate; + +/** + * A drag-and-drop object for vcards. The according MIME type + * is set to text/x-vcard. + * + * See the TQt drag'n'drop documentation. + */ +class KDE_EXPORT KVCardDrag : public TQStoredDrag +{ + Q_OBJECT + TQ_OBJECT + + public: + /** + * Constructs an empty vcard drag. + */ + KVCardDrag( TQWidget *dragsource = 0, const char *name = 0 ); + + /** + * Constructs a vcard drag with the @p addressee. + */ +#if defined(KABC_VCARD_ENCODING_FIX) + KVCardDrag( const TQByteArray &content, TQWidget *dragsource = 0, const char *name = 0 ); +#else + KVCardDrag( const TQString &content, TQWidget *dragsource = 0, const char *name = 0 ); +#endif + virtual ~KVCardDrag() {} + + /** + * Sets the vcard of the drag to @p content. + */ +#if defined(KABC_VCARD_ENCODING_FIX) + void setVCard( const TQByteArray &content ); +#else + void setVCard( const TQString &content ); +#endif + /** + * Returns true if the MIME source @p e contains a vcard object. + */ + static bool canDecode( TQMimeSource *e ); + + /** + * Decodes the MIME source @p e and puts the resulting vcard into @p content. + */ +#if defined(KABC_VCARD_ENCODING_FIX) + static bool decode( TQMimeSource *e, TQByteArray &content ); +#else + static bool decode( TQMimeSource *e, TQString &content ); +#endif + + /** + * Decodes the MIME source @p e and puts the resulting vcard into @p addresseess. + */ + static bool decode( TQMimeSource *e, KABC::Addressee::List& addressees ); + + protected: + virtual void virtual_hook( int id, void* data ); + + private: + KVCardDragPrivate *d; +}; + +#endif // KVCARDDRAG_H diff --git a/libtdepim/kwidgetlister.cpp b/libtdepim/kwidgetlister.cpp new file mode 100644 index 000000000..b2b839f43 --- /dev/null +++ b/libtdepim/kwidgetlister.cpp @@ -0,0 +1,178 @@ +/* -*- c++ -*- + kwidgetlister.cpp + + This file is part of libkdenetwork. + Copyright (c) 2001 Marc Mutz <mutz@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this library with any edition of + the TQt library by Trolltech AS, Norway (or with modified versions + of TQt that use the same license as TQt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + TQt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#include "kwidgetlister.h" + +#include <klocale.h> +#include <kdebug.h> + +#include <tqpushbutton.h> +#include <tqlayout.h> +#include <tqhbox.h> + +#include <assert.h> +#include <kguiitem.h> +#include <kpushbutton.h> +#include <kdialog.h> + +KWidgetLister::KWidgetLister( int minWidgets, int maxWidgets, TQWidget *parent, const char* name ) + : TQWidget( parent, name ) +{ + mWidgetList.setAutoDelete(TRUE); + + mMinWidgets = TQMAX( minWidgets, 1 ); + mMaxWidgets = TQMAX( maxWidgets, mMinWidgets + 1 ); + + //--------- the button box + mLayout = new TQVBoxLayout(this, 0, 4); + mButtonBox = new TQHBox(this); + mButtonBox->setSpacing( KDialog::spacingHint() ); + mLayout->addWidget( mButtonBox ); + + mBtnMore = new KPushButton( KGuiItem( i18n( "more widgets", "More" ), "button_more" ), mButtonBox ); + mButtonBox->setStretchFactor( mBtnMore, 0 ); + + mBtnFewer = new KPushButton( KGuiItem( i18n( "fewer widgets", "Fewer" ), "button_fewer" ), mButtonBox ); + mButtonBox->setStretchFactor( mBtnFewer, 0 ); + + TQWidget *spacer = new TQWidget( mButtonBox ); + mButtonBox->setStretchFactor( spacer, 1 ); + + // FIXME: We need a KStdGuiItem::clear here and in other locations to be automagically RTL aware - Martijn + mBtnClear = new KPushButton( KGuiItem( i18n( "clear widgets", "Clear" ), "locationbar_erase" ), mButtonBox ); + mButtonBox->setStretchFactor( mBtnClear, 0 ); + + //---------- connect everything + connect( mBtnMore, TQT_SIGNAL(clicked()), + this, TQT_SLOT(slotMore()) ); + connect( mBtnFewer, TQT_SIGNAL(clicked()), + this, TQT_SLOT(slotFewer()) ); + connect( mBtnClear, TQT_SIGNAL(clicked()), + this, TQT_SLOT(slotClear()) ); + + enableControls(); +} + +KWidgetLister::~KWidgetLister() +{ +} + +void KWidgetLister::slotMore() +{ + // the class should make certain that slotMore can't + // be called when mMaxWidgets are on screen. + assert( (int)mWidgetList.count() < mMaxWidgets ); + + addWidgetAtEnd(); + // adjustSize(); + enableControls(); +} + +void KWidgetLister::slotFewer() +{ + // the class should make certain that slotFewer can't + // be called when mMinWidgets are on screen. + assert( (int)mWidgetList.count() > mMinWidgets ); + + removeLastWidget(); + // adjustSize(); + enableControls(); +} + +void KWidgetLister::slotClear() +{ + setNumberOfShownWidgetsTo( mMinWidgets ); + + // clear remaining widgets + TQPtrListIterator<TQWidget> it( mWidgetList ); + for ( it.toFirst() ; it.current() ; ++it ) + clearWidget( (*it) ); + + // adjustSize(); + enableControls(); + emit clearWidgets(); +} + +void KWidgetLister::addWidgetAtEnd(TQWidget *w) +{ + if (!w) w = this->createWidget(this); + + mLayout->insertWidget( mLayout->findWidget( mButtonBox ), w ); + mWidgetList.append( w ); + w->show(); + enableControls(); + emit widgetAdded(); + emit widgetAdded(w); +} + +void KWidgetLister::removeLastWidget() +{ + // The tqlayout will take care that the + // widget is removed from screen, too. + mWidgetList.removeLast(); + enableControls(); + emit widgetRemoved(); +} + +void KWidgetLister::clearWidget( TQWidget* /*aWidget*/ ) +{ +} + +TQWidget* KWidgetLister::createWidget( TQWidget* parent ) +{ + return new TQWidget( parent ); +} + +void KWidgetLister::setNumberOfShownWidgetsTo( int aNum ) +{ + int superfluousWidgets = TQMAX( (int)mWidgetList.count() - aNum, 0 ); + int missingWidgets = TQMAX( aNum - (int)mWidgetList.count(), 0 ); + + // remove superfluous widgets + for ( ; superfluousWidgets ; superfluousWidgets-- ) + removeLastWidget(); + + // add missing widgets + for ( ; missingWidgets ; missingWidgets-- ) + addWidgetAtEnd(); +} + +void KWidgetLister::enableControls() +{ + int count = mWidgetList.count(); + bool isMaxWidgets = ( count >= mMaxWidgets ); + bool isMinWidgets = ( count <= mMinWidgets ); + + mBtnMore->setEnabled( !isMaxWidgets ); + mBtnFewer->setEnabled( !isMinWidgets ); +} + +#include "kwidgetlister.moc" diff --git a/libtdepim/kwidgetlister.h b/libtdepim/kwidgetlister.h new file mode 100644 index 000000000..db993297f --- /dev/null +++ b/libtdepim/kwidgetlister.h @@ -0,0 +1,154 @@ +/* -*- c++ -*- + kwidgetlister.h + + This file is part of libkdenetwork. + Copyright (c) 2001 Marc Mutz <mutz@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this library with any edition of + the TQt library by Trolltech AS, Norway (or with modified versions + of TQt that use the same license as TQt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + TQt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#ifndef _KWIDGETLISTER_H_ +#define _KWIDGETLISTER_H_ + +#include <tqwidget.h> +#include <tqptrlist.h> +#include <tdepimmacros.h> + +class TQPushButton; +class TQVBoxLayout; +class TQHBox; + +/** + @short Widget that manages a list of other widgets (incl. 'more', 'fewer' and 'clear' buttons). + + Simple widget that nonetheless does a lot of the dirty work for + the filter edit widgets (KMSearchPatternEdit and + KMFilterActionEdit). It provides a growable and shrinkable area + where widget may be displayed in rows. Widgets can be added by + hitting the provided 'More' button, removed by the 'Fewer' button + and cleared (e.g. reset, if an derived class implements that and + removed for all but @ref mMinWidgets). + + To use this widget, derive from it with the template changed to + the type of widgets this class should list. Then reimplement @ref + addWidgetAtEnd, @ref removeLastWidget, calling the original + implementation as necessary. Instantiate an object of the class and + put it in your dialog. + + @author Marc Mutz <Marc@Mutz.com> + @see KMSearchPatternEdit::WidgetLister KMFilterActionEdit::WidgetLister + +*/ + +class KDE_EXPORT KWidgetLister : public TQWidget +{ + Q_OBJECT + TQ_OBJECT +public: + KWidgetLister( int minWidgets=1, int maxWidgets=8, TQWidget* parent=0, const char* name=0 ); + virtual ~KWidgetLister(); + +protected slots: + /** Called whenever the user clicks on the 'more' button. + Reimplementations should call this method, because this + implementation does all the dirty work with adding the widgets + to the tqlayout (through @ref addWidgetAtEnd) and enabling/disabling + the control buttons. */ + virtual void slotMore(); + /** Called whenever the user clicks on the 'fewer' button. + Reimplementations should call this method, because this + implementation does all the dirty work with removing the widgets + from the tqlayout (through @ref removeLastWidget) and + enabling/disabling the control buttons. */ + virtual void slotFewer(); + /** Called whenever the user clicks on the 'clear' button. + Reimplementations should call this method, because this + implementation does all the dirty work with removing all but + @ref mMinWidgets widgets from the tqlayout and enabling/disabling + the control buttons. */ + virtual void slotClear(); + + + +protected: + /** Adds a single widget. Doesn't care if there are already @ref + mMaxWidgets on screen and whether it should enable/disable any + controls. It simply does what it is asked to do. You want to + reimplement this method if you want to initialize the the widget + when showing it on screen. Make sure you call this + implementaion, though, since you cannot put the widget on screen + from derived classes (@p mLayout is private). + Make sure the parent of the TQWidget to add is this KWidgetLister. */ + virtual void addWidgetAtEnd(TQWidget *w =0); + /** Removes a single (always the last) widget. Doesn't care if there + are still only @ref mMinWidgets left on screen and whether it + should enable/disable any controls. It simply does what it is + asked to do. You want to reimplement this method if you want to + save the the widget's state before removing it from screen. Make + sure you call this implementaion, though, since you should not + remove the widget from screen from derived classes. */ + virtual void removeLastWidget(); + /** Called to clear a given widget. The default implementation does + nothing. */ + virtual void clearWidget( TQWidget* ); + /** Because QT 2.x does not support signals/slots in template + classes, we are forced to emulate this by forcing the + implementers of subclasses of KWidgetLister to reimplement this + function which replaces the "@p new @p T" call. */ + virtual TQWidget* createWidget( TQWidget *parent ); + /** Sets the number of widgets on scrren to exactly @p aNum. Doesn't + check if @p aNum is inside the range @p + [mMinWidgets,mMaxWidgets]. */ + virtual void setNumberOfShownWidgetsTo( int aNum ); + /** The list of widgets. Note that this list is set to auto-delete, + meaning that widgets that are removed from the screen by either + @ref slotFewer or @ref slotClear will be destroyed! */ + TQPtrList<TQWidget> mWidgetList; + /** The minimum number of widgets that are to stay on screen. */ + int mMinWidgets; + /** The maximum number of widgets that are to be shown on screen. */ + int mMaxWidgets; + +signals: + /** This signal is emitted whenever a widget was added */ + void widgetAdded(); + /** This signal is emitted whenever a widget was added */ + void widgetAdded(TQWidget *); + /** This signal is emitted whenever a widget was removed */ + void widgetRemoved(); + /** This signal is emitted whenever the clear button is clicked */ + void clearWidgets(); + +private: + void enableControls(); + + TQPushButton *mBtnMore, *mBtnFewer, *mBtnClear; + TQVBoxLayout *mLayout; + TQHBox *mButtonBox; +}; + + + +#endif /* _KWIDGETLISTER_H_ */ diff --git a/libtdepim/kxface.cpp b/libtdepim/kxface.cpp new file mode 100644 index 000000000..210fefb5f --- /dev/null +++ b/libtdepim/kxface.cpp @@ -0,0 +1,729 @@ +/* + This file is part of libtdepim. + + Original compface: + Copyright (c) James Ashton - Sydney University - June 1990. + + Additions for KDE: + Copyright (c) 2004 Jakob Schröter <js@camaya.net> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "kxface.h" + +#include <kdebug.h> + +#include <tqbuffer.h> +#include <tqcstring.h> +#include <tqimage.h> +#include <tqregexp.h> +#include <tqstring.h> +#include <tqpainter.h> + +#include <stdlib.h> +#include <string.h> + +#define GEN(g) F[h] ^= G.g[k]; break + +#define BITSPERDIG 4 +#define DIGITS (PIXELS / BITSPERDIG) +#define DIGSPERWORD 4 +#define WORDSPERLINE (WIDTH / DIGSPERWORD / BITSPERDIG) + +/* compressed output uses the full range of printable characters. + * in ascii these are in a contiguous block so we just need to know + * the first and last. The total number of printables is needed too */ +#define FIRSTPRINT '!' +#define LASTPRINT '~' +#define NUMPRINTS (LASTPRINT - FIRSTPRINT + 1) + +/* output line length for compressed data */ +#define MAXLINELEN 78 + +/* Portable, very large unsigned integer arithmetic is needed. + * Implementation uses arrays of WORDs. COMPs must have at least + * twice as many bits as WORDs to handle intermediate results */ +#define COMP unsigned long +#define WORDCARRY (1 << BITSPERWORD) +#define WORDMASK (WORDCARRY - 1) + +#define ERR_OK 0 /* successful completion */ +#define ERR_EXCESS 1 /* completed OK but some input was ignored */ +#define ERR_INSUFF -1 /* insufficient input. Bad face format? */ +#define ERR_INTERNAL -2 /* Arithmetic overflow or buffer overflow */ + +#define BLACK 0 +#define GREY 1 +#define WHITE 2 + +#define MAX_XFACE_LENGTH 2048 + +using namespace KPIM; + +KXFace::KXFace() +{ + NumProbs = 0; +} + +KXFace::~KXFace() +{ +} + +TQString KXFace::fromImage( const TQImage &image ) +{ + if( image.isNull() ) + return TQString(); + + TQImage scaledImg = image.smoothScale( 48, 48 ); + TQByteArray ba; + TQBuffer buffer( ba ); + buffer.open( IO_WriteOnly ); + scaledImg.save( &buffer, "XBM" ); + TQString xbm( ba ); + xbm.remove( 0, xbm.find( "{" ) + 1 ); + xbm.truncate( xbm.find( "}" ) ); + xbm.remove( " " ); + xbm.remove( "," ); + xbm.remove( "0x" ); + xbm.remove( "\n" ); + xbm.truncate( 576 ); + TQCString tmp = TQCString( xbm.latin1() ); + uint len = tmp.length(); + for( uint i=0; i<len; ++i ) + { + switch( tmp[i] ) + { + case '1': tmp[i] = '8'; break; + case '2': tmp[i] = '4'; break; + case '3': tmp[i] = 'c'; break; + case '4': tmp[i] = '2'; break; + case '5': tmp[i] = 'a'; break; + case '7': tmp[i] = 'e'; break; + case '8': tmp[i] = '1'; break; + case 'A': + case 'a': tmp[i] = '5'; break; + case 'B': + case 'b': tmp[i] = 'd'; break; + case 'C': + case 'c': tmp[i] = '3'; break; + case 'D': + case 'd': tmp[i] = 'b'; break; + case 'E': + case 'e': tmp[i] = '7'; break; + } + if ( i % 2 ) + { + char t = tmp[i]; + tmp[i] = tmp[i-1]; + tmp[i-1] = t; + } + } + tmp.replace( TQRegExp( "(\\w{12})" ), "\\1\n" ); + tmp.replace( TQRegExp( "(\\w{4})" ), "0x\\1," ); + len = tmp.length(); + char *fbuf = (char *)malloc( len + 1 ); + strncpy( fbuf, (const char *)tmp, len ); + fbuf[len] = '\0'; + if ( !( status = setjmp( comp_env ) ) ) + { + ReadFace( fbuf ); + GenFace(); + CompAll( fbuf ); + } + TQString ret( fbuf ); + free( fbuf ); + + return ret; +} + +TQImage KXFace::toImage(const TQString &xface) +{ + if ( xface.length() > MAX_XFACE_LENGTH ) + return TQImage(); + + char *fbuf = (char *)malloc( MAX_XFACE_LENGTH ); + memset( fbuf, '\0', MAX_XFACE_LENGTH ); + strncpy( fbuf, xface.latin1(), xface.length() ); + TQCString img; + if ( !( status = setjmp( comp_env ) ) ) + { + UnCompAll( fbuf );/* compress otherwise */ + UnGenFace(); + img = WriteFace(); + } + free( fbuf ); + TQImage p; + p.loadFromData( img, "XBM" ); + + return p; +} + +//============================================================================ +// more or less original compface 1.4 source + +void KXFace::RevPush(const Prob *p) +{ + if (NumProbs >= PIXELS * 2 - 1) + longjmp(comp_env, ERR_INTERNAL); + ProbBuf[NumProbs++] = (Prob *) p; +} + +void KXFace::BigPush(Prob *p) +{ + static unsigned char tmp; + + BigDiv(p->p_range, &tmp); + BigMul(0); + BigAdd(tmp + p->p_offset); +} + +int KXFace::BigPop(register const Prob *p) +{ + static unsigned char tmp; + register int i; + + BigDiv(0, &tmp); + i = 0; + while ((tmp < p->p_offset) || (tmp >= p->p_range + p->p_offset)) + { + p++; + i++; + } + BigMul(p->p_range); + BigAdd(tmp - p->p_offset); + return i; +} + + +/* Divide B by a storing the result in B and the remainder in the word + * pointer to by r + */ +void KXFace::BigDiv(register unsigned char a, register unsigned char *r) +{ + register int i; + register unsigned char *w; + register COMP c, d; + + a &= WORDMASK; + if ((a == 1) || (B.b_words == 0)) + { + *r = 0; + return; + } + if (a == 0) /* treat this as a == WORDCARRY */ + { /* and just shift everything right a WORD (unsigned char)*/ + i = --B.b_words; + w = B.b_word; + *r = *w; + while (i--) + { + *w = *(w + 1); + w++; + } + *w = 0; + return; + } + w = B.b_word + (i = B.b_words); + c = 0; + while (i--) + { + c <<= BITSPERWORD; + c += (COMP)*--w; + d = c / (COMP)a; + c = c % (COMP)a; + *w = (unsigned char)(d & WORDMASK); + } + *r = c; + if (B.b_word[B.b_words - 1] == 0) + B.b_words--; +} + +/* Multiply a by B storing the result in B + */ +void KXFace::BigMul(register unsigned char a) +{ + register int i; + register unsigned char *w; + register COMP c; + + a &= WORDMASK; + if ((a == 1) || (B.b_words == 0)) + return; + if (a == 0) /* treat this as a == WORDCARRY */ + { /* and just shift everything left a WORD (unsigned char) */ + if ((i = B.b_words++) >= MAXWORDS - 1) + longjmp(comp_env, ERR_INTERNAL); + w = B.b_word + i; + while (i--) + { + *w = *(w - 1); + w--; + } + *w = 0; + return; + } + i = B.b_words; + w = B.b_word; + c = 0; + while (i--) + { + c += (COMP)*w * (COMP)a; + *(w++) = (unsigned char)(c & WORDMASK); + c >>= BITSPERWORD; + } + if (c) + { + if (B.b_words++ >= MAXWORDS) + longjmp(comp_env, ERR_INTERNAL); + *w = (COMP)(c & WORDMASK); + } +} + +/* Add to a to B storing the result in B + */ +void KXFace::BigAdd(unsigned char a) +{ + register int i; + register unsigned char *w; + register COMP c; + + a &= WORDMASK; + if (a == 0) + return; + i = 0; + w = B.b_word; + c = a; + while ((i < B.b_words) && c) + { + c += (COMP)*w; + *w++ = (unsigned char)(c & WORDMASK); + c >>= BITSPERWORD; + i++; + } + if ((i == B.b_words) && c) + { + if (B.b_words++ >= MAXWORDS) + longjmp(comp_env, ERR_INTERNAL); + *w = (COMP)(c & WORDMASK); + } +} + +void KXFace::BigClear() +{ + B.b_words = 0; +} + +TQCString KXFace::WriteFace() +{ + register char *s; + register int i, j, bits, digits, words; + int digsperword = DIGSPERWORD; + int wordsperline = WORDSPERLINE; + TQCString t( "#define noname_width 48\n#define noname_height 48\nstatic char noname_bits[] = {\n " ); + j = t.length() - 1; + + s = F; + bits = digits = words = i = 0; + t.resize( MAX_XFACE_LENGTH ); + digsperword = 2; + wordsperline = 15; + while ( s < F + PIXELS ) + { + if ( ( bits == 0 ) && ( digits == 0 ) ) + { + t[j++] = '0'; + t[j++] = 'x'; + } + if ( *(s++) ) + i = ( i >> 1 ) | 0x8; + else + i >>= 1; + if ( ++bits == BITSPERDIG ) + { + j++; + t[j-( ( digits & 1 ) * 2 )] = *(i + HexDigits); + bits = i = 0; + if ( ++digits == digsperword ) + { + if ( s >= F + PIXELS ) + break; + t[j++] = ','; + digits = 0; + if ( ++words == wordsperline ) + { + t[j++] = '\n'; + t[j++] = ' '; + words = 0; + } + } + } + } + t.resize( j + 1 ); + t += "};\n"; + return t; +} + +void KXFace::UnCompAll(char *fbuf) +{ + register char *p; + + BigClear(); + BigRead(fbuf); + p = F; + while (p < F + PIXELS) + *(p++) = 0; + UnCompress(F, 16, 16, 0); + UnCompress(F + 16, 16, 16, 0); + UnCompress(F + 32, 16, 16, 0); + UnCompress(F + WIDTH * 16, 16, 16, 0); + UnCompress(F + WIDTH * 16 + 16, 16, 16, 0); + UnCompress(F + WIDTH * 16 + 32, 16, 16, 0); + UnCompress(F + WIDTH * 32, 16, 16, 0); + UnCompress(F + WIDTH * 32 + 16, 16, 16, 0); + UnCompress(F + WIDTH * 32 + 32, 16, 16, 0); +} + +void KXFace::UnCompress(char *f, int wid, int hei, int lev) +{ + switch (BigPop(&levels[lev][0])) + { + case WHITE : + return; + case BLACK : + PopGreys(f, wid, hei); + return; + default : + wid /= 2; + hei /= 2; + lev++; + UnCompress(f, wid, hei, lev); + UnCompress(f + wid, wid, hei, lev); + UnCompress(f + hei * WIDTH, wid, hei, lev); + UnCompress(f + wid + hei * WIDTH, wid, hei, lev); + return; + } +} + +void KXFace::BigWrite(register char *fbuf) +{ + static unsigned char tmp; + static char buf[DIGITS]; + register char *s; + register int i; + + s = buf; + while (B.b_words > 0) + { + BigDiv(NUMPRINTS, &tmp); + *(s++) = tmp + FIRSTPRINT; + } + i = 7; // leave room for the field name on the first line + *(fbuf++) = ' '; + while (s-- > buf) + { + if (i == 0) + *(fbuf++) = ' '; + *(fbuf++) = *s; + if (++i >= MAXLINELEN) + { + *(fbuf++) = '\n'; + i = 0; + } + } + if (i > 0) + *(fbuf++) = '\n'; + *(fbuf++) = '\0'; +} + +void KXFace::BigRead(register char *fbuf) +{ + register int c; + + while (*fbuf != '\0') + { + c = *(fbuf++); + if ((c < FIRSTPRINT) || (c > LASTPRINT)) + continue; + BigMul(NUMPRINTS); + BigAdd((unsigned char)(c - FIRSTPRINT)); + } +} + +void KXFace::ReadFace(char *fbuf) +{ + register int c, i; + register char *s, *t; + + t = s = fbuf; + for(i = strlen(s); i > 0; i--) + { + c = (int)*(s++); + if ((c >= '0') && (c <= '9')) + { + if (t >= fbuf + DIGITS) + { + status = ERR_EXCESS; + break; + } + *(t++) = c - '0'; + } + else if ((c >= 'A') && (c <= 'F')) + { + if (t >= fbuf + DIGITS) + { + status = ERR_EXCESS; + break; + } + *(t++) = c - 'A' + 10; + } + else if ((c >= 'a') && (c <= 'f')) + { + if (t >= fbuf + DIGITS) + { + status = ERR_EXCESS; + break; + } + *(t++) = c - 'a' + 10; + } + else if (((c == 'x') || (c == 'X')) && (t > fbuf) && (*(t-1) == 0)) + t--; + } + if (t < fbuf + DIGITS) + longjmp(comp_env, ERR_INSUFF); + s = fbuf; + t = F; + c = 1 << (BITSPERDIG - 1); + while (t < F + PIXELS) + { + *(t++) = (*s & c) ? 1 : 0; + if ((c >>= 1) == 0) + { + s++; + c = 1 << (BITSPERDIG - 1); + } + } +} + +void KXFace::GenFace() +{ + static char newp[PIXELS]; + register char *f1; + register char *f2; + register int i; + + f1 = newp; + f2 = F; + i = PIXELS; + while (i-- > 0) + *(f1++) = *(f2++); + Gen(newp); +} + +void KXFace::UnGenFace() +{ + Gen(F); +} + +// static +void KXFace::Gen(register char *f) +{ + register int m, l, k, j, i, h; + + for (j = 0; j < HEIGHT; j++) + { + for (i = 0; i < WIDTH; i++) + { + h = i + j * WIDTH; + k = 0; + for (l = i - 2; l <= i + 2; l++) + for (m = j - 2; m <= j; m++) + { + if ((l >= i) && (m == j)) + continue; + if ((l > 0) && (l <= WIDTH) && (m > 0)) + k = *(f + l + m * WIDTH) ? k * 2 + 1 : k * 2; + } + switch (i) + { + case 1 : + switch (j) + { + case 1 : GEN(g_22); + case 2 : GEN(g_21); + default : GEN(g_20); + } + break; + case 2 : + switch (j) + { + case 1 : GEN(g_12); + case 2 : GEN(g_11); + default : GEN(g_10); + } + break; + case WIDTH - 1 : + switch (j) + { + case 1 : GEN(g_42); + case 2 : GEN(g_41); + default : GEN(g_40); + } + break; + /* i runs from 0 to WIDTH-1, so case can never occur. I leave the code in + because it appears exactly like this in the original compface code. + case WIDTH : + switch (j) + { + case 1 : GEN(g_32); + case 2 : GEN(g_31); + default : GEN(g_30); + } + break; + */ + default : + switch (j) + { + case 1 : GEN(g_02); + case 2 : GEN(g_01); + default : GEN(g_00); + } + break; + } + } + } +} + +void KXFace::PopGreys(char *f, int wid, int hei) +{ + if (wid > 3) + { + wid /= 2; + hei /= 2; + PopGreys(f, wid, hei); + PopGreys(f + wid, wid, hei); + PopGreys(f + WIDTH * hei, wid, hei); + PopGreys(f + WIDTH * hei + wid, wid, hei); + } + else + { + wid = BigPop(freqs); + if (wid & 1) + *f = 1; + if (wid & 2) + *(f + 1) = 1; + if (wid & 4) + *(f + WIDTH) = 1; + if (wid & 8) + *(f + WIDTH + 1) = 1; + } +} + +void KXFace::CompAll(char *fbuf) +{ + Compress(F, 16, 16, 0); + Compress(F + 16, 16, 16, 0); + Compress(F + 32, 16, 16, 0); + Compress(F + WIDTH * 16, 16, 16, 0); + Compress(F + WIDTH * 16 + 16, 16, 16, 0); + Compress(F + WIDTH * 16 + 32, 16, 16, 0); + Compress(F + WIDTH * 32, 16, 16, 0); + Compress(F + WIDTH * 32 + 16, 16, 16, 0); + Compress(F + WIDTH * 32 + 32, 16, 16, 0); + BigClear(); + while (NumProbs > 0) + BigPush(ProbBuf[--NumProbs]); + BigWrite(fbuf); +} + +void KXFace::Compress(register char *f, register int wid, register int hei, register int lev) +{ + if (AllWhite(f, wid, hei)) + { + RevPush(&levels[lev][WHITE]); + return; + } + if (AllBlack(f, wid, hei)) + { + RevPush(&levels[lev][BLACK]); + PushGreys(f, wid, hei); + return; + } + RevPush(&levels[lev][GREY]); + wid /= 2; + hei /= 2; + lev++; + Compress(f, wid, hei, lev); + Compress(f + wid, wid, hei, lev); + Compress(f + hei * WIDTH, wid, hei, lev); + Compress(f + wid + hei * WIDTH, wid, hei, lev); +} + +int KXFace::AllWhite(char *f, int wid, int hei) +{ + return ((*f == 0) && Same(f, wid, hei)); +} + +int KXFace::AllBlack(char *f, int wid, int hei) +{ + if (wid > 3) + { + wid /= 2; + hei /= 2; + return (AllBlack(f, wid, hei) && AllBlack(f + wid, wid, hei) && + AllBlack(f + WIDTH * hei, wid, hei) && + AllBlack(f + WIDTH * hei + wid, wid, hei)); + } + else + return (*f || *(f + 1) || *(f + WIDTH) || *(f + WIDTH + 1)); +} + +int KXFace::Same(register char *f, register int wid, register int hei) +{ + register char val, *row; + register int x; + + val = *f; + while (hei--) + { + row = f; + x = wid; + while (x--) + if (*(row++) != val) + return(0); + f += WIDTH; + } + return 1; +} + +void KXFace::PushGreys(char *f, int wid, int hei) +{ + if (wid > 3) + { + wid /= 2; + hei /= 2; + PushGreys(f, wid, hei); + PushGreys(f + wid, wid, hei); + PushGreys(f + WIDTH * hei, wid, hei); + PushGreys(f + WIDTH * hei + wid, wid, hei); + } + else + RevPush(freqs + *f + 2 * *(f + 1) + 4 * *(f + WIDTH) + + 8 * *(f + WIDTH + 1)); +} + + +#include "kxface.moc" diff --git a/libtdepim/kxface.h b/libtdepim/kxface.h new file mode 100644 index 000000000..770232306 --- /dev/null +++ b/libtdepim/kxface.h @@ -0,0 +1,594 @@ +/* + This file is part of libtdepim. + + Original compface: + Copyright (c) James Ashton - Sydney University - June 1990. + + Additions for KDE: + Copyright (c) 2004 Jakob Schröter <js@camaya.net> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef __KXFACE_H +#define __KXFACE_H + + +#include <tqobject.h> +#include <setjmp.h> + +#include <tdepimmacros.h> + +// #define WIDTH 48 +// #define HEIGHT WIDTH + +/* total number of pixels and digits */ +// #define PIXELS (WIDTH * HEIGHT) +// #define WORD unsigned char +// #define MAXWORDS ((PIXELS * 2 + BITSPERWORD - 1) / BITSPERWORD) +// #define BITSPERWORD 8 + + +typedef struct guesses +{ + char g_00[1<<12]; + char g_01[1<<7]; + char g_02[1<<2]; + char g_10[1<<9]; + char g_20[1<<6]; + char g_30[1<<8]; + char g_40[1<<10]; + char g_11[1<<5]; + char g_21[1<<3]; + char g_31[1<<5]; + char g_41[1<<6]; + char g_12[1<<1]; + char g_22[1<<0]; + char g_32[1<<2]; + char g_42[1<<2]; +} Guesses; + +static const Guesses G = +{ + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, + 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, + 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, + 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, + 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, + 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, + 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, + 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, + 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, + 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, + 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, + 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, + 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, + 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, + 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, + 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, + 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, + 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, + 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, + 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, + 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, + 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, + 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, + 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, + 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, + 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, + 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, + 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, + 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, + 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, + 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, + 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + }, + { + 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, + 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, + 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, + 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + }, + { + 0, 1, 0, 1, + }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, + 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, + 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, + 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, + 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, + 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, + 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + }, + { + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, + }, + { + 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, + 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, + 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, + }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, + 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, + 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, + 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, + 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, + 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + }, + { + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, + 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + }, + { + 0, 0, 0, 1, 0, 1, 1, 1, + }, + { + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, + 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, + }, + { + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, + 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + }, + { + 0, 1, + }, + { + 0, + }, + { + 0, 0, 0, 1, + }, + { + 0, 0, 0, 1, + } +}; + +typedef struct prob +{ + int p_range; + int p_offset; +} Prob; + + + +static const Prob levels[4][3] = { + {{1, 255}, {251, 0}, {4, 251}}, /* Top of tree almost always grey */ + {{1, 255}, {200, 0}, {55, 200}}, + {{33, 223}, {159, 0}, {64, 159}}, + {{131, 0}, {0, 0}, {125, 131}} /* Grey disallowed at bottom */ +}; + +static const Prob freqs[16] = { + {0, 0}, {38, 0}, {38, 38}, {13, 152}, + {38, 76}, {13, 165}, {13, 178}, {6, 230}, + {38, 114}, {13, 191}, {13, 204}, {6, 236}, + {13, 217}, {6, 242}, {5, 248}, {3, 253} +}; + +static const char HexDigits[] = "0123456789ABCDEF"; + + +class TQImage; +class TQString; + +namespace KPIM { + + class KDE_EXPORT KXFace : public TQObject + { + Q_OBJECT + TQ_OBJECT + + public: + KXFace(); + virtual ~KXFace(); + + /** + * generates the xface string from @p image + */ + TQString fromImage(const TQImage &image); + + /** + * creates a pixmap from @p xface + */ + TQImage toImage(const TQString &xface); + + + private: + static const int WIDTH = 48; + static const int HEIGHT = WIDTH; + static const int PIXELS = (WIDTH * HEIGHT); + static const int BITSPERWORD = 8; + static const int MAXWORDS = ((PIXELS * 2 + BITSPERWORD - 1) / BITSPERWORD); + + int NumProbs; + int status; + int compface_xbitmap; + + char F[PIXELS]; + Prob *ProbBuf[PIXELS * 2]; + + jmp_buf comp_env; + + typedef struct bigint + { + int b_words; + unsigned char b_word[MAXWORDS]; + } BigInt; + + BigInt B; + + void RevPush(const Prob *p); + void BigPush(Prob *p); + int BigPop(register const Prob *p); + void BigDiv(register unsigned char a, register unsigned char *r); + void BigMul(register unsigned char a); + void BigAdd(unsigned char a); + void BigClear(); + + TQCString WriteFace(); + void UnCompAll(char *fbuf); + void UnCompress(register char *f, register int wid, register int hei, register int lev); + void BigWrite(register char *fbuf); + void BigRead(register char *fbuf); + void ReadFace(char *fbuf); + void GenFace(); + void UnGenFace(); + void Gen(register char *f); + void PopGreys(char *f, int wid, int hei); + void CompAll(char *fbuf); + void Compress(register char *f, register int wid, register int hei, register int lev); + int AllWhite(char *f, int wid, int hei); + int AllBlack(char *f, int wid, int hei); + int Same(register char *f, register int wid, register int hei); + void PushGreys(char *f, int wid, int hei); + + + }; +} // namespace KPIM +#endif // __KXFACE_H diff --git a/libtdepim/ldapclient.cpp b/libtdepim/ldapclient.cpp new file mode 100644 index 000000000..f8497f084 --- /dev/null +++ b/libtdepim/ldapclient.cpp @@ -0,0 +1,613 @@ +/* kldapclient.cpp - LDAP access + * Copyright (C) 2002 Klarälvdalens Datakonsult AB + * + * Author: Steffen Hansen <hansen@kde.org> + * + * Ported to KABC by Daniel Molkentin <molkentin@kde.org> + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This file 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + + + +#include <tqfile.h> +#include <tqimage.h> +#include <tqlabel.h> +#include <tqpixmap.h> +#include <tqtextstream.h> +#include <tqurl.h> + +#include <kabc/ldapurl.h> +#include <kabc/ldif.h> +#include <kapplication.h> +#include <kconfig.h> +#include <kdebug.h> +#include <kdirwatch.h> +#include <kmdcodec.h> +#include <kprotocolinfo.h> +#include <kstandarddirs.h> +#include <kstaticdeleter.h> + +#include "ldapclient.h" + +using namespace KPIM; + +KConfig *KPIM::LdapSearch::s_config = 0L; +static KStaticDeleter<KConfig> configDeleter; + +TQString LdapObject::toString() const +{ + TQString result = TQString::tqfromLatin1( "\ndn: %1\n" ).tqarg( dn ); + for ( LdapAttrMap::ConstIterator it = attrs.begin(); it != attrs.end(); ++it ) { + TQString attr = it.key(); + for ( LdapAttrValue::ConstIterator it2 = (*it).begin(); it2 != (*it).end(); ++it2 ) { + result += TQString::fromUtf8( KABC::LDIF::assembleLine( attr, *it2, 76 ) ) + "\n"; + } + } + + return result; +} + +void LdapObject::clear() +{ + dn = TQString(); + objectClass = TQString(); + attrs.clear(); +} + +void LdapObject::assign( const LdapObject& that ) +{ + if ( &that != this ) { + dn = that.dn; + attrs = that.attrs; + client = that.client; + } +} + +LdapClient::LdapClient( int clientNumber, TQObject* parent, const char* name ) + : TQObject( parent, name ), mJob( 0 ), mActive( false ), mReportObjectClass( false ) +{ +// d = new LdapClientPrivate; + mClientNumber = clientNumber; + mCompletionWeight = 50 - mClientNumber; +} + +LdapClient::~LdapClient() +{ + cancelQuery(); +// delete d; d = 0; +} + +void LdapClient::setAttrs( const TQStringList& attrs ) +{ + mAttrs = attrs; + for ( TQStringList::Iterator it = mAttrs.begin(); it != mAttrs.end(); ++it ) + if( (*it).lower() == "objectclass" ){ + mReportObjectClass = true; + return; + } + mAttrs << "objectClass"; // via objectClass we detect distribution lists + mReportObjectClass = false; +} + +void LdapClient::startQuery( const TQString& filter ) +{ + cancelQuery(); + KABC::LDAPUrl url; + + url.setProtocol( ( mServer.security() == LdapServer::SSL ) ? "ldaps" : "ldap" ); + if ( mServer.auth() != LdapServer::Anonymous ) { + url.setUser( mServer.user() ); + url.setPass( mServer.pwdBindDN() ); + } + url.setHost( mServer.host() ); + url.setPort( mServer.port() ); + url.setExtension( "x-ver", TQString::number( mServer.version() ) ); + url.setDn( mServer.baseDN() ); + url.setDn( mServer.baseDN() ); + if ( mServer.security() == LdapServer::TLS ) url.setExtension( "x-tls","" ); + if ( mServer.auth() == LdapServer::SASL ) { + url.setExtension( "x-sasl","" ); + if ( !mServer.bindDN().isEmpty() ) url.setExtension( "x-bindname", mServer.bindDN() ); + if ( !mServer.mech().isEmpty() ) url.setExtension( "x-mech", mServer.mech() ); + } + if ( mServer.timeLimit() != 0 ) url.setExtension( "x-timelimit", + TQString::number( mServer.timeLimit() ) ); + if ( mServer.sizeLimit() != 0 ) url.setExtension( "x-sizelimit", + TQString::number( mServer.sizeLimit() ) ); + + url.setAttributes( mAttrs ); + url.setScope( mScope == "one" ? KABC::LDAPUrl::One : KABC::LDAPUrl::Sub ); + url.setFilter( "("+filter+")" ); + + kdDebug(5300) << "LdapClient: Doing query: " << url.prettyURL() << endl; + + startParseLDIF(); + mActive = true; + mJob = KIO::get( url, false, false ); + connect( mJob, TQT_SIGNAL( data( KIO::Job*, const TQByteArray& ) ), + this, TQT_SLOT( slotData( KIO::Job*, const TQByteArray& ) ) ); + connect( mJob, TQT_SIGNAL( infoMessage( KIO::Job*, const TQString& ) ), + this, TQT_SLOT( slotInfoMessage( KIO::Job*, const TQString& ) ) ); + connect( mJob, TQT_SIGNAL( result( KIO::Job* ) ), + this, TQT_SLOT( slotDone() ) ); +} + +void LdapClient::cancelQuery() +{ + if ( mJob ) { + mJob->kill(); + mJob = 0; + } + + mActive = false; +} + +void LdapClient::slotData( KIO::Job*, const TQByteArray& data ) +{ + parseLDIF( data ); +} + +void LdapClient::slotInfoMessage( KIO::Job*, const TQString & ) +{ + //qDebug("Job said \"%s\"", info.latin1()); +} + +void LdapClient::slotDone() +{ + endParseLDIF(); + mActive = false; +#if 0 + for ( TQValueList<LdapObject>::Iterator it = mObjects.begin(); it != mObjects.end(); ++it ) { + qDebug( (*it).toString().latin1() ); + } +#endif + int err = mJob->error(); + if ( err && err != KIO::ERR_USER_CANCELED ) { + emit error( mJob->errorString() ); + } + emit done(); +} + +void LdapClient::startParseLDIF() +{ + mCurrentObject.clear(); + mLdif.startParsing(); +} + +void LdapClient::endParseLDIF() +{ +} + +void LdapClient::finishCurrentObject() +{ + mCurrentObject.dn = mLdif.dn(); + const TQString sClass( mCurrentObject.objectClass.lower() ); + if( sClass == "groupofnames" || sClass == "kolabgroupofnames" ){ + LdapAttrMap::ConstIterator it = mCurrentObject.attrs.find("mail"); + if( it == mCurrentObject.attrs.end() ){ + // No explicit mail address found so far? + // Fine, then we use the address stored in the DN. + TQString sMail; + TQStringList lMail = TQStringList::split(",dc=", mCurrentObject.dn); + const int n = lMail.count(); + if( n ){ + if( lMail.first().lower().startsWith("cn=") ){ + sMail = lMail.first().simplifyWhiteSpace().mid(3); + if( 1 < n ) + sMail.append('@'); + for( int i=1; i<n; ++i){ + sMail.append( lMail[i] ); + if( i < n-1 ) + sMail.append('.'); + } + mCurrentObject.attrs["mail"].append( sMail.utf8() ); + } + } + } + } + mCurrentObject.client = this; + emit result( mCurrentObject ); + mCurrentObject.clear(); +} + +void LdapClient::parseLDIF( const TQByteArray& data ) +{ + //kdDebug(5300) << "LdapClient::parseLDIF( " << TQCString(data.data(), data.size()+1) << " )" << endl; + if ( data.size() ) { + mLdif.setLDIF( data ); + } else { + mLdif.endLDIF(); + } + + KABC::LDIF::ParseVal ret; + TQString name; + do { + ret = mLdif.nextItem(); + switch ( ret ) { + case KABC::LDIF::Item: + { + name = mLdif.attr(); + // Must make a copy! TQByteArray is explicitely shared + TQByteArray value = mLdif.val().copy(); + bool bIsObjectClass = name.lower() == "objectclass"; + if( bIsObjectClass ) + mCurrentObject.objectClass = TQString::fromUtf8( value, value.size() ); + if( mReportObjectClass || !bIsObjectClass ) + mCurrentObject.attrs[ name ].append( value ); + //kdDebug(5300) << "LdapClient::parseLDIF(): name=" << name << " value=" << TQCString(value.data(), value.size()+1) << endl; + } + break; + case KABC::LDIF::EndEntry: + finishCurrentObject(); + break; + default: + break; + } + } while ( ret != KABC::LDIF::MoreData ); +} + +int LdapClient::clientNumber() const +{ + return mClientNumber; +} + +int LdapClient::completionWeight() const +{ + return mCompletionWeight; +} + +void LdapClient::setCompletionWeight( int weight ) +{ + mCompletionWeight = weight; +} + +void LdapSearch::readConfig( LdapServer &server, KConfig *config, int j, bool active ) +{ + TQString prefix; + if ( active ) prefix = "Selected"; + TQString host = config->readEntry( prefix + TQString( "Host%1" ).tqarg( j ), "" ).stripWhiteSpace(); + if ( !host.isEmpty() ) + server.setHost( host ); + + int port = config->readNumEntry( prefix + TQString( "Port%1" ).tqarg( j ), 389 ); + server.setPort( port ); + + TQString base = config->readEntry( prefix + TQString( "Base%1" ).tqarg( j ), "" ).stripWhiteSpace(); + if ( !base.isEmpty() ) + server.setBaseDN( base ); + + TQString user = config->readEntry( prefix + TQString( "User%1" ).tqarg( j ) ).stripWhiteSpace(); + if ( !user.isEmpty() ) + server.setUser( user ); + + TQString bindDN = config->readEntry( prefix + TQString( "Bind%1" ).tqarg( j ) ).stripWhiteSpace(); + if ( !bindDN.isEmpty() ) + server.setBindDN( bindDN ); + + TQString pwdBindDN = config->readEntry( prefix + TQString( "PwdBind%1" ).tqarg( j ) ); + if ( !pwdBindDN.isEmpty() ) + server.setPwdBindDN( pwdBindDN ); + + server.setTimeLimit( config->readNumEntry( prefix + TQString( "TimeLimit%1" ).tqarg( j ) ) ); + server.setSizeLimit( config->readNumEntry( prefix + TQString( "SizeLimit%1" ).tqarg( j ) ) ); + server.setVersion( config->readNumEntry( prefix + TQString( "Version%1" ).tqarg( j ), 3 ) ); + server.setSecurity( config->readNumEntry( prefix + TQString( "Security%1" ).tqarg( j ) ) ); + server.setAuth( config->readNumEntry( prefix + TQString( "Auth%1" ).tqarg( j ) ) ); + server.setMech( config->readEntry( prefix + TQString( "Mech%1" ).tqarg( j ) ) ); +} + +void LdapSearch::writeConfig( const LdapServer &server, KConfig *config, int j, bool active ) +{ + TQString prefix; + if ( active ) prefix = "Selected"; + config->writeEntry( prefix + TQString( "Host%1" ).tqarg( j ), server.host() ); + config->writeEntry( prefix + TQString( "Port%1" ).tqarg( j ), server.port() ); + config->writeEntry( prefix + TQString( "Base%1" ).tqarg( j ), server.baseDN() ); + config->writeEntry( prefix + TQString( "User%1" ).tqarg( j ), server.user() ); + config->writeEntry( prefix + TQString( "Bind%1" ).tqarg( j ), server.bindDN() ); + config->writeEntry( prefix + TQString( "PwdBind%1" ).tqarg( j ), server.pwdBindDN() ); + config->writeEntry( prefix + TQString( "TimeLimit%1" ).tqarg( j ), server.timeLimit() ); + config->writeEntry( prefix + TQString( "SizeLimit%1" ).tqarg( j ), server.sizeLimit() ); + config->writeEntry( prefix + TQString( "Version%1" ).tqarg( j ), server.version() ); + config->writeEntry( prefix + TQString( "Security%1" ).tqarg( j ), server.security() ); + config->writeEntry( prefix + TQString( "Auth%1" ).tqarg( j ), server.auth() ); + config->writeEntry( prefix + TQString( "Mech%1" ).tqarg( j ), server.mech() ); +} + +KConfig* LdapSearch::config() +{ + if ( !s_config ) + configDeleter.setObject( s_config, new KConfig( "kabldaprc", false, false ) ); // Open read-write, no kdeglobals + + return s_config; +} + + +LdapSearch::LdapSearch() + : mActiveClients( 0 ), mNoLDAPLookup( false ) +{ + if ( !KProtocolInfo::isKnownProtocol( KURL("ldap://localhost") ) ) { + mNoLDAPLookup = true; + return; + } + + readConfig(); + connect(KDirWatch::self(), TQT_SIGNAL(dirty (const TQString&)),this, + TQT_SLOT(slotFileChanged(const TQString&))); +} + +void LdapSearch::readWeighForClient( LdapClient *client, KConfig *config, int clientNumber ) +{ + const int completionWeight = config->readNumEntry( TQString( "SelectedCompletionWeight%1" ).tqarg( clientNumber ), -1 ); + if ( completionWeight != -1 ) + client->setCompletionWeight( completionWeight ); +} + +void LdapSearch::updateCompletionWeights() +{ + KConfig *config = KPIM::LdapSearch::config(); + config->setGroup( "LDAP" ); + for ( uint i = 0; i < mClients.size(); i++ ) { + readWeighForClient( mClients[i], config, i ); + } +} + +void LdapSearch::readConfig() +{ + cancelSearch(); + TQValueList< LdapClient* >::Iterator it; + for ( it = mClients.begin(); it != mClients.end(); ++it ) + delete *it; + mClients.clear(); + + // stolen from KAddressBook + KConfig *config = KPIM::LdapSearch::config(); + config->setGroup( "LDAP" ); + int numHosts = config->readUnsignedNumEntry( "NumSelectedHosts"); + if ( !numHosts ) { + mNoLDAPLookup = true; + } else { + for ( int j = 0; j < numHosts; j++ ) { + LdapClient* ldapClient = new LdapClient( j, this ); + LdapServer server; + readConfig( server, config, j, true ); + if ( !server.host().isEmpty() ) mNoLDAPLookup = false; + ldapClient->setServer( server ); + + readWeighForClient( ldapClient, config, j ); + + TQStringList attrs; + // note: we need "objectClass" to detect distribution lists + attrs << "cn" << "mail" << "givenname" << "sn" << "objectClass"; + ldapClient->setAttrs( attrs ); + + connect( ldapClient, TQT_SIGNAL( result( const KPIM::LdapObject& ) ), + this, TQT_SLOT( slotLDAPResult( const KPIM::LdapObject& ) ) ); + connect( ldapClient, TQT_SIGNAL( done() ), + this, TQT_SLOT( slotLDAPDone() ) ); + connect( ldapClient, TQT_SIGNAL( error( const TQString& ) ), + this, TQT_SLOT( slotLDAPError( const TQString& ) ) ); + + mClients.append( ldapClient ); + } + + connect( &mDataTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotDataTimer() ) ); + } + mConfigFile = locateLocal( "config", "kabldaprc" ); + KDirWatch::self()->addFile( mConfigFile ); +} + +void LdapSearch::slotFileChanged( const TQString& file ) +{ + if ( file == mConfigFile ) + readConfig(); +} + +void LdapSearch::startSearch( const TQString& txt ) +{ + if ( mNoLDAPLookup ) + return; + + cancelSearch(); + + int pos = txt.find( '\"' ); + if( pos >= 0 ) + { + ++pos; + int pos2 = txt.find( '\"', pos ); + if( pos2 >= 0 ) + mSearchText = txt.mid( pos , pos2 - pos ); + else + mSearchText = txt.mid( pos ); + } else + mSearchText = txt; + + /* The reasoning behind this filter is: + * If it's a person, or a distlist, show it, even if it doesn't have an email address. + * If it's not a person, or a distlist, only show it if it has an email attribute. + * This allows both resource accounts with an email address which are not a person and + * person entries without an email address to show up, while still not showing things + * like structural entries in the ldap tree. */ + TQString filter = TQString( "&(|(objectclass=person)(objectclass=groupOfNames)(mail=*))(|(cn=%1*)(mail=%2*)(mail=*@%3*)(givenName=%4*)(sn=%5*))" ) + .tqarg( mSearchText ).tqarg( mSearchText ).tqarg( mSearchText ).tqarg( mSearchText ).tqarg( mSearchText ); + + TQValueList< LdapClient* >::Iterator it; + for ( it = mClients.begin(); it != mClients.end(); ++it ) { + (*it)->startQuery( filter ); + kdDebug(5300) << "LdapSearch::startSearch() " << filter << endl; + ++mActiveClients; + } +} + +void LdapSearch::cancelSearch() +{ + TQValueList< LdapClient* >::Iterator it; + for ( it = mClients.begin(); it != mClients.end(); ++it ) + (*it)->cancelQuery(); + + mActiveClients = 0; + mResults.clear(); +} + +void LdapSearch::slotLDAPResult( const KPIM::LdapObject& obj ) +{ + mResults.append( obj ); + if ( !mDataTimer.isActive() ) + mDataTimer.start( 500, true ); +} + +void LdapSearch::slotLDAPError( const TQString& ) +{ + slotLDAPDone(); +} + +void LdapSearch::slotLDAPDone() +{ + if ( --mActiveClients > 0 ) + return; + + finish(); +} + +void LdapSearch::slotDataTimer() +{ + TQStringList lst; + LdapResultList reslist; + makeSearchData( lst, reslist ); + if ( !lst.isEmpty() ) + emit searchData( lst ); + if ( !reslist.isEmpty() ) + emit searchData( reslist ); +} + +void LdapSearch::finish() +{ + mDataTimer.stop(); + + slotDataTimer(); // emit final bunch of data + emit searchDone(); +} + +void LdapSearch::makeSearchData( TQStringList& ret, LdapResultList& resList ) +{ + TQString search_text_upper = mSearchText.upper(); + + TQValueList< KPIM::LdapObject >::ConstIterator it1; + for ( it1 = mResults.begin(); it1 != mResults.end(); ++it1 ) { + TQString name, mail, givenname, sn; + TQStringList mails; + bool isDistributionList = false; + bool wasCN = false; + bool wasDC = false; + + //kdDebug(5300) << "\n\nLdapSearch::makeSearchData()\n\n" << endl; + + LdapAttrMap::ConstIterator it2; + for ( it2 = (*it1).attrs.begin(); it2 != (*it1).attrs.end(); ++it2 ) { + TQByteArray val = (*it2).first(); + int len = val.size(); + if( len > 0 && '\0' == val[len-1] ) + --len; + const TQString tmp = TQString::fromUtf8( val, len ); + //kdDebug(5300) << " key: \"" << it2.key() << "\" value: \"" << tmp << "\"" << endl; + if ( it2.key() == "cn" ) { + name = tmp; + if( mail.isEmpty() ) + mail = tmp; + else{ + if( wasCN ) + mail.prepend( "." ); + else + mail.prepend( "@" ); + mail.prepend( tmp ); + } + wasCN = true; + } else if ( it2.key() == "dc" ) { + if( mail.isEmpty() ) + mail = tmp; + else{ + if( wasDC ) + mail.append( "." ); + else + mail.append( "@" ); + mail.append( tmp ); + } + wasDC = true; + } else if( it2.key() == "mail" ) { + mail = tmp; + LdapAttrValue::ConstIterator it3 = it2.data().begin(); + for ( ; it3 != it2.data().end(); ++it3 ) { + mails.append( TQString::fromUtf8( (*it3).data(), (*it3).size() ) ); + } + } else if( it2.key() == "givenName" ) + givenname = tmp; + else if( it2.key() == "sn" ) + sn = tmp; + else if( it2.key() == "objectClass" && + (tmp == "groupOfNames" || tmp == "kolabGroupOfNames") ) { + isDistributionList = true; + } + } + + if( mails.isEmpty()) { + if ( !mail.isEmpty() ) mails.append( mail ); + if( isDistributionList ) { + //kdDebug(5300) << "\n\nLdapSearch::makeSearchData() found a list: " << name << "\n\n" << endl; + ret.append( name ); + // following lines commented out for bugfixing kolab issue #177: + // + // Unlike we thought previously we may NOT append the server name here. + // + // The right server is found by the SMTP server instead: Kolab users + // must use the correct SMTP server, by definition. + // + //mail = (*it1).client->base().simplifyWhiteSpace(); + //mail.replace( ",dc=", ".", false ); + //if( mail.startsWith("dc=", false) ) + // mail.remove(0, 3); + //mail.prepend( '@' ); + //mail.prepend( name ); + //mail = name; + } else { + //kdDebug(5300) << "LdapSearch::makeSearchData() found BAD ENTRY: \"" << name << "\"" << endl; + continue; // nothing, bad entry + } + } else if ( name.isEmpty() ) { + //kdDebug(5300) << "LdapSearch::makeSearchData() mail: \"" << mail << "\"" << endl; + ret.append( mail ); + } else { + //kdDebug(5300) << "LdapSearch::makeSearchData() name: \"" << name << "\" mail: \"" << mail << "\"" << endl; + ret.append( TQString( "%1 <%2>" ).tqarg( name ).tqarg( mail ) ); + } + + LdapResult sr; + sr.clientNumber = (*it1).client->clientNumber(); + sr.completionWeight = (*it1).client->completionWeight(); + sr.name = name; + sr.email = mails; + resList.append( sr ); + } + + mResults.clear(); +} + +bool LdapSearch::isAvailable() const +{ + return !mNoLDAPLookup; +} + + +#include "ldapclient.moc" diff --git a/libtdepim/ldapclient.h b/libtdepim/ldapclient.h new file mode 100644 index 000000000..a2a85f1e3 --- /dev/null +++ b/libtdepim/ldapclient.h @@ -0,0 +1,300 @@ +/* kldapclient.h - LDAP access + * Copyright (C) 2002 Klar�vdalens Datakonsult AB + * + * Author: Steffen Hansen <hansen@kde.org> + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This file 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + + +#ifndef KPIM_LDAPCLIENT_H +#define KPIM_LDAPCLIENT_H + + +#include <tqobject.h> +#include <tqstring.h> +#include <tqcstring.h> +#include <tqstringlist.h> +#include <tqmemarray.h> +#include <tqguardedptr.h> +#include <tqtimer.h> + +#include <kio/job.h> +#include <kabc/ldif.h> +#include <kconfig.h> + +#include <tdepimmacros.h> + +namespace KPIM { + +class LdapClient; +typedef TQValueList<TQByteArray> LdapAttrValue; +typedef TQMap<TQString,LdapAttrValue > LdapAttrMap; + +class LdapServer +{ + public: + LdapServer() + : mPort( 389 ), + mTimeLimit(0), + mSizeLimit(0), + mVersion(2), + mSecurity(Sec_None), + mAuth( LdapServer::Anonymous ) + {} + + enum Security{ Sec_None, TLS, SSL }; + enum Auth{ Anonymous, Simple, SASL }; + TQString host() const { return mHost; } + int port() const { return mPort; } + const TQString &baseDN() const { return mBaseDN; } + const TQString &user() const { return mUser; } + const TQString &bindDN() const { return mBindDN; } + const TQString &pwdBindDN() const { return mPwdBindDN; } + int timeLimit() const { return mTimeLimit; } + int sizeLimit() const { return mSizeLimit; } + int version() const { return mVersion; } + int security() const { return mSecurity; } + int auth() const { return mAuth; } + const TQString &mech() const { return mMech; } + + void setHost( const TQString &host ) { mHost = host; } + void setPort( int port ) { mPort = port; } + void setBaseDN( const TQString &baseDN ) { mBaseDN = baseDN; } + void setUser( const TQString &user ) { mUser = user; } + void setBindDN( const TQString &bindDN ) { mBindDN = bindDN; } + void setPwdBindDN( const TQString &pwdBindDN ) { mPwdBindDN = pwdBindDN; } + void setTimeLimit( int timelimit ) { mTimeLimit = timelimit; } + void setSizeLimit( int sizelimit ) { mSizeLimit = sizelimit; } + void setVersion( int version ) { mVersion = version; } + void setSecurity( int security ) { mSecurity = security; } //0-No, 1-TLS, 2-SSL - KDE4: add an enum to Lda + void setAuth( int auth ) { mAuth = auth; } //0-Anonymous, 1-simple, 2-SASL - KDE4: add an enum to LdapCon + void setMech( const TQString &mech ) { mMech = mech; } + + private: + TQString mHost; + int mPort; + TQString mBaseDN; + TQString mUser; + TQString mBindDN; + TQString mPwdBindDN; + TQString mMech; + int mTimeLimit, mSizeLimit, mVersion, mSecurity, mAuth; +}; + + +/** + * This class is internal. Binary compatibiliy might be broken any time + * without notification. Do not use it. + * + * We mean it! + * + */ +class LdapObject +{ + public: + LdapObject() + : dn( TQString() ), client( 0 ) {} + explicit LdapObject( const TQString& _dn, LdapClient* _cl ) : dn( _dn ), client( _cl ) {} + LdapObject( const LdapObject& that ) { assign( that ); } + + LdapObject& operator=( const LdapObject& that ) + { + assign( that ); + return *this; + } + + TQString toString() const; + + void clear(); + + TQString dn; + TQString objectClass; + LdapAttrMap attrs; + LdapClient* client; + + protected: + void assign( const LdapObject& that ); + + private: + //class LdapObjectPrivate* d; +}; + +/** + * This class is internal. Binary compatibility might be broken any time + * without notification. Do not use it. + * + * We mean it! + * + */ +class KDE_EXPORT LdapClient : public TQObject +{ + Q_OBJECT + TQ_OBJECT + + public: + LdapClient( int clientNumber, TQObject* parent = 0, const char* name = 0 ); + virtual ~LdapClient(); + + /*! returns true if there is a query running */ + bool isActive() const { return mActive; } + + int clientNumber() const; + int completionWeight() const; + void setCompletionWeight( int ); + + const LdapServer& server() { return mServer; } + void setServer( const LdapServer &server ) { mServer = server; } + /*! Return the attributes that should be + * returned, or an empty list if + * all attributes are wanted + */ + TQStringList attrs() const { return mAttrs; } + + signals: + /*! Emitted when the query is done */ + void done(); + + /*! Emitted in case of error */ + void error( const TQString& ); + + /*! Emitted once for each object returned + * from the query + */ + void result( const KPIM::LdapObject& ); + + public slots: // why are those slots? + /*! Set the attributes that should be + * returned, or an empty list if + * all attributes are wanted + */ + void setAttrs( const TQStringList& attrs ); + + void setScope( const TQString scope ) { mScope = scope; } + + /*! + * Start the query with filter filter + */ + void startQuery( const TQString& filter ); + + /*! + * Abort a running query + */ + void cancelQuery(); + + protected slots: + void slotData( KIO::Job*, const TQByteArray &data ); + void slotInfoMessage( KIO::Job*, const TQString &info ); + void slotDone(); + + protected: + void startParseLDIF(); + void parseLDIF( const TQByteArray& data ); + void endParseLDIF(); + void finishCurrentObject(); + + LdapServer mServer; + TQString mScope; + TQStringList mAttrs; + + TQGuardedPtr<KIO::SimpleJob> mJob; + bool mActive; + bool mReportObjectClass; + + LdapObject mCurrentObject; + + private: + KABC::LDIF mLdif; + int mClientNumber; + int mCompletionWeight; + +// class LdapClientPrivate; +// LdapClientPrivate* d; +}; + +/** + * Structure describing one result returned by a LDAP query + */ +struct LdapResult { + TQString name; ///< full name + TQStringList email; ///< emails + int clientNumber; ///< for sorting in a ldap-only lookup + int completionWeight; ///< for sorting in a completion list +}; +typedef TQValueList<LdapResult> LdapResultList; + + +/** + * This class is internal. Binary compatibiliy might be broken any time + * without notification. Do not use it. + * + * We mean it! + * + */ +class KDE_EXPORT LdapSearch : public TQObject +{ + Q_OBJECT + TQ_OBJECT + + public: + LdapSearch(); + + static KConfig *config(); + static void readConfig( LdapServer &server, KConfig *config, int num, bool active ); + static void writeConfig( const LdapServer &server, KConfig *config, int j, bool active ); + + void startSearch( const TQString& txt ); + void cancelSearch(); + bool isAvailable() const; + void updateCompletionWeights(); + + TQValueList< LdapClient* > clients() const { return mClients; } + + signals: + /// Results, assembled as "Full Name <email>" + /// (This signal can be emitted many times) + void searchData( const TQStringList& ); + /// Another form for the results, with separate fields + /// (This signal can be emitted many times) + void searchData( const KPIM::LdapResultList& ); + void searchDone(); + + private slots: + void slotLDAPResult( const KPIM::LdapObject& ); + void slotLDAPError( const TQString& ); + void slotLDAPDone(); + void slotDataTimer(); + void slotFileChanged( const TQString& ); + + private: + void readWeighForClient( LdapClient *client, KConfig *config, int clientNumber ); + void readConfig(); + void finish(); + void makeSearchData( TQStringList& ret, LdapResultList& resList ); + TQValueList< LdapClient* > mClients; + TQString mSearchText; + TQTimer mDataTimer; + int mActiveClients; + bool mNoLDAPLookup; + TQValueList< LdapObject > mResults; + TQString mConfigFile; + + private: + static KConfig *s_config; + class LdapSearchPrivate* d; +}; + +} +#endif // KPIM_LDAPCLIENT_H diff --git a/libtdepim/ldapsearchdialog.cpp b/libtdepim/ldapsearchdialog.cpp new file mode 100644 index 000000000..ed1aa2933 --- /dev/null +++ b/libtdepim/ldapsearchdialog.cpp @@ -0,0 +1,480 @@ +/* ldapsearchdialogimpl.cpp - LDAP access + * Copyright (C) 2002 Klar�vdalens Datakonsult AB + * + * Author: Steffen Hansen <hansen@kde.org> + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This file 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include "ldapsearchdialog.h" +#include "ldapclient.h" + +#include <libemailfunctions/email.h> + +#include <tqcheckbox.h> +#include <tqgroupbox.h> +#include <tqheader.h> +#include <tqlabel.h> +#include <tqlayout.h> +#include <tqlistview.h> +#include <tqpushbutton.h> + +#include <kabc/addresslineedit.h> +#include <kapplication.h> +#include <kcombobox.h> +#include <kconfig.h> +#include <klineedit.h> +#include <klocale.h> +#include <kmessagebox.h> + +using namespace KPIM; + +static TQString asUtf8( const TQByteArray &val ) +{ + if ( val.isEmpty() ) + return TQString(); + + const char *data = val.data(); + + //TQString::fromUtf8() bug workaround + if ( data[ val.size() - 1 ] == '\0' ) + return TQString::fromUtf8( data, val.size() - 1 ); + else + return TQString::fromUtf8( data, val.size() ); +} + +static TQString join( const KPIM::LdapAttrValue& lst, const TQString& sep ) +{ + TQString res; + bool alredy = false; + for ( KPIM::LdapAttrValue::ConstIterator it = lst.begin(); it != lst.end(); ++it ) { + if ( alredy ) + res += sep; + alredy = TRUE; + res += asUtf8( *it ); + } + return res; +} + +static TQMap<TQString, TQString>& adrbookattr2ldap() +{ + static TQMap<TQString, TQString> keys; + + if ( keys.isEmpty() ) { + keys[ i18n( "Title" ) ] = "title"; + keys[ i18n( "Full Name" ) ] = "cn"; + keys[ i18n( "Email" ) ] = "mail"; + keys[ i18n( "Home Number" ) ] = "homePhone"; + keys[ i18n( "Work Number" ) ] = "telephoneNumber"; + keys[ i18n( "Mobile Number" ) ] = "mobile"; + keys[ i18n( "Fax Number" ) ] = "facsimileTelephoneNumber"; + keys[ i18n( "Pager" ) ] = "pager"; + keys[ i18n( "Street") ] = "street"; + keys[ i18n( "State" ) ] = "st"; + keys[ i18n( "Country" ) ] = "co"; + keys[ i18n( "City" ) ] = "l"; + keys[ i18n( "Organization" ) ] = "o"; + keys[ i18n( "Company" ) ] = "Company"; + keys[ i18n( "Department" ) ] = "department"; + keys[ i18n( "Zip Code" ) ] = "postalCode"; + keys[ i18n( "Postal Address" ) ] = "postalAddress"; + keys[ i18n( "Description" ) ] = "description"; + keys[ i18n( "User ID" ) ] = "uid"; + } + return keys; +} + +namespace KPIM { + +class ContactListItem : public TQListViewItem +{ + public: + ContactListItem( TQListView* parent, const KPIM::LdapAttrMap& attrs ) + : TQListViewItem( parent ), mAttrs( attrs ) + { + const KPIM::LdapAttrValue &mailAttrs = attrs[ "mail" ]; + if ( mailAttrs.isEmpty() ) { + setSelectable( false ); + setEnabled( false ); + } + } + + KPIM::LdapAttrMap mAttrs; + + virtual TQString text( int col ) const + { + // Look up a suitable attribute for column col + const TQString colName = listView()->columnText( col ); + const TQString ldapAttrName = adrbookattr2ldap()[ colName ]; + return join( mAttrs[ ldapAttrName ], ", " ); + } +}; + +} + +LDAPSearchDialog::LDAPSearchDialog( TQWidget* parent, const char* name ) + : KDialogBase( Plain, i18n( "Search for Addresses in Directory" ), Help | User1 | + User2 | User3 | Cancel, Default, parent, name, false, true ) +{ + setButtonCancel( KStdGuiItem::close() ); + TQFrame *page = plainPage(); + TQVBoxLayout *topLayout = new TQVBoxLayout( page, marginHint(), spacingHint() ); + + TQGroupBox *groupBox = new TQGroupBox( i18n( "Search for Addresses in Directory" ), + page ); + groupBox->setFrameShape( TQGroupBox::Box ); + groupBox->setFrameShadow( TQGroupBox::Sunken ); + groupBox->setColumnLayout( 0, Qt::Vertical ); + TQGridLayout *boxLayout = new TQGridLayout( groupBox->tqlayout(), 2, + 5, spacingHint() ); + boxLayout->setColStretch( 1, 1 ); + + TQLabel *label = new TQLabel( i18n( "Search for:" ), groupBox ); + boxLayout->addWidget( label, 0, 0 ); + + mSearchEdit = new KLineEdit( groupBox ); + boxLayout->addWidget( mSearchEdit, 0, 1 ); + label->setBuddy( mSearchEdit ); + + label = new TQLabel( i18n( "in" ), groupBox ); + boxLayout->addWidget( label, 0, 2 ); + + mFilterCombo = new KComboBox( groupBox ); + mFilterCombo->insertItem( i18n( "Name" ) ); + mFilterCombo->insertItem( i18n( "Email" ) ); + mFilterCombo->insertItem( i18n( "Home Number" ) ); + mFilterCombo->insertItem( i18n( "Work Number" ) ); + boxLayout->addWidget( mFilterCombo, 0, 3 ); + + TQSize buttonSize; + mSearchButton = new TQPushButton( i18n( "Stop" ), groupBox ); + buttonSize = mSearchButton->tqsizeHint(); + mSearchButton->setText( i18n( "Search" ) ); + if ( buttonSize.width() < mSearchButton->tqsizeHint().width() ) + buttonSize = mSearchButton->tqsizeHint(); + mSearchButton->setFixedWidth( buttonSize.width() ); + + mSearchButton->setDefault( true ); + boxLayout->addWidget( mSearchButton, 0, 4 ); + + mRecursiveCheckbox = new TQCheckBox( i18n( "Recursive search" ), groupBox ); + mRecursiveCheckbox->setChecked( true ); + boxLayout->addMultiCellWidget( mRecursiveCheckbox, 1, 1, 0, 4 ); + + mSearchType = new KComboBox( groupBox ); + mSearchType->insertItem( i18n( "Contains" ) ); + mSearchType->insertItem( i18n( "Starts With" ) ); + boxLayout->addMultiCellWidget( mSearchType, 1, 1, 3, 4 ); + + topLayout->addWidget( groupBox ); + + mResultListView = new TQListView( page ); + mResultListView->setSelectionMode( TQListView::Multi ); + mResultListView->setAllColumnsShowFocus( true ); + mResultListView->setShowSortIndicator( true ); + topLayout->addWidget( mResultListView ); + + resize( TQSize( 600, 400).expandedTo( tqminimumSizeHint() ) ); + + setButtonText( User1, i18n( "Unselect All" ) ); + setButtonText( User2, i18n( "Select All" ) ); + setButtonText( User3, i18n( "Add Selected" ) ); + + mNumHosts = 0; + mIsOK = false; + + connect( mRecursiveCheckbox, TQT_SIGNAL( toggled( bool ) ), + this, TQT_SLOT( slotSetScope( bool ) ) ); + connect( mSearchButton, TQT_SIGNAL( clicked() ), + this, TQT_SLOT( slotStartSearch() ) ); + + setTabOrder(mSearchEdit, mFilterCombo); + setTabOrder(mFilterCombo, mSearchButton); + mSearchEdit->setFocus(); + + restoreSettings(); +} + +LDAPSearchDialog::~LDAPSearchDialog() +{ + saveSettings(); +} + +void LDAPSearchDialog::restoreSettings() +{ + // Create one KPIM::LdapClient per selected server and configure it. + + // First clean the list to make sure it is empty at + // the beginning of the process + mLdapClientList.setAutoDelete( true ); + mLdapClientList.clear(); + + KConfig kabConfig( "kaddressbookrc" ); + kabConfig.setGroup( "LDAPSearch" ); + mSearchType->setCurrentItem( kabConfig.readNumEntry( "SearchType", 0 ) ); + + // then read the config file and register all selected + // server in the list + KConfig* config = KABC::AddressLineEdit::config(); // singleton kabldaprc config object + KConfigGroupSaver saver( config, "LDAP" ); + mNumHosts = config->readUnsignedNumEntry( "NumSelectedHosts" ); + if ( !mNumHosts ) { + KMessageBox::error( this, i18n( "You must select a LDAP server before searching.\nYou can do this from the menu Settings/Configure KAddressBook." ) ); + mIsOK = false; + } else { + mIsOK = true; + for ( int j = 0; j < mNumHosts; ++j ) { + KPIM::LdapServer ldapServer; + + TQString host = config->readEntry( TQString( "SelectedHost%1" ).tqarg( j ), "" ); + if ( !host.isEmpty() ) + ldapServer.setHost( host ); + + int port = config->readUnsignedNumEntry( TQString( "SelectedPort%1" ).tqarg( j ) ); + if ( port ) + ldapServer.setPort( port ); + + TQString base = config->readEntry( TQString( "SelectedBase%1" ).tqarg( j ), "" ); + if ( !base.isEmpty() ) + ldapServer.setBaseDN( base ); + + TQString bindDN = config->readEntry( TQString( "SelectedBind%1" ).tqarg( j ), "" ); + if ( !bindDN.isEmpty() ) + ldapServer.setBindDN( bindDN ); + + TQString pwdBindDN = config->readEntry( TQString( "SelectedPwdBind%1" ).tqarg( j ), "" ); + if ( !pwdBindDN.isEmpty() ) + ldapServer.setPwdBindDN( pwdBindDN ); + + KPIM::LdapClient* ldapClient = new KPIM::LdapClient( 0, TQT_TQOBJECT(this), "ldapclient" ); + ldapClient->setServer( ldapServer ); + + TQStringList attrs; + + for ( TQMap<TQString,TQString>::Iterator it = adrbookattr2ldap().begin(); it != adrbookattr2ldap().end(); ++it ) + attrs << *it; + + ldapClient->setAttrs( attrs ); + + connect( ldapClient, TQT_SIGNAL( result( const KPIM::LdapObject& ) ), + this, TQT_SLOT( slotAddResult( const KPIM::LdapObject& ) ) ); + connect( ldapClient, TQT_SIGNAL( done() ), + this, TQT_SLOT( slotSearchDone() ) ); + connect( ldapClient, TQT_SIGNAL( error( const TQString& ) ), + this, TQT_SLOT( slotError( const TQString& ) ) ); + + mLdapClientList.append( ldapClient ); + } + +/** CHECKIT*/ + while ( mResultListView->header()->count() > 0 ) { + mResultListView->removeColumn(0); + } + + mResultListView->addColumn( i18n( "Full Name" ) ); + mResultListView->addColumn( i18n( "Email" ) ); + mResultListView->addColumn( i18n( "Home Number" ) ); + mResultListView->addColumn( i18n( "Work Number" ) ); + mResultListView->addColumn( i18n( "Mobile Number" ) ); + mResultListView->addColumn( i18n( "Fax Number" ) ); + mResultListView->addColumn( i18n( "Company" ) ); + mResultListView->addColumn( i18n( "Organization" ) ); + mResultListView->addColumn( i18n( "Street" ) ); + mResultListView->addColumn( i18n( "State" ) ); + mResultListView->addColumn( i18n( "Country" ) ); + mResultListView->addColumn( i18n( "Zip Code" ) ); + mResultListView->addColumn( i18n( "Postal Address" ) ); + mResultListView->addColumn( i18n( "City" ) ); + mResultListView->addColumn( i18n( "Department" ) ); + mResultListView->addColumn( i18n( "Description" ) ); + mResultListView->addColumn( i18n( "User ID" ) ); + mResultListView->addColumn( i18n( "Title" ) ); + + mResultListView->clear(); + } +} + +void LDAPSearchDialog::saveSettings() +{ + KConfig config( "kaddressbookrc" ); + config.setGroup( "LDAPSearch" ); + config.writeEntry( "SearchType", mSearchType->currentItem() ); + config.sync(); +} + +void LDAPSearchDialog::cancelQuery() +{ + for ( KPIM::LdapClient* client = mLdapClientList.first(); client; client = mLdapClientList.next() ) { + client->cancelQuery(); + } +} + +void LDAPSearchDialog::slotAddResult( const KPIM::LdapObject& obj ) +{ + new ContactListItem( mResultListView, obj.attrs ); +} + +void LDAPSearchDialog::slotSetScope( bool rec ) +{ + for ( KPIM::LdapClient* client = mLdapClientList.first(); client; client = mLdapClientList.next() ) { + if ( rec ) + client->setScope( "sub" ); + else + client->setScope( "one" ); + } +} + +TQString LDAPSearchDialog::makeFilter( const TQString& query, const TQString& attr, + bool startsWith ) +{ + /* The reasoning behind this filter is: + * If it's a person, or a distlist, show it, even if it doesn't have an email address. + * If it's not a person, or a distlist, only show it if it has an email attribute. + * This allows both resource accounts with an email address which are not a person and + * person entries without an email address to show up, while still not showing things + * like structural entries in the ldap tree. */ + TQString result( "&(|(objectclass=person)(objectclass=groupofnames)(mail=*))(" ); + if( query.isEmpty() ) + // Return a filter that matches everything + return result + "|(cn=*)(sn=*)" + ")"; + + if ( attr == i18n( "Name" ) ) { + result += startsWith ? "|(cn=%1*)(sn=%2*)" : "|(cn=*%1*)(sn=*%2*)"; + result = result.tqarg( query ).tqarg( query ); + } else { + result += (startsWith ? "%1=%2*" : "%1=*%2*"); + if ( attr == i18n( "Email" ) ) { + result = result.tqarg( "mail" ).tqarg( query ); + } else if ( attr == i18n( "Home Number" ) ) { + result = result.tqarg( "homePhone" ).tqarg( query ); + } else if ( attr == i18n( "Work Number" ) ) { + result = result.tqarg( "telephoneNumber" ).tqarg( query ); + } else { + // Error? + result = TQString(); + return result; + } + } + result += ")"; + return result; +} + +void LDAPSearchDialog::slotStartSearch() +{ + cancelQuery(); + + TQApplication::setOverrideCursor( TQt::waitCursor ); + mSearchButton->setText( i18n( "Stop" ) ); + + disconnect( mSearchButton, TQT_SIGNAL( clicked() ), + this, TQT_SLOT( slotStartSearch() ) ); + connect( mSearchButton, TQT_SIGNAL( clicked() ), + this, TQT_SLOT( slotStopSearch() ) ); + + bool startsWith = (mSearchType->currentItem() == 1); + + TQString filter = makeFilter( mSearchEdit->text().stripWhiteSpace(), mFilterCombo->currentText(), startsWith ); + + // loop in the list and run the KPIM::LdapClients + mResultListView->clear(); + for( KPIM::LdapClient* client = mLdapClientList.first(); client; client = mLdapClientList.next() ) { + client->startQuery( filter ); + } + + saveSettings(); +} + +void LDAPSearchDialog::slotStopSearch() +{ + cancelQuery(); + slotSearchDone(); +} + +void LDAPSearchDialog::slotSearchDone() +{ + // If there are no more active clients, we are done. + for ( KPIM::LdapClient* client = mLdapClientList.first(); client; client = mLdapClientList.next() ) { + if ( client->isActive() ) + return; + } + + disconnect( mSearchButton, TQT_SIGNAL( clicked() ), + this, TQT_SLOT( slotStopSearch() ) ); + connect( mSearchButton, TQT_SIGNAL( clicked() ), + this, TQT_SLOT( slotStartSearch() ) ); + + mSearchButton->setText( i18n( "Search" ) ); + TQApplication::restoreOverrideCursor(); +} + +void LDAPSearchDialog::slotError( const TQString& error ) +{ + TQApplication::restoreOverrideCursor(); + KMessageBox::error( this, error ); +} + +void LDAPSearchDialog::closeEvent( TQCloseEvent* e ) +{ + slotStopSearch(); + e->accept(); +} + +/*! + * Returns a ", " separated list of email addresses that were + * checked by the user + */ +TQString LDAPSearchDialog::selectedEMails() const +{ + TQStringList result; + ContactListItem* cli = static_cast<ContactListItem*>( mResultListView->firstChild() ); + while ( cli ) { + if ( cli->isSelected() ) { + TQString email = asUtf8( cli->mAttrs[ "mail" ].first() ).stripWhiteSpace(); + if ( !email.isEmpty() ) { + TQString name = asUtf8( cli->mAttrs[ "cn" ].first() ).stripWhiteSpace(); + if ( name.isEmpty() ) { + result << email; + } else { + result << KPIM::quoteNameIfNecessary( name ) + " <" + email + ">"; + } + } + } + cli = static_cast<ContactListItem*>( cli->nextSibling() ); + } + + return result.join( ", " ); +} + +void LDAPSearchDialog::slotHelp() +{ + kapp->invokeHelp( "ldap-queries" ); +} + +void LDAPSearchDialog::slotUser1() +{ + mResultListView->selectAll( false ); +} + +void LDAPSearchDialog::slotUser2() +{ + mResultListView->selectAll( true ); +} + +void LDAPSearchDialog::slotUser3() +{ + emit addresseesAdded(); +} + +#include "ldapsearchdialog.moc" diff --git a/libtdepim/ldapsearchdialog.h b/libtdepim/ldapsearchdialog.h new file mode 100644 index 000000000..45e174efe --- /dev/null +++ b/libtdepim/ldapsearchdialog.h @@ -0,0 +1,93 @@ +/* ldapsearchdialogimpl.h - LDAP access + * Copyright (C) 2002 Klar�vdalens Datakonsult AB + * + * Author: Steffen Hansen <hansen@kde.org> + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This file 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef LDAPSEARCHDIALOG_H +#define LDAPSEARCHDIALOG_H + +#include <tqptrlist.h> + +#include <ldapclient.h> +#include <kdialogbase.h> +#include <klineedit.h> + +class KComboBox; + +class TQCheckBox; +class TQListView; +class TQPushButton; + +namespace KPIM { + +class LDAPSearchDialog : public KDialogBase +{ + Q_OBJECT + TQ_OBJECT + + public: + LDAPSearchDialog( TQWidget* parent, const char* name = 0 ); + ~LDAPSearchDialog(); + + bool isOK() const { return mIsOK; } + + void restoreSettings(); + + void setSearchText( const TQString &text ) { mSearchEdit->setText( text ); } + TQString selectedEMails() const; + signals: + void addresseesAdded(); + + protected slots: + void slotAddResult( const KPIM::LdapObject& obj ); + void slotSetScope( bool rec ); + void slotStartSearch(); + void slotStopSearch(); + void slotSearchDone(); + void slotError( const TQString& ); + virtual void slotHelp(); + virtual void slotUser1(); + virtual void slotUser2(); + virtual void slotUser3(); + + protected: + + virtual void closeEvent( TQCloseEvent* ); + + private: + void saveSettings(); + + TQString makeFilter( const TQString& query, const TQString& attr, bool startsWith ); + + void cancelQuery(); + + int mNumHosts; + TQPtrList<KPIM::LdapClient> mLdapClientList; + bool mIsOK; + KComboBox* mFilterCombo; + KComboBox* mSearchType; + KLineEdit* mSearchEdit; + + TQCheckBox* mRecursiveCheckbox; + TQListView* mResultListView; + TQPushButton* mSearchButton; +}; + + +} +#endif diff --git a/libtdepim/linklocator.cpp b/libtdepim/linklocator.cpp new file mode 100644 index 000000000..399c5d3b3 --- /dev/null +++ b/libtdepim/linklocator.cpp @@ -0,0 +1,466 @@ +/** + * linklocator.cpp + * + * Copyright (c) 2002 Dave Corrie <kde@davecorrie.com> + * + * This file is part of KMail. + * + * KMail is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "linklocator.h" +#include "pimemoticons.h" +#include <kglobal.h> +#include <kstandarddirs.h> +#include <kstaticdeleter.h> +#include <kmdcodec.h> +#include <kdebug.h> + +#include <tqstylesheet.h> +#include <tqfile.h> +#include <tqregexp.h> + +#include <limits.h> + +TQMap<TQString, TQString> *LinkLocator::s_smileyEmoticonNameMap = 0; +TQMap<TQString, TQString> *LinkLocator::s_smileyEmoticonHTMLCache = 0; + +static KStaticDeleter< TQMap<TQString, TQString> > smileyMapDeleter; +static KStaticDeleter< TQMap<TQString, TQString> > smileyCacheDeleter; + +LinkLocator::LinkLocator(const TQString& text, int pos) + : mText(text), mPos(pos), mMaxUrlLen(4096), mMaxAddressLen(255) +{ + // If you change either of the above values for maxUrlLen or + // maxAddressLen, then please also update the documentation for + // setMaxUrlLen()/setMaxAddressLen() in the header file AND the + // default values used for the maxUrlLen/maxAddressLen parameters + // of convertToHtml(). + + if ( !s_smileyEmoticonNameMap ) { + smileyMapDeleter.setObject( s_smileyEmoticonNameMap, + new TQMap<TQString, TQString>() ); + for ( int i = 0; i < EmotIcons::EnumSindex::COUNT; ++i ) { + TQString imageName( EmotIcons::EnumSindex::enumToString[i] ); + imageName.truncate( imageName.length() - 2 ); //remove the _0 bit + s_smileyEmoticonNameMap->insert( EmotIcons::smiley(i), imageName ); + } + } + + if ( !s_smileyEmoticonHTMLCache ) + smileyCacheDeleter.setObject( s_smileyEmoticonHTMLCache, + new TQMap<TQString, TQString>() ); +} + +void LinkLocator::setMaxUrlLen(int length) +{ + mMaxUrlLen = length; +} + +int LinkLocator::maxUrlLen() const +{ + return mMaxUrlLen; +} + +void LinkLocator::setMaxAddressLen(int length) +{ + mMaxAddressLen = length; +} + +int LinkLocator::maxAddressLen() const +{ + return mMaxAddressLen; +} + +TQString LinkLocator::getUrl() +{ + TQString url; + if(atUrl()) + { + // handle cases like this: <link>http://foobar.org/</link> + int start = mPos; + while(mPos < (int)mText.length() && mText[mPos] > ' ' && mText[mPos] != '"' && + TQString("<>()[]").find(mText[mPos]) == -1) + { + ++mPos; + } + /* some URLs really end with: # / & - _ */ + const TQString allowedSpecialChars = TQString("#/&-_"); + while(mPos > start && mText[mPos-1].isPunct() && + allowedSpecialChars.find(mText[mPos-1]) == -1 ) + { + --mPos; + } + + url = mText.mid(start, mPos - start); + if(isEmptyUrl(url) || mPos - start > maxUrlLen()) + { + mPos = start; + url = ""; + } + else + { + --mPos; + } + } + return url; +} + +// keep this in sync with KMMainWin::slotUrlClicked() +bool LinkLocator::atUrl() const +{ + // the following characters are allowed in a dot-atom (RFC 2822): + // a-z A-Z 0-9 . ! # $ % & ' * + - / = ? ^ _ ` { | } ~ + const TQString allowedSpecialChars = TQString(".!#$%&'*+-/=?^_`{|}~"); + + // the character directly before the URL must not be a letter, a number or + // any other character allowed in a dot-atom (RFC 2822). + if( ( mPos > 0 ) && ( mText[mPos-1].isLetterOrNumber() || + ( allowedSpecialChars.find( mText[mPos-1] ) != -1 ) ) ) + return false; + + TQChar ch = mText[mPos]; + return (ch=='h' && ( mText.mid(mPos, 7) == "http://" || + mText.mid(mPos, 8) == "https://") ) || + (ch=='v' && mText.mid(mPos, 6) == "vnc://") || + (ch=='f' && ( mText.mid(mPos, 7) == "fish://" || + mText.mid(mPos, 6) == "ftp://" || + mText.mid(mPos, 7) == "ftps://") ) || + (ch=='s' && ( mText.mid(mPos, 7) == "sftp://" || + mText.mid(mPos, 6) == "smb://") ) || + (ch=='m' && mText.mid(mPos, 7) == "mailto:") || + (ch=='w' && mText.mid(mPos, 4) == "www.") || + (ch=='f' && mText.mid(mPos, 4) == "ftp.") || + (ch=='n' && mText.mid(mPos, 5) == "news:"); + // note: no "file:" for security reasons +} + +bool LinkLocator::isEmptyUrl(const TQString& url) +{ + return url.isEmpty() || + url == "http://" || + url == "https://" || + url == "fish://" || + url == "ftp://" || + url == "ftps://" || + url == "sftp://" || + url == "smb://" || + url == "vnc://" || + url == "mailto" || + url == "www" || + url == "ftp" || + url == "news" || + url == "news://"; +} + +TQString LinkLocator::getEmailAddress() +{ + TQString address; + + if ( mText[mPos] == '@' ) { + // the following characters are allowed in a dot-atom (RFC 2822): + // a-z A-Z 0-9 . ! # $ % & ' * + - / = ? ^ _ ` { | } ~ + const TQString allowedSpecialChars = TQString(".!#$%&'*+-/=?^_`{|}~"); + + // determine the local part of the email address + int start = mPos - 1; + while ( start >= 0 && mText[start].tqunicode() < 128 && + ( mText[start].isLetterOrNumber() || + mText[start] == '@' || // allow @ to find invalid email addresses + allowedSpecialChars.find( mText[start] ) != -1 ) ) { + if ( mText[start] == '@' ) + return TQString(); // local part contains '@' -> no email address + --start; + } + ++start; + // we assume that an email address starts with a letter or a digit + while ( ( start < mPos ) && !mText[start].isLetterOrNumber() ) + ++start; + if ( start == mPos ) + return TQString(); // local part is empty -> no email address + + // determine the domain part of the email address + int dotPos = INT_MAX; + int end = mPos + 1; + while ( end < (int)mText.length() && + ( mText[end].isLetterOrNumber() || + mText[end] == '@' || // allow @ to find invalid email addresses + mText[end] == '.' || + mText[end] == '-' ) ) { + if ( mText[end] == '@' ) + return TQString(); // domain part contains '@' -> no email address + if ( mText[end] == '.' ) + dotPos = TQMIN( dotPos, end ); // remember index of first dot in domain + ++end; + } + // we assume that an email address ends with a letter or a digit + while ( ( end > mPos ) && !mText[end - 1].isLetterOrNumber() ) + --end; + if ( end == mPos ) + return TQString(); // domain part is empty -> no email address + if ( dotPos >= end ) + return TQString(); // domain part doesn't contain a dot + + if ( end - start > maxAddressLen() ) + return TQString(); // too long -> most likely no email address + address = mText.mid( start, end - start ); + + mPos = end - 1; + } + return address; +} + +TQString LinkLocator::convertToHtml(const TQString& plainText, int flags, + int maxUrlLen, int maxAddressLen) +{ + LinkLocator locator(plainText); + locator.setMaxUrlLen(maxUrlLen); + locator.setMaxAddressLen(maxAddressLen); + + TQString str; + TQString result((TQChar*)0, (int)locator.mText.length() * 2); + TQChar ch; + int x; + bool startOfLine = true; + TQString emoticon; + + for (locator.mPos = 0, x = 0; locator.mPos < (int)locator.mText.length(); locator.mPos++, x++) + { + ch = locator.mText[locator.mPos]; + if ( flags & PreserveSpaces ) + { + if (ch==' ') + { + if (startOfLine) { + result += " "; + locator.mPos++, x++; + startOfLine = false; + } + while (locator.mText[locator.mPos] == ' ') + { + result += " "; + locator.mPos++, x++; + if (locator.mText[locator.mPos] == ' ') { + result += " "; + locator.mPos++, x++; + } + } + locator.mPos--, x--; + continue; + } + else if (ch=='\t') + { + do + { + result += " "; + x++; + } + while((x&7) != 0); + x--; + startOfLine = false; + continue; + } + } + if (ch=='\n') + { + result += "<br />"; + startOfLine = true; + x = -1; + continue; + } + + startOfLine = false; + if (ch=='&') + result += "&"; + else if (ch=='"') + result += """; + else if (ch=='<') + result += "<"; + else if (ch=='>') + result += ">"; + else + { + const int start = locator.mPos; + if ( !(flags & IgnoreUrls) ) { + str = locator.getUrl(); + if (!str.isEmpty()) + { + TQString hyperlink; + if(str.left(4) == "www.") + hyperlink = "http://" + str; + else if(str.left(4) == "ftp.") + hyperlink = "ftp://" + str; + else + hyperlink = str; + + str = str.replace('&', "&"); + result += "<a href=\"" + hyperlink + "\">" + str + "</a>"; + x += locator.mPos - start; + continue; + } + str = locator.getEmailAddress(); + if(!str.isEmpty()) + { + // len is the length of the local part + int len = str.find('@'); + TQString localPart = str.left(len); + + // remove the local part from the result (as '&'s have been expanded to + // & we have to take care of the 4 additional characters per '&') + result.truncate(result.length() - len - (localPart.contains('&')*4)); + x -= len; + + result += "<a href=\"mailto:" + str + "\">" + str + "</a>"; + x += str.length() - 1; + continue; + } + } + if ( flags & ReplaceSmileys ) { + str = locator.getEmoticon(); + if ( ! str.isEmpty() ) { + result += str; + x += locator.mPos - start; + continue; + } + } + if ( flags & HighlightText ) { + str = locator.highlightedText(); + if ( !str.isEmpty() ) { + result += str; + x += locator.mPos - start; + continue; + } + } + result += ch; + } + } + + return result; +} + +TQString LinkLocator::pngToDataUrl( const TQString & iconPath ) +{ + if ( iconPath.isEmpty() ) + return TQString(); + + TQFile pngFile( iconPath ); + if ( !pngFile.open( IO_ReadOnly | IO_Raw ) ) + return TQString(); + + TQByteArray ba = pngFile.readAll(); + pngFile.close(); + return TQString::tqfromLatin1("data:image/png;base64,%1") + .tqarg( KCodecs::base64Encode( ba ).data() ); +} + + +TQString LinkLocator::getEmoticon() +{ + // smileys have to be prepended by whitespace + if ( ( mPos > 0 ) && !mText[mPos-1].isSpace() ) + return TQString(); + + // since smileys start with ':', ';', '(' or '8' short circuit method + const TQChar ch = mText[mPos]; + if ( ch !=':' && ch != ';' && ch != '(' && ch != '8' ) + return TQString(); + + // find the end of the smiley (a smiley is at most 4 chars long and ends at + // lineend or whitespace) + const int MinSmileyLen = 2; + const int MaxSmileyLen = 4; + int smileyLen = 1; + while ( ( smileyLen <= MaxSmileyLen ) && + ( mPos+smileyLen < (int)mText.length() ) && + !mText[mPos+smileyLen].isSpace() ) + smileyLen++; + if ( smileyLen < MinSmileyLen || smileyLen > MaxSmileyLen ) + return TQString(); + + const TQString smiley = mText.mid( mPos, smileyLen ); + if ( !s_smileyEmoticonNameMap->contains( smiley ) ) + return TQString(); // that's not a (known) smiley + + TQString htmlRep; + if ( s_smileyEmoticonHTMLCache->contains( smiley ) ) { + htmlRep = (*s_smileyEmoticonHTMLCache)[smiley]; + } + else { + const TQString imageName = (*s_smileyEmoticonNameMap)[smiley]; + +#if KDE_IS_VERSION( 3, 3, 91 ) + const TQString iconPath = locate( "emoticons", + EmotIcons::theme() + + TQString::tqfromLatin1( "/" ) + + imageName + TQString::tqfromLatin1(".png") ); +#else + const TQString iconPath = locate( "data", + TQString::tqfromLatin1( "kopete/pics/emoticons/" )+ + EmotIcons::theme() + + TQString::tqfromLatin1( "/" ) + + imageName + TQString::tqfromLatin1(".png") ); +#endif + + const TQString dataUrl = pngToDataUrl( iconPath ); + if ( dataUrl.isEmpty() ) { + htmlRep = TQString(); + } + else { + // create an image tag (the text in attribute alt is used + // for copy & paste) representing the smiley + htmlRep = TQString("<img class=\"pimsmileyimg\" src=\"%1\" " + "alt=\"%2\" title=\"%3\" width=\"16\" height=\"16\"/>") + .tqarg( dataUrl, + TQStyleSheet::escape( smiley ), + TQStyleSheet::escape( smiley ) ); + } + s_smileyEmoticonHTMLCache->insert( smiley, htmlRep ); + } + + if ( !htmlRep.isEmpty() ) + mPos += smileyLen - 1; + + return htmlRep; +} + +TQString LinkLocator::highlightedText() +{ + // formating symbols must be prepended with a whitespace + if ( ( mPos > 0 ) && !mText[mPos-1].isSpace() ) + return TQString(); + + const TQChar ch = mText[mPos]; + if ( ch != '/' && ch != '*' && ch != '_' ) + return TQString(); + + TQRegExp re = TQRegExp( TQString("\\%1([0-9A-Za-z]+)\\%2").tqarg( ch ).tqarg( ch ) ); + if ( re.search( mText, mPos ) == mPos ) { + uint length = re.matchedLength(); + // there must be a whitespace after the closing formating symbol + if ( mPos + length < mText.length() && !mText[mPos + length].isSpace() ) + return TQString(); + mPos += length - 1; + switch ( ch.latin1() ) { + case '*': + return "<b>" + re.cap( 1 ) + "</b>"; + case '_': + return "<u>" + re.cap( 1 ) + "</u>"; + case '/': + return "<i>" + re.cap( 1 ) + "</i>"; + } + } + return TQString(); +} + diff --git a/libtdepim/linklocator.h b/libtdepim/linklocator.h new file mode 100644 index 000000000..4597d836b --- /dev/null +++ b/libtdepim/linklocator.h @@ -0,0 +1,190 @@ +/* + * linklocator.h + * + * Copyright (c) 2002 Dave Corrie <kde@davecorrie.com> + * + * This file is part of KMail. + * + * KMail is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef LINKLOCATOR_H_INCLUDED +#define LINKLOCATOR_H_INCLUDED + +#include <tqstring.h> +#include <tqmap.h> + +#include <tdepimmacros.h> + +/** + * LinkLocator assists in identifying sections of text that can + * usefully be converted in hyperlinks in html. It is intended + * to be used in two ways: either by calling @ref convertToHtml() + * to convert a plaintext string into html, or to be derived from + * where more control is needed. + * + * @short Identifies URLs and email addresses embedded in plaintext. + * @author Dave Corrie <kde@davecorrie.com> + */ +class KDE_EXPORT LinkLocator +{ +public: + /** + * Constructs a LinkLocator that will search a plaintext string + * from a given starting point. + * + * @param text The string in which to search. + * @param pos An index into 'text' from where the search + * should begin. + */ + LinkLocator(const TQString& text, int pos = 0); + + /** + * Sets the maximum length of URLs that will be matched by + * @ref getUrl(). By default, this is set to 4096 + * characters. The reason for this limit is that there may + * be possible security implications in handling URLs of + * unlimited length. + * + * @param length The new maximum length of URLs that will be + * matched by @ref getUrl(). + */ + void setMaxUrlLen(int length); + + /** + * @return The current limit on the maximum length of a URL. + * + * @see setMaxUrlLen(). + */ + int maxUrlLen() const; + + /** + * Sets the maximum length of email addresses that will be + * matched by @ref getEmailAddress(). By default, this is + * set to 255 characters. The reason for this limit is that + * there may be possible security implications in handling + * addresses of unlimited length. + * + * @param length The new maximum length of email addresses + * that will be matched by @ref getEmailAddress(). + */ + void setMaxAddressLen(int length); + + /** + * @return The current limit on the maximum length of an email + * address. + * + * @see setMaxAddressLen(). + */ + int maxAddressLen() const; + + /** + * Attempts to grab a URL starting at the current scan position. + * If there is no URL at the current scan position, then an empty + * string is returned. If a URL is found, the current scan position + * is set to the index of the last character in the URL. + * + * @return The URL at the current scan position, or an empty string. + */ + TQString getUrl(); + + /** + * Attempts to grab an email address. If there is an @ symbol at the + * current scan position, then the text will be searched both backwards + * and forwards to find the email address. If there is no @ symbol at + * the current scan position, an empty string is returned. If an address + * is found, then the current scan position is set to the index of the + * last character in the address. + * + * @return The email address at the current scan position, or an empty + * string. + */ + TQString getEmailAddress(); + + /** + * Converts plaintext into html. The following characters are converted to HTML + * entities: & " < >. Newlines are also preserved. + * + * @param plainText The text to be converted into HTML. + * @param flags The flags to consider when processing plainText. + * Currently supported flags are: + * - PreserveSpaces, preserves the appearance of + * sequences of space and tab + * characters in the resulting HTML. + * - ReplaceSmileys, replace text smileys with + * emoticon images. + * - IgnoreUrls, doesn't parse any URLs. + * - HighlightText, interprets text highlighting markup + * like *bold*, _underlined_ and + * /italic/. + * @param maxUrlLen The maximum length of permitted URLs. (See + * @ref maxUrlLen().) + * @param maxAddressLen The maximum length of permitted email addresses. + * (See @ref maxAddressLen().) + * @return An HTML version of the text supplied in the 'plainText' parameter, + * suitable for inclusion in the BODY of an HTML document. + */ + static TQString convertToHtml(const TQString& plainText, int flags = 0, + int maxUrlLen = 4096, int maxAddressLen = 255); + + static const int PreserveSpaces = 0x01; + static const int ReplaceSmileys = 0x02; + static const int IgnoreUrls = 0x04; + static const int HighlightText = 0x08; + + /** Embed the given PNG image into a data URL. + * @param iconPath path to the PNG image + * @return A data URL, TQString() if the image could not be read. + */ + static TQString pngToDataUrl( const TQString & iconPath ); + +protected: + /** + * The plaintext string being scanned for URLs and email addresses. + */ + TQString mText; + /** + * The current scan position. + */ + int mPos; + +private: + bool atUrl() const; + bool isEmptyUrl(const TQString& url); + + /** + * Replaces smiley text with an <img> tag containing the relevant image. + * For an emoticon text to be recognised it has to match + * "(^|\s+)text(\s+|$)" + * @return An HTML String with <img> for an emoticon + */ + TQString getEmoticon(); + /** + * Highlight text according to *bold*, /italic/ and _underlined_ markup. + * @return A HTML string. + */ + TQString highlightedText(); + + int mMaxUrlLen; + int mMaxAddressLen; + + // maps the smiley text to the corresponding emoticon name + static TQMap<TQString, TQString> *s_smileyEmoticonNameMap; + // cache for the HTML representation of a smiley + static TQMap<TQString, TQString> *s_smileyEmoticonHTMLCache; +}; + +#endif // LINKLOCATOR_H_INCLUDED + diff --git a/libtdepim/maillistdrag.cpp b/libtdepim/maillistdrag.cpp new file mode 100644 index 000000000..a9e2e9fa0 --- /dev/null +++ b/libtdepim/maillistdrag.cpp @@ -0,0 +1,259 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2003 Don Sanders <sanders@kde.org> + Copyright (c) 2005 George Staikos <staikos@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "maillistdrag.h" +#include <tqbuffer.h> +#include <tqdatastream.h> +#include <tqeventloop.h> +#include <kapplication.h> +#include <klocale.h> +#include <kprogress.h> + +using namespace KPIM; + +MailSummary::MailSummary( TQ_UINT32 serialNumber, TQString messageId, + TQString subject, TQString from, TQString to, + time_t date ) + : mSerialNumber( serialNumber ), mMessageId( messageId ), + mSubject( subject ), mFrom( from ), mTo( to ), mDate( date ) +{} + +TQ_UINT32 MailSummary::serialNumber() const +{ + return mSerialNumber; +} + +TQString MailSummary::messageId() +{ + return mMessageId; +} + +TQString MailSummary::subject() +{ + return mSubject; +} + +TQString MailSummary::from() +{ + return mFrom; +} + +TQString MailSummary::to() +{ + return mTo; +} + +time_t MailSummary::date() +{ + return mDate; +} + +void MailSummary::set( TQ_UINT32 serialNumber, TQString messageId, + TQString subject, TQString from, TQString to, time_t date ) +{ + mSerialNumber = serialNumber; + mMessageId = messageId; + mSubject = subject; + mFrom = from; + mTo = to; + mDate = date; +} + +MailListDrag::MailListDrag( MailList mailList, TQWidget * parent, MailTextSource *src ) + : TQStoredDrag( MailListDrag::format(), parent ), _src(src) +{ + setMailList( mailList ); +} + +MailListDrag::~MailListDrag() +{ + delete _src; + _src = 0; +} + +const char* MailListDrag::format() +{ + return "x-kmail-drag/message-list"; +} + +bool MailListDrag::canDecode( TQMimeSource *e ) +{ + return e->provides( MailListDrag::format() ); +} + +// Have to define before use +TQDataStream& operator<< ( TQDataStream &s, MailSummary &d ) +{ + s << d.serialNumber(); + s << d.messageId(); + s << d.subject(); + s << d.from(); + s << d.to(); + s << d.date(); + return s; +} + +TQDataStream& operator>> ( TQDataStream &s, MailSummary &d ) +{ + TQ_UINT32 serialNumber; + TQString messageId, subject, from, to; + time_t date; + s >> serialNumber; + s >> messageId; + s >> subject; + s >> from; + s >> to; + s >> date; + d.set( serialNumber, messageId, subject, from, to, date ); + return s; +} + +TQDataStream& operator<< ( TQDataStream &s, MailList &mailList ) +{ + MailList::iterator it; + for (it = mailList.begin(); it != mailList.end(); ++it) { + MailSummary mailDrag = *it; + s << mailDrag; + } + return s; +} + +TQDataStream& operator>> ( TQDataStream &s, MailList &mailList ) +{ + mailList.clear(); + MailSummary mailDrag; + while (!s.atEnd()) { + s >> mailDrag; + mailList.append( mailDrag ); + } + return s; +} + +bool MailListDrag::decode( TQDropEvent* e, MailList& mailList ) +{ + TQByteArray payload = e->tqencodedData( MailListDrag::format() ); + TQDataStream buffer( payload, IO_ReadOnly ); + if ( payload.size() ) { + e->accept(); + buffer >> mailList; + return TRUE; + } + return FALSE; +} + +bool MailListDrag::decode( TQByteArray& payload, MailList& mailList ) +{ + TQDataStream stream( payload, IO_ReadOnly ); + if ( payload.size() ) { + stream >> mailList; + return TRUE; + } + return FALSE; +} + +bool MailListDrag::decode( TQDropEvent* e, TQByteArray &a ) +{ + MailList mailList; + if (decode( e, mailList )) { + MailList::iterator it; + TQBuffer buffer( a ); + buffer.open( IO_WriteOnly ); + TQDataStream stream( &buffer ); + for (it = mailList.begin(); it != mailList.end(); ++it) { + MailSummary mailDrag = *it; + stream << mailDrag.serialNumber(); + } + buffer.close(); + return TRUE; + } + return FALSE; +} + +void MailListDrag::setMailList( MailList mailList ) +{ + TQByteArray array; + TQBuffer buffer( array ); + buffer.open( IO_WriteOnly); + TQDataStream stream( array, IO_WriteOnly ); + stream << mailList; + buffer.close(); + setEncodedData( array ); +} + +const char *MailListDrag::format(int i) const +{ + if (_src) { + if (i == 0) { + return "message/rfc822"; + } else { + return TQStoredDrag::format(i - 1); + } + } + + return TQStoredDrag::format(i); +} + +bool MailListDrag::provides(const char *mimeType) const +{ + if (_src && TQCString(mimeType) == "message/rfc822") { + return true; + } + + return TQStoredDrag::provides(mimeType); +} + +TQByteArray MailListDrag::tqencodedData(const char *mimeType) const +{ + if (TQCString(mimeType) != "message/rfc822") { + return TQStoredDrag::tqencodedData(mimeType); + } + + TQByteArray rc; + if (_src) { + MailList ml; + TQByteArray enc = TQStoredDrag::tqencodedData(format()); + decode(enc, ml); + + KProgressDialog *dlg = new KProgressDialog(0, 0, TQString(), i18n("Retrieving and storing messages..."), true); + dlg->setAllowCancel(true); + dlg->progressBar()->setTotalSteps(ml.count()); + int i = 0; + dlg->progressBar()->setValue(i); + dlg->show(); + + TQTextStream *ts = new TQTextStream(rc, IO_WriteOnly); + for (MailList::ConstIterator it = ml.begin(); it != ml.end(); ++it) { + MailSummary mailDrag = *it; + *ts << _src->text(mailDrag.serialNumber()); + if (dlg->wasCancelled()) { + break; + } + dlg->progressBar()->setValue(++i); + kapp->eventLoop()->processEvents(TQEventLoop::ExcludeSocketNotifiers); + } + + delete dlg; + delete ts; + } + return rc; +} + diff --git a/libtdepim/maillistdrag.h b/libtdepim/maillistdrag.h new file mode 100644 index 000000000..f13e59ebf --- /dev/null +++ b/libtdepim/maillistdrag.h @@ -0,0 +1,135 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2003 Don Sanders <sanders@kde.org> + Copyright (c) 2005 George Staikos <staikos@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ +#ifndef maillistdrag_h +#define maillistdrag_h + +#include "tqdragobject.h" +#include "tqvaluelist.h" +#include "tqglobal.h" +#include "time.h" + +#include <tdepimmacros.h> + +/** + * KDEPIM classes for drag and drop of mails + * + * // Code example for drag and drop enabled widget + * + * void SomeWidget::contentsDropEvent(TQDropEvent *e) + * { + * if (e->provides(MailListDrag::format())) { + * MailList mailList; + * MailListDrag::decode( e, mailList ); + * ... + **/ + +namespace KPIM { + +class KDE_EXPORT MailSummary +{ +public: + MailSummary( TQ_UINT32 serialNumber, TQString messageId, TQString subject, + TQString from, TQString to, time_t date ); + MailSummary() {} + ~MailSummary() {} + + /*** Set fields for this mail summary ***/ + void set( TQ_UINT32, TQString, TQString, TQString, TQString, time_t ); + + /*** KMail unique identification number ***/ + TQ_UINT32 serialNumber() const; + + /*** MD5 checksum of message identification string ***/ + TQString messageId(); + + /*** Subject of the message including prefixes ***/ + TQString subject(); + + /*** Simplified from address ***/ + TQString from(); + + /** Simplified to address ***/ + TQString to(); + + /*** Date the message was sent ***/ + time_t date(); + +private: + TQ_UINT32 mSerialNumber; + TQString mMessageId, mSubject, mFrom, mTo; + time_t mDate; +}; + +// List of mail summaries +typedef TQValueList<MailSummary> MailList; + +// Object for the drag object to call-back for message fulltext +class KDE_EXPORT MailTextSource { +public: + MailTextSource() {} + virtual ~MailTextSource() {} + + virtual TQCString text(TQ_UINT32 serialNumber) const = 0; +}; + +// Drag and drop object for mails +class KDE_EXPORT MailListDrag : public TQStoredDrag +{ +public: + // Takes ownership of "src" and deletes it when done + MailListDrag( MailList, TQWidget * parent = 0, MailTextSource *src = 0 ); + ~MailListDrag(); + + const char *format(int i) const; + + bool provides(const char *mimeType) const; + + TQByteArray tqencodedData(const char *) const; + + /* Reset the list of mail summaries */ + void setMailList( MailList ); + + /* The format for this drag - "x-kmail-drag/message-list" */ + static const char* format(); + + /* Returns TRUE if the information in e can be decoded into a TQString; + otherwsie returns FALSE */ + static bool canDecode( TQMimeSource* e ); + + /* Attempts to decode the dropped information; + Returns TRUE if successful; otherwise return false */ + static bool decode( TQDropEvent* e, MailList& s ); + + /* Attempts to decode the serialNumbers of the dropped information; + Returns TRUE if successful; otherwise return false */ + static bool decode( TQDropEvent* e, TQByteArray& a ); + + /* Attempts to decode the encoded MailList; + Returns TRUE if successful; otherwise return false */ + static bool decode( TQByteArray& a, MailList& s ); + +private: + MailTextSource *_src; +}; + +} +#endif /*maillistdrag_h*/ diff --git a/libtdepim/overlaywidget.cpp b/libtdepim/overlaywidget.cpp new file mode 100644 index 000000000..ac05d84fd --- /dev/null +++ b/libtdepim/overlaywidget.cpp @@ -0,0 +1,92 @@ +/** -*- c++ -*- + * overlaywidget.h + * + * Copyright (c) 2004 David Faure <faure@kde.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of this program with any edition of + * the TQt library by Trolltech AS, Norway (or with modified versions + * of TQt that use the same license as TQt), and distribute linked + * combinations including the two. You must obey the GNU General + * Public License in all respects for all of the code used other than + * TQt. If you modify this file, you may extend this exception to + * your version of the file, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from + * your version. + */ + +#include "overlaywidget.h" +using namespace KPIM; + +OverlayWidget::OverlayWidget( TQWidget* alignWidget, TQWidget* parent, const char* name ) + : TQHBox( parent, name ), mAlignWidget( 0 ) +{ + setAlignWidget( alignWidget ); +} + +OverlayWidget::~OverlayWidget() +{ +} + +void OverlayWidget::reposition() +{ + if ( !mAlignWidget ) + return; + // p is in the alignWidget's coordinates + TQPoint p; + // We are always above the alignWidget, right-aligned with it. + p.setX( mAlignWidget->width() - width() ); + p.setY( -height() ); + // Position in the toplevelwidget's coordinates + TQPoint pTopLevel = mAlignWidget->mapTo( tqtopLevelWidget(), p ); + // Position in the widget's parentWidget coordinates + TQPoint pParent = parentWidget()->mapFrom( tqtopLevelWidget(), pTopLevel ); + // Move 'this' to that position. + move( pParent ); +} + +void OverlayWidget::setAlignWidget( TQWidget * w ) +{ + if (w == mAlignWidget) + return; + + if (mAlignWidget) + mAlignWidget->removeEventFilter(this); + + mAlignWidget = w; + + if (mAlignWidget) + mAlignWidget->installEventFilter(this); + + reposition(); +} + +bool OverlayWidget::eventFilter( TQObject* o, TQEvent* e) +{ + if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(mAlignWidget) && + ( e->type() == TQEvent::Move || e->type() == TQEvent::Resize ) ) { + reposition(); + } + return TQFrame::eventFilter(o,e); +} + +void OverlayWidget::resizeEvent( TQResizeEvent* ev ) +{ + reposition(); + TQFrame::resizeEvent( ev ); +} + +#include "overlaywidget.moc" diff --git a/libtdepim/overlaywidget.h b/libtdepim/overlaywidget.h new file mode 100644 index 000000000..9e611fb92 --- /dev/null +++ b/libtdepim/overlaywidget.h @@ -0,0 +1,70 @@ +/* -*- c++ -*- + * overlaywidget.h + * + * Copyright (c) 2004 David Faure <faure@kde.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of this program with any edition of + * the TQt library by Trolltech AS, Norway (or with modified versions + * of TQt that use the same license as TQt), and distribute linked + * combinations including the two. You must obey the GNU General + * Public License in all respects for all of the code used other than + * TQt. If you modify this file, you may extend this exception to + * your version of the file, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from + * your version. + */ +#ifndef OVERLAYWIDGET_H +#define OVERLAYWIDGET_H + +#include <tqhbox.h> + +namespace KPIM { + +/** + * This is a widget that can align itself with another one, without using a tqlayout, + * so that it can actually be on top of other widgets. + * Currently the only supported type of tqalignment is "right aligned, on top of the other widget". + * + * OverlayWidget inherits TQHBox for convenience purposes (tqlayout, and frame) + */ +class OverlayWidget : public TQHBox +{ + Q_OBJECT + TQ_OBJECT + +public: + OverlayWidget( TQWidget* alignWidget, TQWidget* parent, const char* name = 0 ); + ~OverlayWidget(); + + TQWidget * alignWidget() { return mAlignWidget; } + void setAlignWidget( TQWidget * alignWidget ); + +protected: + void resizeEvent( TQResizeEvent* ev ); + bool eventFilter( TQObject* o, TQEvent* e); + +private: + void reposition(); + +private: + TQWidget * mAlignWidget; +}; + +} // namespace + +#endif /* OVERLAYWIDGET_H */ + diff --git a/libtdepim/pics/CMakeLists.txt b/libtdepim/pics/CMakeLists.txt new file mode 100644 index 000000000..7cbe71d0a --- /dev/null +++ b/libtdepim/pics/CMakeLists.txt @@ -0,0 +1,15 @@ +################################################# +# +# (C) 2010-2011 Serghei Amelian +# serghei (DOT) amelian (AT) gmail.com +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +install( FILES + addresseelineedit.png clicklineedit.png + kdateedit.png ktimeedit.png + DESTINATION ${DATA_INSTALL_DIR}/tdepimwidgets/pics ) diff --git a/libtdepim/pics/Makefile.am b/libtdepim/pics/Makefile.am new file mode 100644 index 000000000..c4c611966 --- /dev/null +++ b/libtdepim/pics/Makefile.am @@ -0,0 +1,6 @@ +tdepimwidgetsdata_DATA = addresseelineedit.png \ + clicklineedit.png kdateedit.png ktimeedit.png + +tdepimwidgetsdatadir = $(kde_datadir)/tdepimwidgets/pics + +EXTRA_DIST = $(tdepimwidgetsdata_DATA) diff --git a/libtdepim/pics/addresseelineedit.png b/libtdepim/pics/addresseelineedit.png Binary files differnew file mode 100644 index 000000000..81d313913 --- /dev/null +++ b/libtdepim/pics/addresseelineedit.png diff --git a/libtdepim/pics/clicklineedit.png b/libtdepim/pics/clicklineedit.png Binary files differnew file mode 100644 index 000000000..ec40fb2f3 --- /dev/null +++ b/libtdepim/pics/clicklineedit.png diff --git a/libtdepim/pics/kdateedit.png b/libtdepim/pics/kdateedit.png Binary files differnew file mode 100644 index 000000000..e2fa34682 --- /dev/null +++ b/libtdepim/pics/kdateedit.png diff --git a/libtdepim/pics/ktimeedit.png b/libtdepim/pics/ktimeedit.png Binary files differnew file mode 100644 index 000000000..1b0d79afe --- /dev/null +++ b/libtdepim/pics/ktimeedit.png diff --git a/libtdepim/pimemoticons.kcfg b/libtdepim/pimemoticons.kcfg new file mode 100644 index 000000000..f06a7743a --- /dev/null +++ b/libtdepim/pimemoticons.kcfg @@ -0,0 +1,178 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE kcfg SYSTEM "http://www.kde.org/standards/kcfg/1.0/kcfg.dtd"> +<kcfg> + <kcfgfile name="pimemoticonsrc"/> + <group name="General"> + <entry name="Theme" type="String"> + <default>Default</default> + <label>Emoticon theme</label> + <whatsthis>This allows you to change the emoticon theme that should be used.</whatsthis> + </entry> + <entry name="smiley$(sindex)" type="String" key="smiley_$(sindex)"> + <parameter name="sindex" type="Enum"> + <values> + <value>angry_0</value> + <value>angry_1</value> + <!--value>angry_2</value--> + <!--value>angry_3</value--> + <value>bat_0</value> + <value>bat_1</value> + <!--value>beer_0</value--> + <!--value>beer_1</value--> + <value>cake_0</value> + <!--value>camera_0</value--> + <!--value>camera_1</value--> + <value>cat_0</value> + <!--value>clock_0</value--> + <!--value>clock_1</value--> + <!--value>cocktail_0</value--> + <!--value>cocktail_1</value--> + <value>confused_0</value> + <value>confused_1</value> + <value>confused_2</value> + <value>confused_3</value> + <value>cry_0</value> + <value>cry_1</value> + <value>cry_2</value> + <value>cry_3</value> + <!--value>cup_0</value--> + <!--value>cup_1</value--> + <value>dog_0</value> + <value>embarassed_0</value> + <value>embarassed_1</value> + <!--value>email_0</value--> + <!--value>email_1</value--> + <value>film_0</value> + <!--value>kiss_0</value--> + <!--value>kiss_1</value--> + <value>kiss_2</value> + <value>kiss_3</value> + <value>kiss_4</value> + <value>kiss_5</value> + <!--value>lightbulb_0</value--> + <!--value>lightbulb_1</value--> + <!--value>love_0</value--> + <!--value>love_1</value--> + <!--value>note_0</value--> + <value>omg_0</value> + <value>omg_1</value> + <value>omg_2</value> + <value>omg_3</value> + <!--value>phone_0</value--> + <!--value>phone_1</value--> + <!--value>present_0</value--> + <!--value>present_1</value--> + <!--value>rose_0</value--> + <!--value>rose_1</value--> + <value>sad_0</value> + <value>sad_1</value> + <!--value>shade_0</value--> + <value>shade_1</value> + <!--value>shade_2</value--> + <value>smile_0</value> + <value>smile_1</value> + <value>smile_2</value> + <value>star_0</value> + <value>teeth_0</value> + <value>teeth_1</value> + <value>teeth_2</value> + <value>teeth_3</value> + <!--value>thumbs_down_0</value--> + <!--value>thumbs_down_1</value--> + <!--value>thumbs_up_0</value--> + <!--value>thumbs_up_1</value--> + <value>tongue_0</value> + <value>tongue_1</value> + <value>tongue_2</value> + <value>tongue_3</value> + <!--value>unlove_0</value--> + <!--value>unlove_1</value--> + <!--value>wilted_rose_0</value--> + <!--value>wilted_rose_1</value--> + <value>wink_0</value> + <value>wink_1</value> + </values> + </parameter> + <default param='angry_0'>:-@</default> + <default param='angry_1'>:@</default> + <!--default param='angry_2'>>:-(</default--> + <!--default param='angry_3'>>:(</default--> + <default param='bat_0'>:-[</default> + <default param='bat_1'>:[</default> + <!--default param='beer_0'>(B)</default--> + <!--default param='beer_1'>(b)</default--> + <default param='cake_0'>(^)</default> + <!--default param='camera_0'>(P)</default--> + <!--default param='camera_1'>(p)</default--> + <default param='cat_0'>(@)</default> + <!--default param='clock_0'>(O)</default--> + <!--default param='clock_1'>(o)</default--> + <!--default param='cocktail_0'>(D)</default--> + <!--default param='cocktail_1'>(d)</default--> + <default param='confused_0'>:-S</default> + <default param='confused_1'>:S</default> + <default param='confused_2'>:-s</default> + <default param='confused_3'>:s</default> + <default param='cry_0'>:'(</default> + <default param='cry_1'>:'-(</default> + <default param='cry_2'>;-(</default> + <default param='cry_3'>;(</default> + <!--default param='cup_0'>(C)</default--> <!-- We don't want coffee cups in the copyright headers of cvs commit messages. --> + <!--default param='cup_1'>(c)</default--> + <default param='dog_0'>(&)</default> + <default param='embarassed_0'>:-$</default> + <default param='embarassed_1'>:$</default> + <!--default param='email_0'>(E)</default--> + <!--default param='email_1'>(e)</default--> + <default param='film_0'>(~)</default> + <!--default param='kiss_0'>(K)</default--> + <!--default param='kiss_1'>(k)</default--> + <default param='kiss_2'>:-X</default> + <default param='kiss_3'>:X</default> + <default param='kiss_4'>:-x</default> + <default param='kiss_5'>:x</default> + <!--default param='lightbulb_0'>(I)</default--> + <!--default param='lightbulb_1'>(i)</default--> + <!--default param='love_0'>(L)</default--> + <!--default param='love_1'>(l)</default--> + <!--default param='note_0'>(8)</default--> + <default param='omg_0'>:-O</default> + <default param='omg_1'>:O</default> + <default param='omg_2'>:-o</default> + <default param='omg_3'>:o</default> + <!--default param='phone_0'>(T)</default--> + <!--default param='phone_1'>(t)</default--> + <!--default param='present_0'>(G)</default--> + <!--default param='present_1'>(g)</default--> + <!--default param='rose_0'>(F)</default--> + <!--default param='rose_1'>(f)</default--> + <default param='sad_0'>:-(</default> + <default param='sad_1'>:(</default> + <!--default param='shade_0'>(H)</default--> + <default param='shade_1'>8-)</default> + <!--default param='shade_2'>8)</default--> + <default param='smile_0'>:-)</default> + <default param='smile_1'>:)</default> + <default param='smile_2'>:o)</default> + <default param='star_0'>(*)</default> + <default param='teeth_0'>:-D</default> + <default param='teeth_1'>:D</default> + <default param='teeth_2'>:-d</default> + <default param='teeth_3'>:d</default> + <!--default param='thumbs_down_0'>(N)</default--> + <!--default param='thumbs_down_1'>(n)</default--> + <!--default param='thumbs_up_0'>(Y)</default--> + <!--default param='thumbs_up_1'>(y)</default--> + <default param='tongue_0'>:-P</default> + <default param='tongue_1'>:P</default> + <default param='tongue_2'>:-p</default> + <default param='tongue_3'>:p</default> + <!--default param='unlove_0'>(U)</default--> + <!--default param='unlove_1'>(u)</default--> + <!--default param='wilted_rose_0'>(W)</default--> + <!--default param='wilted_rose_1'>(w)</default--> + <default param='wink_0'>;-)</default> + <default param='wink_1'>;)</default> + </entry> + </group> +</kcfg> diff --git a/libtdepim/pimemoticons.kcfgc b/libtdepim/pimemoticons.kcfgc new file mode 100644 index 000000000..9121a7302 --- /dev/null +++ b/libtdepim/pimemoticons.kcfgc @@ -0,0 +1,5 @@ +File=pimemoticons.kcfg +ClassName=EmotIcons +Singleton=true +#ItemAccessors=true + diff --git a/libtdepim/pluginloader.h b/libtdepim/pluginloader.h new file mode 100644 index 000000000..6d38bbd18 --- /dev/null +++ b/libtdepim/pluginloader.h @@ -0,0 +1,135 @@ +/* -*- c++ -*- + This file is part of libtdepim. + + Copyright (c) 2002,2004 Marc Mutz <mutz@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef __KPIM_SHARED_PLUGINLOADER_H__ +#define __KPIM_SHARED_PLUGINLOADER_H__ + +#include <pluginloaderbase.h> + +namespace KPIM { + + /** + * @short A generic plugin loader for when KPart::Plugin is overkill + * @author Marc Mutz <mutz@kde.org> based on KABC's FormatFactory + * + * This is a generic plugin loader / factory for small plugins that + * don't want to be TQObjects. + * + * @section Usage + * + * A PluginLoader takes two template arguments, <code>T</code> and + * <code>T_config</code>: + * + * <dl> + * <dt>T</dt><dd>The type of object to return</dd> + * <dt>T_config::mainfunc</dt><dd>The suffix of the factory function to call + * in the library to obtain a new object of type <code>T</code>. + * The string passed to <code>KLibrary::symbol()</code> is + * <code>libName_mainfunc</code>.</dd> + * <dt>T_config::path</dt><dd>The search pattern for <tt>.desktop</tt> files + * containing the plugin descriptions. This is the string passed as + * the @p filter argument to + * <code>KStandardDirs::findAllResources</code>.</dd> + * </dl> + * + * The last two parameters being strings, they are passed via an + * encapsulating class, of which <code>mainfunc</code> and + * <code>path</code> are public static members: + * + * <pre> + * struct MyObjectPluginLoaderConfig { + * static const char * const mainfunc; + * static const char * const path; + * }; + * const char * const MyObjectPluginLoaderConfig::mainfunc = "myapp_create_myobject"; + * const char * const MyObjectPluginLoaderConfig::path = "myapp/plugins/ *.desktop"; + * </pre> + * + * You would then use a <tt>typedef</tt> to create a less unwieldy + * name for your plugin loader: + * + * <pre> + * typedef KPIM::PluginLoader< MyObject, MyObjectPluginLoaderConfig > MyObjectPluginLoader; + * </pre> + * + * All of this is what the + * <code>KPIM_DEFINE_PLUGIN_LOADER(pluginloadername,type,mainfunc,path)</code> macro + * achieves. + * + **/ + template< typename T, typename T_config > + class KDE_EXPORT PluginLoader : public PluginLoaderBase { + protected: + PluginLoader() : PluginLoaderBase() {} + + private: + static PluginLoader<T,T_config> * mSelf; + + public: + virtual ~PluginLoader() { mSelf = 0; } + + /** Returns the single instance of this loader. */ + static PluginLoader<T,T_config> * instance() { + if ( !mSelf ) { + mSelf = new PluginLoader<T,T_config>(); + mSelf->scan(); + } + return mSelf; + } + + /** Rescans the plugin directory to find any newly installed + plugins. + **/ + virtual void scan() { + doScan( T_config::path ); + } + + /** Returns a pointer to a plugin object (of type @p T) or a null + pointer if the type wasn't found. You can extend this method + for when you want to handle builtin types */ + virtual T * createForName( const TQString & type ) const { + void * main_func = mainFunc( type, T_config::mainfunc ); + if ( !main_func ) return 0; + + // cast to a pointer to a function returning T*, call it and + // return the result; don't you love C? ;-) + return ((T* (*)())( main_func ))(); + } + }; + + template< typename T, typename T_config > + PluginLoader<T,T_config> * PluginLoader<T,T_config>::mSelf = 0; + +} + +#define KPIM_DEFINE_PLUGIN_LOADER( pl, t, mf, p ) \ + namespace { /* don't pollute namespaces */ \ + struct KDE_EXPORT pl##Config { \ + static const char * const mainfunc; \ + static const char * const path; \ + }; \ + const char * const pl##Config::mainfunc = mf; \ + const char * const pl##Config::path = p; \ + } \ + typedef KPIM::PluginLoader< t, pl##Config > pl; \ + + +#endif // __KPIM_SHARED_PLUGINLOADER_H__ diff --git a/libtdepim/pluginloaderbase.cpp b/libtdepim/pluginloaderbase.cpp new file mode 100644 index 000000000..60a925efa --- /dev/null +++ b/libtdepim/pluginloaderbase.cpp @@ -0,0 +1,159 @@ +/* -*- c++ -*- + This file is part of libtdepim. + + Copyright (c) 2002,2004 Marc Mutz <mutz@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <pluginloaderbase.h> + +#include <ksimpleconfig.h> +#include <klocale.h> +#include <kstandarddirs.h> +#include <klibloader.h> +#include <kglobal.h> +#include <kdebug.h> + +#include <tqfile.h> +#include <tqstringlist.h> + +static kdbgstream warning() { + return kdWarning( 5300 ) << "PluginLoaderBase: "; +} +#ifndef NDEBUG +static kdbgstream debug( bool cond ) +#else +static kndbgstream debug( bool cond ) +#endif +{ + return kdDebug( cond, 5300 ) << "PluginLoaderBase: "; +} + +namespace KPIM { + + PluginLoaderBase::PluginLoaderBase() : d(0) {} + PluginLoaderBase::~PluginLoaderBase() {} + + + TQStringList PluginLoaderBase::types() const { + TQStringList result; + for ( TQMap< TQString, PluginMetaData >::const_iterator it = mPluginMap.begin(); + it != mPluginMap.end() ; ++it ) + result.push_back( it.key() ); + return result; + } + + const PluginMetaData * PluginLoaderBase::infoForName( const TQString & type ) const { + return mPluginMap.contains( type ) ? &(mPluginMap[type]) : 0 ; + } + + + void PluginLoaderBase::doScan( const char * path ) { + mPluginMap.clear(); + + const TQStringList list = + KGlobal::dirs()->findAllResources( "data", path, true, true ); + for ( TQStringList::const_iterator it = list.begin() ; + it != list.end() ; ++it ) { + KSimpleConfig config( *it, true ); + if ( config.hasGroup( "Misc" ) && config.hasGroup( "Plugin" ) ) { + config.setGroup( "Plugin" ); + + const TQString type = config.readEntry( "Type" ).lower(); + if ( type.isEmpty() ) { + warning() << "missing or empty [Plugin]Type value in \"" + << *it << "\" - skipping" << endl; + continue; + } + + const TQString library = config.readEntry( "X-KDE-Library" ); + if ( library.isEmpty() ) { + warning() << "missing or empty [Plugin]X-KDE-Library value in \"" + << *it << "\" - skipping" << endl; + continue; + } + + config.setGroup( "Misc" ); + + TQString name = config.readEntry( "Name" ); + if ( name.isEmpty() ) { + warning() << "missing or empty [Misc]Name value in \"" + << *it << "\" - inserting default name" << endl; + name = i18n("Unnamed plugin"); + } + + TQString comment = config.readEntry( "Comment" ); + if ( comment.isEmpty() ) { + warning() << "missing or empty [Misc]Comment value in \"" + << *it << "\" - inserting default name" << endl; + comment = i18n("No description available"); + } + + mPluginMap.insert( type, PluginMetaData( library, name, comment ) ); + } else { + warning() << "Desktop file \"" << *it + << "\" doesn't seem to describe a plugin " + << "(misses Misc and/or Plugin group)" << endl; + } + } + } + + void * PluginLoaderBase::mainFunc( const TQString & type, + const char * mf_name ) const { + if ( type.isEmpty() || !mPluginMap.contains( type ) ) + return 0; + + const TQString libName = mPluginMap[ type ].library; + if ( libName.isEmpty() ) + return 0; + + const KLibrary * lib = openLibrary( libName ); + if ( !lib ) + return 0; + + mPluginMap[ type ].loaded = true; + + const TQString factory_name = libName + '_' + mf_name; + if ( !lib->hasSymbol( factory_name.latin1() ) ) { + warning() << "No symbol named \"" << factory_name.latin1() << "\" (" + << factory_name << ") was found in library \"" << libName + << "\"" << endl; + return 0; + } + + return lib->symbol( factory_name.latin1() ); + } + + const KLibrary * PluginLoaderBase::openLibrary( const TQString & libName ) const { + + const TQString path = KLibLoader::findLibrary( TQFile::encodeName( libName ) ); + + if ( path.isEmpty() ) { + warning() << "No plugin library named \"" << libName + << "\" was found!" << endl; + return 0; + } + + const KLibrary * library = KLibLoader::self()->library( TQFile::encodeName( path ) ); + + debug( !library ) << "Could not load library '" << libName << "'" << endl; + + return library; + } + + +} // namespace KMime diff --git a/libtdepim/pluginloaderbase.h b/libtdepim/pluginloaderbase.h new file mode 100644 index 000000000..766372787 --- /dev/null +++ b/libtdepim/pluginloaderbase.h @@ -0,0 +1,86 @@ +/* -*- c++ -*- + This file is part of libtdepim. + + Copyright (c) 2002,2004 Marc Mutz <mutz@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef __LIBKDEPIM_PLUGINLOADERBASE_H__ +#define __LIBKDEPIM_PLUGINLOADERBASE_H__ + +#include <tqstring.h> +#include <tqmap.h> + +#include <tdepimmacros.h> + +class KLibrary; +class TQStringList; + +namespace KPIM { + + class KDE_EXPORT PluginMetaData { + public: + PluginMetaData() {} + PluginMetaData( const TQString & lib, const TQString & name, + const TQString & comment ) + : library( lib ), nameLabel( name ), + descriptionLabel( comment ), loaded( false ) {} + TQString library; + TQString nameLabel; + TQString descriptionLabel; + mutable bool loaded; + }; + + class KDE_EXPORT PluginLoaderBase { + protected: + PluginLoaderBase(); + virtual ~PluginLoaderBase(); + + public: + /** Returns a list of all available plugin objects (of kind @p T) */ + TQStringList types() const; + + /** Returns the @ref PluginMetaData structure for a given type */ + const PluginMetaData * infoForName( const TQString & type ) const; + + /** Overload this method in subclasses to call @ref doScan with + the right @p path argument */ + virtual void scan() = 0; + + protected: + /** Rescans the plugin directory to find any newly installed + plugins. Extend this method in subclasses to add any + builtins. Subclasses must call this explicitely. It's not + called for them in the constructor. + **/ + void doScan( const char * path ); + + /** Returns a pointer to symbol @p main_func in the library that + implements the plugin of type @p type */ + void * mainFunc( const TQString & type, const char * main_func ) const; + + private: + const KLibrary * openLibrary( const TQString & libName ) const; + TQMap< TQString, PluginMetaData > mPluginMap; + + class Private; + Private * d; + }; + +} // namespace KMime + +#endif // __LIBKDEPIM_PLUGINLOADERBASE_H__ diff --git a/libtdepim/progressdialog.cpp b/libtdepim/progressdialog.cpp new file mode 100644 index 000000000..bd5b594c6 --- /dev/null +++ b/libtdepim/progressdialog.cpp @@ -0,0 +1,437 @@ +/** -*- c++ -*- + * progressdialog.cpp + * + * Copyright (c) 2004 Till Adam <adam@kde.org>, + * David Faure <faure@kde.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of this program with any edition of + * the TQt library by Trolltech AS, Norway (or with modified versions + * of TQt that use the same license as TQt), and distribute linked + * combinations including the two. You must obey the GNU General + * Public License in all respects for all of the code used other than + * TQt. If you modify this file, you may extend this exception to + * your version of the file, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from + * your version. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <tqapplication.h> +#include <tqlayout.h> +#include <tqprogressbar.h> +#include <tqtimer.h> +#include <tqheader.h> +#include <tqobject.h> +#include <tqscrollview.h> +#include <tqtoolbutton.h> +#include <tqpushbutton.h> +#include <tqvbox.h> +#include <tqtooltip.h> + +#include <klocale.h> +#include <kdialog.h> +#include <kstdguiitem.h> +#include <kiconloader.h> +#include <kdebug.h> + +#include "progressdialog.h" +#include "progressmanager.h" +#include "ssllabel.h" +#include <tqwhatsthis.h> + +namespace KPIM { + +class TransactionItem; + +TransactionItemView::TransactionItemView( TQWidget * parent, + const char * name, + WFlags f ) + : TQScrollView( parent, name, f ) { + setFrameStyle( NoFrame ); + mBigBox = new TQVBox( viewport() ); + mBigBox->setSpacing( 5 ); + addChild( mBigBox ); + setResizePolicy( TQScrollView::AutoOneFit ); // Fit so that the box expands horizontally +} + +TransactionItem* TransactionItemView::addTransactionItem( ProgressItem* item, bool first ) +{ + TransactionItem *ti = new TransactionItem( mBigBox, item, first ); + ti->hide(); + TQTimer::singleShot( 1000, ti, TQT_SLOT( show() ) ); + return ti; +} + +void TransactionItemView::resizeContents( int w, int h ) +{ + // (handling of TQEvent::LayoutHint in TQScrollView calls this method) + //kdDebug(5300) << k_funcinfo << w << "," << h << endl; + TQScrollView::resizeContents( w, h ); + // Tell the tqlayout in the parent (progressdialog) that our size changed + updateGeometry(); + // Resize the parent (progressdialog) - this works but resize horizontally too often + //parentWidget()->adjustSize(); + + TQApplication::sendPostedEvents( 0, TQEvent::ChildInserted ); + TQApplication::sendPostedEvents( 0, TQEvent::LayoutHint ); + TQSize sz = parentWidget()->tqsizeHint(); + int currentWidth = parentWidget()->width(); + // Don't resize to sz.width() every time when it only reduces a little bit + if ( currentWidth < sz.width() || currentWidth > sz.width() + 100 ) + currentWidth = sz.width(); + parentWidget()->resize( currentWidth, sz.height() ); +} + +TQSize TransactionItemView::tqsizeHint() const +{ + return tqminimumSizeHint(); +} + +TQSize TransactionItemView::tqminimumSizeHint() const +{ + int f = 2 * frameWidth(); + // Make room for a vertical scrollbar in all cases, to avoid a horizontal one + int vsbExt = verticalScrollBar()->tqsizeHint().width(); + int minw = tqtopLevelWidget()->width() / 3; + int maxh = tqtopLevelWidget()->height() / 2; + TQSize sz( mBigBox->tqminimumSizeHint() ); + sz.setWidth( TQMAX( sz.width(), minw ) + f + vsbExt ); + sz.setHeight( TQMIN( sz.height(), maxh ) + f ); + return sz; +} + + +void TransactionItemView::slotLayoutFirstItem() +{ + /* + The below relies on some details in TQt's behaviour regarding deleting + objects. This slot is called from the destroyed signal of an item just + going away. That item is at that point still in the list of chilren, but + since the vtable is already gone, it will have type TQObject. The first + one with both the right name and the right class therefor is what will + be the first item very shortly. That's the one we want to remove the + hline for. + */ + TQObject *o = mBigBox->child( "TransactionItem", "KPIM::TransactionItem" ); + TransactionItem *ti = dynamic_cast<TransactionItem*>( o ); + if ( ti ) { + ti->hideHLine(); + } +} + + +// ---------------------------------------------------------------------------- + +TransactionItem::TransactionItem( TQWidget* parent, + ProgressItem *item, bool first ) + : TQVBox( parent, "TransactionItem" ), mCancelButton( 0 ), mItem( item ) + +{ + setSpacing( 2 ); + setMargin( 2 ); + tqsetSizePolicy( TQSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Fixed ) ); + + mFrame = new TQFrame( this ); + mFrame->setFrameShape( TQFrame::HLine ); + mFrame->setFrameShadow( TQFrame::Raised ); + mFrame->show(); + setStretchFactor( mFrame, 3 ); + + TQHBox *h = new TQHBox( this ); + h->setSpacing( 5 ); + + mItemLabel = new TQLabel( item->label(), h ); + // always interpret the label text as RichText, but disable word wrapping + mItemLabel->setTextFormat( TQt::RichText ); + mItemLabel->tqsetAlignment( TQt::AlignAuto | TQt::AlignVCenter | TQt::SingleLine ); + h->tqsetSizePolicy( TQSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Fixed ) ); + + mProgress = new TQProgressBar( 100, h ); + mProgress->setProgress( item->progress() ); + + if ( item->canBeCanceled() ) { + mCancelButton = new TQPushButton( SmallIcon( "cancel" ), TQString(), h ); + TQToolTip::add( mCancelButton, i18n("Cancel this operation.") ); + connect ( mCancelButton, TQT_SIGNAL( clicked() ), + this, TQT_SLOT( slotItemCanceled() )); + } + + h = new TQHBox( this ); + h->setSpacing( 5 ); + h->tqsetSizePolicy( TQSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Fixed ) ); + mSSLLabel = new SSLLabel( h ); + mSSLLabel->tqsetSizePolicy( TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed ) ); + mItemtqStatus = new TQLabel( item->status(), h ); + // always interpret the status text as RichText, but disable word wrapping + mItemtqStatus->setTextFormat( TQt::RichText ); + mItemtqStatus->tqsetAlignment( TQt::AlignAuto | TQt::AlignVCenter | TQt::SingleLine ); + // richtext leads to tqsizeHint acting as if wrapping was enabled though, + // so make sure we only ever have the height of one line. + mItemtqStatus->tqsetSizePolicy( TQSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Ignored ) ); + mItemtqStatus->setFixedHeight( mItemLabel->tqsizeHint().height() ); + setCrypto( item->usesCrypto() ); + if( first ) hideHLine(); +} + +TransactionItem::~TransactionItem() +{ +} + +void TransactionItem::hideHLine() +{ + mFrame->hide(); +} + +void TransactionItem::setProgress( int progress ) +{ + mProgress->setProgress( progress ); +} + +void TransactionItem::setLabel( const TQString& label ) +{ + mItemLabel->setText( label ); +} + +void TransactionItem::setqStatus( const TQString& status ) +{ + mItemtqStatus->setText( status ); +} + +void TransactionItem::setCrypto( bool on ) +{ + if (on) + mSSLLabel->setEncrypted( true ); + else + mSSLLabel->setEncrypted( false ); + + mSSLLabel->setState( mSSLLabel->lastState() ); +} + +void TransactionItem::setTotalSteps( int totalSteps ) +{ + mProgress->setTotalSteps( totalSteps ); +} + +void TransactionItem::slotItemCanceled() +{ + if ( mItem ) + mItem->cancel(); +} + + +void TransactionItem::addSubTransaction( ProgressItem* /*item*/ ) +{ + +} + + +// --------------------------------------------------------------------------- + +ProgressDialog::ProgressDialog( TQWidget* alignWidget, TQWidget* parent, const char* name ) + : OverlayWidget( alignWidget, parent, name ), mWasLastShown( false ) +{ + setFrameStyle( TQFrame::Panel | TQFrame::Sunken ); // TQFrame + setSpacing( 0 ); // TQHBox + setMargin( 1 ); + + mScrollView = new TransactionItemView( this, "ProgressScrollView" ); + + // No more close button for now, since there is no more autoshow + /* + TQVBox* rightBox = new TQVBox( this ); + TQToolButton* pbClose = new TQToolButton( rightBox ); + pbClose->setAutoRaise(true); + pbClose->tqsetSizePolicy( TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed ) ); + pbClose->setFixedSize( 16, 16 ); + pbClose->setIconSet( KGlobal::iconLoader()->loadIconSet( "fileclose", KIcon::Small, 14 ) ); + TQToolTip::add( pbClose, i18n( "Hide detailed progress window" ) ); + connect(pbClose, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotClose())); + TQWidget* spacer = new TQWidget( rightBox ); // don't let the close button take up all the height + rightBox->setStretchFactor( spacer, 100 ); + */ + + /* + * Get the singleton ProgressManager item which will inform us of + * appearing and vanishing items. + */ + ProgressManager *pm = ProgressManager::instance(); + connect ( pm, TQT_SIGNAL( progressItemAdded( KPIM::ProgressItem* ) ), + this, TQT_SLOT( slotTransactionAdded( KPIM::ProgressItem* ) ) ); + connect ( pm, TQT_SIGNAL( progressItemCompleted( KPIM::ProgressItem* ) ), + this, TQT_SLOT( slotTransactionCompleted( KPIM::ProgressItem* ) ) ); + connect ( pm, TQT_SIGNAL( progressItemProgress( KPIM::ProgressItem*, unsigned int ) ), + this, TQT_SLOT( slotTransactionProgress( KPIM::ProgressItem*, unsigned int ) ) ); + connect ( pm, TQT_SIGNAL( progressItemtqStatus( KPIM::ProgressItem*, const TQString& ) ), + this, TQT_SLOT( slotTransactiontqStatus( KPIM::ProgressItem*, const TQString& ) ) ); + connect ( pm, TQT_SIGNAL( progressItemLabel( KPIM::ProgressItem*, const TQString& ) ), + this, TQT_SLOT( slotTransactionLabel( KPIM::ProgressItem*, const TQString& ) ) ); + connect ( pm, TQT_SIGNAL( progressItemUsesCrypto(KPIM::ProgressItem*, bool) ), + this, TQT_SLOT( slotTransactionUsesCrypto( KPIM::ProgressItem*, bool ) ) ); + connect ( pm, TQT_SIGNAL( progressItemUsesBusyIndicator(KPIM::ProgressItem*, bool) ), + this, TQT_SLOT( slotTransactionUsesBusyIndicator( KPIM::ProgressItem*, bool ) ) ); + connect ( pm, TQT_SIGNAL( showProgressDialog() ), + this, TQT_SLOT( slotShow() ) ); +} + +void ProgressDialog::closeEvent( TQCloseEvent* e ) +{ + e->accept(); + hide(); +} + + +/* + * Destructor + */ +ProgressDialog::~ProgressDialog() +{ + // no need to delete child widgets. +} + +void ProgressDialog::slotTransactionAdded( ProgressItem *item ) +{ + TransactionItem *parent = 0; + if ( item->parent() ) { + if ( mTransactionsToListviewItems.contains( item->parent() ) ) { + parent = mTransactionsToListviewItems[ item->parent() ]; + parent->addSubTransaction( item ); + } + } else { + const bool first = mTransactionsToListviewItems.empty(); + TransactionItem *ti = mScrollView->addTransactionItem( item, first ); + if ( ti ) + mTransactionsToListviewItems.replace( item, ti ); + if ( first && mWasLastShown ) + TQTimer::singleShot( 1000, this, TQT_SLOT( slotShow() ) ); + + } +} + +void ProgressDialog::slotTransactionCompleted( ProgressItem *item ) +{ + if ( mTransactionsToListviewItems.contains( item ) ) { + TransactionItem *ti = mTransactionsToListviewItems[ item ]; + mTransactionsToListviewItems.remove( item ); + ti->setItemComplete(); + TQTimer::singleShot( 3000, ti, TQT_SLOT( deleteLater() ) ); + // see the slot for comments as to why that works + connect ( ti, TQT_SIGNAL( destroyed() ), + mScrollView, TQT_SLOT( slotLayoutFirstItem() ) ); + } + // This was the last item, hide. + if ( mTransactionsToListviewItems.empty() ) + TQTimer::singleShot( 3000, this, TQT_SLOT( slotHide() ) ); +} + +void ProgressDialog::slotTransactionCanceled( ProgressItem* ) +{ +} + +void ProgressDialog::slotTransactionProgress( ProgressItem *item, + unsigned int progress ) +{ + if ( mTransactionsToListviewItems.contains( item ) ) { + TransactionItem *ti = mTransactionsToListviewItems[ item ]; + ti->setProgress( progress ); + } +} + +void ProgressDialog::slotTransactiontqStatus( ProgressItem *item, + const TQString& status ) +{ + if ( mTransactionsToListviewItems.contains( item ) ) { + TransactionItem *ti = mTransactionsToListviewItems[ item ]; + ti->setqStatus( status ); + } +} + +void ProgressDialog::slotTransactionLabel( ProgressItem *item, + const TQString& label ) +{ + if ( mTransactionsToListviewItems.contains( item ) ) { + TransactionItem *ti = mTransactionsToListviewItems[ item ]; + ti->setLabel( label ); + } +} + + +void ProgressDialog::slotTransactionUsesCrypto( ProgressItem *item, + bool value ) +{ + if ( mTransactionsToListviewItems.contains( item ) ) { + TransactionItem *ti = mTransactionsToListviewItems[ item ]; + ti->setCrypto( value ); + } +} + +void ProgressDialog::slotTransactionUsesBusyIndicator( KPIM::ProgressItem *item, bool value ) +{ + if ( mTransactionsToListviewItems.contains( item ) ) { + TransactionItem *ti = mTransactionsToListviewItems[ item ]; + if ( value ) + ti->setTotalSteps( 0 ); + else + ti->setTotalSteps( 100 ); + } +} + +void ProgressDialog::slotShow() +{ + setVisible( true ); +} + +void ProgressDialog::slotHide() +{ + // check if a new item showed up since we started the timer. If not, hide + if ( mTransactionsToListviewItems.isEmpty() ) { + setVisible( false ); + } +} + +void ProgressDialog::slotClose() +{ + mWasLastShown = false; + setVisible( false ); +} + +void ProgressDialog::setVisible( bool b ) +{ + if ( b ) + show(); + else + hide(); + emit visibilityChanged( b ); +} + +void ProgressDialog::slotToggleVisibility() +{ + /* Since we are only hiding with a timeout, there is a short period of + * time where the last item is still visible, but clicking on it in + * the statusbarwidget should not display the dialog, because there + * are no items to be shown anymore. Guard against that. + */ + mWasLastShown = !isShown(); + if ( isShown() || !mTransactionsToListviewItems.isEmpty() ) + setVisible( !isShown() ); +} + +} + +#include "progressdialog.moc" diff --git a/libtdepim/progressdialog.h b/libtdepim/progressdialog.h new file mode 100644 index 000000000..b2511624e --- /dev/null +++ b/libtdepim/progressdialog.h @@ -0,0 +1,160 @@ +/* -*- c++ -*- + * progressdialog.h + * + * Copyright (c) 2004 Till Adam <adam@kde.org> + * based on imapprogressdialog.cpp ,which is + * Copyright (c) 2002-2003 Klar�vdalens Datakonsult AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of this program with any edition of + * the TQt library by Trolltech AS, Norway (or with modified versions + * of TQt that use the same license as TQt), and distribute linked + * combinations including the two. You must obey the GNU General + * Public License in all respects for all of the code used other than + * TQt. If you modify this file, you may extend this exception to + * your version of the file, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from + * your version. + */ + +#ifndef __KPIM_PROGRESSDIALOG_H__ +#define __KPIM_PROGRESSDIALOG_H__ + +#include <tqdialog.h> +#include <tqlistview.h> +#include <tqlabel.h> +#include <tqvbox.h> +#include "overlaywidget.h" +#include <tdepimmacros.h> + +class TQProgressBar; +class TQScrollView; +class TQFrame; + +namespace KPIM { +class ProgressItem; +class TransactionItemListView; +class TransactionItem; +class SSLLabel; + +class TransactionItemView : public TQScrollView { + Q_OBJECT + TQ_OBJECT +public: + TransactionItemView( TQWidget * parent = 0, + const char * name = 0, + WFlags f = 0 ); + + virtual ~TransactionItemView() + {} + TransactionItem* addTransactionItem( ProgressItem *item, bool first ); + + + TQSize tqsizeHint() const; + TQSize tqminimumSizeHint() const; +public slots: + void slotLayoutFirstItem(); + +protected: + virtual void resizeContents ( int w, int h ); + +private: + TQVBox * mBigBox; +}; + +class TransactionItem : public TQVBox { + + Q_OBJECT + TQ_OBJECT + +public: + TransactionItem( TQWidget * parent, + ProgressItem* item, bool first ); + + ~TransactionItem(); + + void hideHLine(); + + void setProgress( int progress ); + void setLabel( const TQString& ); + void setqStatus( const TQString& ); + void setCrypto( bool ); + void setTotalSteps( int totalSteps ); + + ProgressItem* item() const { return mItem; } + + void addSubTransaction( ProgressItem *item); + + // The progressitem is deleted immediately, we take 5s to go out, + // so better not use mItem during this time. + void setItemComplete() { mItem = 0; } + +public slots: + void slotItemCanceled(); + +protected: + TQProgressBar* mProgress; + TQPushButton* mCancelButton; + TQLabel* mItemLabel; + TQLabel* mItemtqStatus; + TQFrame* mFrame; + SSLLabel* mSSLLabel; + ProgressItem* mItem; +}; + +class KDE_EXPORT ProgressDialog : public OverlayWidget +{ + Q_OBJECT + TQ_OBJECT + +public: + ProgressDialog( TQWidget* alignWidget, TQWidget* parent, const char* name = 0 ); + ~ProgressDialog(); + void setVisible( bool b ); + +public slots: + void slotToggleVisibility(); + +protected slots: +void slotTransactionAdded( KPIM::ProgressItem *item ); + void slotTransactionCompleted( KPIM::ProgressItem *item ); + void slotTransactionCanceled( KPIM::ProgressItem *item ); + void slotTransactionProgress( KPIM::ProgressItem *item, unsigned int progress ); + void slotTransactiontqStatus( KPIM::ProgressItem *item, const TQString& ); + void slotTransactionLabel( KPIM::ProgressItem *item, const TQString& ); + void slotTransactionUsesCrypto( KPIM::ProgressItem *item, bool ); + void slotTransactionUsesBusyIndicator( KPIM::ProgressItem*, bool ); + + void slotClose(); + void slotShow(); + void slotHide(); + +signals: + void visibilityChanged( bool ); + +protected: + virtual void closeEvent( TQCloseEvent* ); + + TransactionItemView* mScrollView; + TransactionItem* mPreviousItem; + TQMap< const ProgressItem*, TransactionItem* > mTransactionsToListviewItems; + bool mWasLastShown; +}; + + +} // namespace KPIM + +#endif // __KPIM_PROGRESSDIALOG_H__ diff --git a/libtdepim/progressmanager.cpp b/libtdepim/progressmanager.cpp new file mode 100644 index 000000000..a00970c5e --- /dev/null +++ b/libtdepim/progressmanager.cpp @@ -0,0 +1,248 @@ +/* + progressmanager.cpp + + This file is part of KDEPIM. + + Author: Till Adam <adam@kde.org> (C) 2004 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <tqvaluevector.h> +#include <kdebug.h> + +#include <klocale.h> +#include <kstaticdeleter.h> + +#include "progressmanager.h" + +namespace KPIM { + +KPIM::ProgressManager * KPIM::ProgressManager::mInstance = 0; +unsigned int KPIM::ProgressManager::uID = 42; + +ProgressItem::ProgressItem( + ProgressItem* parent, const TQString& id, + const TQString& label, const TQString& status, bool canBeCanceled, + bool usesCrypto ) + :mId( id ), mLabel( label ), mtqStatus( status ), mParent( parent ), + mCanBeCanceled( canBeCanceled ), mProgress( 0 ), mTotal( 0 ), + mCompleted( 0 ), mWaitingForKids( false ), mCanceled( false ), + mUsesCrypto( usesCrypto ), mUsesBusyIndicator( false ) + {} + +ProgressItem::~ProgressItem() +{ +} + +void ProgressItem::setComplete() +{ +// kdDebug(5300) << "ProgressItem::setComplete - " << label() << endl; + + if ( mChildren.isEmpty() ) { + if ( !mCanceled ) + setProgress( 100 ); + emit progressItemCompleted( this ); + if ( parent() ) + parent()->removeChild( this ); + deleteLater(); + } else { + mWaitingForKids = true; + } +} + +void ProgressItem::addChild( ProgressItem *kiddo ) +{ + mChildren.replace( kiddo, true ); +} + +void ProgressItem::removeChild( ProgressItem *kiddo ) +{ + mChildren.remove( kiddo ); + // in case we were waiting for the last kid to go away, now is the time + if ( mChildren.count() == 0 && mWaitingForKids ) { + emit progressItemCompleted( this ); + deleteLater(); + } +} + +void ProgressItem::cancel() +{ + if ( mCanceled || !mCanBeCanceled ) return; + kdDebug(5300) << "ProgressItem::cancel() - " << label() << endl; + mCanceled = true; + // Cancel all tqchildren. + TQValueList<ProgressItem*> kids = mChildren.keys(); + TQValueList<ProgressItem*>::Iterator it( kids.begin() ); + TQValueList<ProgressItem*>::Iterator end( kids.end() ); + for ( ; it != end; it++ ) { + ProgressItem *kid = *it; + if ( kid->canBeCanceled() ) + kid->cancel(); + } + setqStatus( i18n( "Aborting..." ) ); + emit progressItemCanceled( this ); +} + + +void ProgressItem::setProgress( unsigned int v ) +{ + mProgress = v; + // kdDebug(5300) << "ProgressItem::setProgress(): " << label() << " : " << v << endl; + emit progressItemProgress( this, mProgress ); +} + +void ProgressItem::setLabel( const TQString& v ) +{ + mLabel = v; + emit progressItemLabel( this, mLabel ); +} + +void ProgressItem::setqStatus( const TQString& v ) +{ + mtqStatus = v; + emit progressItemtqStatus( this, mtqStatus ); +} + +void ProgressItem::setUsesCrypto( bool v ) +{ + mUsesCrypto = v; + emit progressItemUsesCrypto( this, v ); +} + +void ProgressItem::setUsesBusyIndicator( bool useBusyIndicator ) +{ + mUsesBusyIndicator = useBusyIndicator; + emit progressItemUsesBusyIndicator( this, useBusyIndicator ); +} + +// ====================================== + +ProgressManager::ProgressManager() :TQObject() { + mInstance = this; +} + +ProgressManager::~ProgressManager() { mInstance = 0; } +static KStaticDeleter<ProgressManager> progressManagerDeleter; + +ProgressManager* ProgressManager::instance() +{ + if ( !mInstance ) { + progressManagerDeleter.setObject( mInstance, new ProgressManager() ); + } + return mInstance; +} + +ProgressItem* ProgressManager::createProgressItemImpl( + ProgressItem* parent, const TQString& id, + const TQString &label, const TQString &status, + bool cancellable, bool usesCrypto ) +{ + ProgressItem *t = 0; + if ( !mTransactions[ id ] ) { + t = new ProgressItem ( parent, id, label, status, cancellable, usesCrypto ); + mTransactions.insert( id, t ); + if ( parent ) { + ProgressItem *p = mTransactions[ parent->id() ]; + if ( p ) { + p->addChild( t ); + } + } + // connect all signals + connect ( t, TQT_SIGNAL( progressItemCompleted(KPIM::ProgressItem*) ), + this, TQT_SLOT( slotTransactionCompleted(KPIM::ProgressItem*) ) ); + connect ( t, TQT_SIGNAL( progressItemProgress(KPIM::ProgressItem*, unsigned int) ), + this, TQT_SIGNAL( progressItemProgress(KPIM::ProgressItem*, unsigned int) ) ); + connect ( t, TQT_SIGNAL( progressItemAdded(KPIM::ProgressItem*) ), + this, TQT_SIGNAL( progressItemAdded(KPIM::ProgressItem*) ) ); + connect ( t, TQT_SIGNAL( progressItemCanceled(KPIM::ProgressItem*) ), + this, TQT_SIGNAL( progressItemCanceled(KPIM::ProgressItem*) ) ); + connect ( t, TQT_SIGNAL( progressItemtqStatus(KPIM::ProgressItem*, const TQString&) ), + this, TQT_SIGNAL( progressItemtqStatus(KPIM::ProgressItem*, const TQString&) ) ); + connect ( t, TQT_SIGNAL( progressItemLabel(KPIM::ProgressItem*, const TQString&) ), + this, TQT_SIGNAL( progressItemLabel(KPIM::ProgressItem*, const TQString&) ) ); + connect ( t, TQT_SIGNAL( progressItemUsesCrypto(KPIM::ProgressItem*, bool) ), + this, TQT_SIGNAL( progressItemUsesCrypto(KPIM::ProgressItem*, bool) ) ); + connect ( t, TQT_SIGNAL( progressItemUsesBusyIndicator(KPIM::ProgressItem*, bool) ), + this, TQT_SIGNAL( progressItemUsesBusyIndicator(KPIM::ProgressItem*, bool) ) ); + + emit progressItemAdded( t ); + } else { + // Hm, is this what makes the most sense? + t = mTransactions[id]; + } + return t; +} + +ProgressItem* ProgressManager::createProgressItemImpl( + const TQString& parent, const TQString &id, + const TQString &label, const TQString& status, + bool canBeCanceled, bool usesCrypto ) +{ + ProgressItem * p = mTransactions[parent]; + return createProgressItemImpl( p, id, label, status, canBeCanceled, usesCrypto ); +} + +void ProgressManager::emitShowProgressDialogImpl() +{ + emit showProgressDialog(); +} + + +// slots + +void ProgressManager::slotTransactionCompleted( ProgressItem *item ) +{ + mTransactions.remove( item->id() ); + emit progressItemCompleted( item ); +} + +void ProgressManager::slotStandardCancelHandler( ProgressItem *item ) +{ + item->setComplete(); +} + +ProgressItem* ProgressManager::singleItem() const +{ + ProgressItem *item = 0; + TQDictIterator< ProgressItem > it( mTransactions ); + for ( ; it.current(); ++it ) { + + // No single item for progress possible, as one of them is a busy indicator one. + if ( (*it)->usesBusyIndicator() ) + return 0; + + if ( !(*it)->parent() ) { // if it's a top level one, only those count + if ( item ) + return 0; // we found more than one + else + item = (*it); + } + } + return item; +} + +void ProgressManager::slotAbortAll() +{ + TQDictIterator< ProgressItem > it( mTransactions ); + for ( ; it.current(); ++it ) { + it.current()->cancel(); + } +} + +} // namespace + +#include "progressmanager.moc" diff --git a/libtdepim/progressmanager.h b/libtdepim/progressmanager.h new file mode 100644 index 000000000..a447d49fb --- /dev/null +++ b/libtdepim/progressmanager.h @@ -0,0 +1,438 @@ +/* + progressmanager.h + + This file is part of KDEPIM. + + Author: Till Adam <adam@kde.org> (C) 2004 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef __KPIM_PROGRESSMANAGER_H__ +#define __KPIM_PROGRESSMANAGER_H__ + +#include <tqobject.h> +#include <tqdict.h> +#include <tqstring.h> + +#include <tdepimmacros.h> + +namespace KPIM { + +class ProgressItem; +class ProgressManager; +typedef TQMap<ProgressItem*, bool> ProgressItemMap; + +class KDE_EXPORT ProgressItem : public TQObject +{ + Q_OBJECT + TQ_OBJECT + friend class ProgressManager; + friend class TQDict< ProgressItem >; // so it can be deleted from dicts + + public: + + /** + * @return The id string which uniquely identifies the operation + * represented by this item. + */ + const TQString& id() const { return mId; } + + /** + * @return The parent item of this one, if there is one. + */ + ProgressItem *parent() const { return mParent; } + + /** + * @return The user visible string to be used to represent this item. + */ + const TQString& label() const { return mLabel; } + + /** + * @param v Set the user visible string identifying this item. @p v will + be interpreted as rich text, so it might have to be escaped. + */ + void setLabel( const TQString& v ); + + /** + * @return The string to be used for showing this item's current status. + */ + const TQString& status() const { return mtqStatus; } + /** + * Set the string to be used for showing this item's current status. + * @p v will be interpreted as rich text, so it might have to be escaped. + * @param v The status string. + */ + void setqStatus( const TQString& v ); + + /** + * @return Whether this item can be cancelled. + */ + bool canBeCanceled() const { return mCanBeCanceled; } + + /** + * @return Whether this item uses secure communication + * (Account uses ssl, for example.). + */ + bool usesCrypto() const { return mUsesCrypto; } + + /** + * Set whether this item uses crypted communication, so listeners + * can display a nice crypto icon. + * @param v The value. + */ + void setUsesCrypto( bool v ); + + /** + * @return whether this item uses a busy indicator instead of real progress display + */ + bool usesBusyIndicator() const { return mUsesBusyIndicator; } + + /** + * Sets whether this item uses a busy indicator instead of real progress for its progress bar. + * If it uses a busy indicator, you are still responsible for calling setProgress() from time to + * time to update the busy indicator. + */ + void setUsesBusyIndicator( bool useBusyIndicator ); + + /** + * @return The current progress value of this item in percent. + */ + unsigned int progress() const { return mProgress; } + + /** + * Set the progress (percentage of completion) value of this item. + * @param v The percentage value. + */ + void setProgress( unsigned int v ); + + /** + * Tell the item it has finished. This will emit progressItemCompleted() + * result in the destruction of the item after all slots connected to this + * signal have executed. This is the only way to get rid of an item and + * needs to be called even if the item is cancelled. Don't use the item + * after this has been called on it. + */ + void setComplete(); + + /** + * Reset the progress value of this item to 0 and the status string to + * the empty string. + */ + void reset() { setProgress( 0 ); setqStatus( TQString() ); mCompleted = 0; } + + void cancel(); + + // Often needed values for calculating progress. + void setTotalItems( unsigned int v ) { mTotal = v; } + unsigned int totalItems() const { return mTotal; } + void setCompletedItems( unsigned int v ) { mCompleted = v; } + void incCompletedItems( unsigned int v = 1 ) { mCompleted += v; } + unsigned int completedItems() const { return mCompleted; } + + /** + * Recalculate progress according to total/completed items and update. + */ + void updateProgress() { setProgress( mTotal? mCompleted*100/mTotal: 0 ); } + + void addChild( ProgressItem *kiddo ); + void removeChild( ProgressItem *kiddo ); + + bool canceled() const { return mCanceled; } + +signals: + /** + * Emitted when a new ProgressItem is added. + * @param The ProgressItem that was added. + */ + void progressItemAdded( KPIM::ProgressItem* ); + /** + * Emitted when the progress value of an item changes. + * @param The item which got a new value. + * @param The value, for convenience. + */ + void progressItemProgress( KPIM::ProgressItem*, unsigned int ); + /** + * Emitted when a progress item was completed. The item will be + * deleted afterwards, so slots connected to this are the last + * chance to work with this item. + * @param The completed item. + */ + void progressItemCompleted( KPIM::ProgressItem* ); + /** + * Emitted when an item was cancelled. It will _not_ go away immediately, + * only when the owner sets it complete, which will usually happen. Can be + * used to visually indicate the cancelled status of an item. Should be used + * by the owner of the item to make sure it is set completed even if it is + * cancelled. There is a ProgressManager::slotStandardCancelHandler which + * simply sets the item completed and can be used if no other work needs to + * be done on cancel. + * @param The canceled item; + */ + void progressItemCanceled( KPIM::ProgressItem* ); + /** + * Emitted when the status message of an item changed. Should be used by + * progress dialogs to update the status message for an item. + * @param The updated item. + * @param The new message. + */ + void progressItemtqStatus( KPIM::ProgressItem*, const TQString& ); + /** + * Emitted when the label of an item changed. Should be used by + * progress dialogs to update the label of an item. + * @param The updated item. + * @param The new label. + */ + void progressItemLabel( KPIM::ProgressItem*, const TQString& ); + /** + * Emitted when the crypto status of an item changed. Should be used by + * progress dialogs to update the crypto indicator of an item. + * @param The updated item. + * @param The new state. + */ + void progressItemUsesCrypto( KPIM::ProgressItem*, bool ); + + /** + * Emitted when the busy indicator state of an item changes. Should be used + * by progress dialogs so that they can adjust the display of the progress bar + * to the new mode. + * @param item The updated item + * @param value True if the item uses a busy indicator now, false otherwise + */ + void progressItemUsesBusyIndicator( KPIM::ProgressItem *item, bool value ); + + + protected: + /* Only to be used by our good friend the ProgressManager */ + ProgressItem( ProgressItem* parent, + const TQString& id, + const TQString& label, + const TQString& status, + bool isCancellable, + bool usesCrypto ); + virtual ~ProgressItem(); + + + private: + TQString mId; + TQString mLabel; + TQString mtqStatus; + ProgressItem* mParent; + bool mCanBeCanceled; + unsigned int mProgress; + ProgressItemMap mChildren; + unsigned int mTotal; + unsigned int mCompleted; + bool mWaitingForKids; + bool mCanceled; + bool mUsesCrypto; + bool mUsesBusyIndicator; +}; + +/** + * The ProgressManager singleton keeps track of all ongoing transactions + * and notifies observers (progress dialogs) when their progress percent value + * changes, when they are completed (by their owner), and when they are canceled. + * Each ProgressItem emits those signals individually and the singleton + * broadcasts them. Use the ::createProgressItem() statics to acquire an item + * and then call ->setProgress( int percent ) on it everytime you want to update + * the item and ->setComplete() when the operation is done. This will delete the + * item. Connect to the item's progressItemCanceled() signal to be notified when + * the user cancels the transaction using one of the observing progress dialogs + * or by calling item->cancel() in some other way. The owner is responsible for + * calling setComplete() on the item, even if it is canceled. Use the + * standardCancelHandler() slot if that is all you want to do on cancel. + * + * Note that if you request an item with a certain id and there is already + * one with that id, there will not be a new one created but the existing + * one will be returned. This is convenient for accessing items that are + * needed regularly without the to store a pointer to them or to add child + * items to parents by id. + */ + +class KDE_EXPORT ProgressManager : public TQObject +{ + + Q_OBJECT + TQ_OBJECT + + public: + virtual ~ProgressManager(); + + /** + * @return The singleton instance of this class. + */ + static ProgressManager * instance(); + + /** + * Use this to aquire a unique id number which can be used to discern + * an operation from all others going on at the same time. Use that + * number as the id string for your progressItem to ensure it is unique. + * @return + */ + static TQString getUniqueID() { return TQString::number( ++uID ); } + + /** + * Creates a ProgressItem with a unique id and the given label. + * This is the simplest way to aquire a progress item. It will not + * have a parent and will be set to be cancellable and not using crypto. + * + * @param label The text to be displayed by progress handlers. It will be + * interpreted as rich text, so it might have to be escaped. + */ + static ProgressItem * createProgressItem( const TQString &label ) { + return instance()->createProgressItemImpl( 0, getUniqueID(), label, + TQString(), true, false ); + } + + /** + * Creates a new progressItem with the given parent, id, label and initial + * status. + * + * @param parent Specify an already existing item as the parent of this one. + * @param id Used to identify this operation for cancel and progress info. + * @param label The text to be displayed by progress handlers. It will be + * interpreted as rich text, so it might have to be escaped. + * @param status Additional text to be displayed for the item. It will be + * interpreted as rich text, so it might have to be escaped. + * @param canBeCanceled can the user cancel this operation? + * @param usesCrypto does the operation use secure transports (SSL) + * Cancelling the parent will cancel the tqchildren as well (if they can be + * cancelled) and ongoing tqchildren prevent parents from finishing. + * @return The ProgressItem representing the operation. + */ + static ProgressItem * createProgressItem( ProgressItem* parent, + const TQString& id, + const TQString& label, + const TQString& status = TQString(), + bool canBeCanceled = true, + bool usesCrypto = false ) { + return instance()->createProgressItemImpl( parent, id, label, status, + canBeCanceled, usesCrypto ); + } + + /** + * Use this version if you have the id string of the parent and want to + * add a subjob to it. + */ + static ProgressItem * createProgressItem( const TQString& parent, + const TQString& id, + const TQString& label, + const TQString& status = TQString(), + bool canBeCanceled = true, + bool usesCrypto = false ) { + return instance()->createProgressItemImpl( parent, id, label, + status, canBeCanceled, usesCrypto ); + } + + /** + * Version without a parent. + */ + static ProgressItem * createProgressItem( const TQString& id, + const TQString& label, + const TQString& status = TQString(), + bool canBeCanceled = true, + bool usesCrypto = false ) { + return instance()->createProgressItemImpl( 0, id, label, status, + canBeCanceled, usesCrypto ); + } + + + /** + * @return true when there is no more progress item + */ + bool isEmpty() const { return mTransactions.isEmpty(); } + + /** + * @return the only top level progressitem when there's only one. + * Returns 0 if there is no item, or more than one top level item. + * Since this is used to calculate the overall progress, it will also return + * 0 if there is an item which uses a busy indicator, since that will tqinvalidate + * the overall progress. + */ + ProgressItem* singleItem() const; + + /** + * Ask all listeners to show the progress dialog, because there is + * something that wants to be shown. + */ + static void emitShowProgressDialog() { + instance()->emitShowProgressDialogImpl(); + } + + signals: + /** @see ProgressItem::progressItemAdded() */ + void progressItemAdded( KPIM::ProgressItem* ); + /** @see ProgressItem::progressItemProgress() */ + void progressItemProgress( KPIM::ProgressItem*, unsigned int ); + /** @see ProgressItem::progressItemCompleted() */ + void progressItemCompleted( KPIM::ProgressItem* ); + /** @see ProgressItem::progressItemCanceled() */ + void progressItemCanceled( KPIM::ProgressItem* ); + /** @see ProgressItem::progressItemtqStatus() */ + void progressItemtqStatus( KPIM::ProgressItem*, const TQString& ); + /** @see ProgressItem::progressItemLabel() */ + void progressItemLabel( KPIM::ProgressItem*, const TQString& ); + /** @see ProgressItem::progressItemUsesCrypto() */ + void progressItemUsesCrypto( KPIM::ProgressItem*, bool ); + /** @see ProgressItem::progressItemUsesBusyIndicator */ + void progressItemUsesBusyIndicator( KPIM::ProgressItem*, bool ); + + /** + * Emitted when an operation requests the listeners to be shown. + * Use emitShowProgressDialog() to trigger it. + */ + void showProgressDialog(); + public slots: + + /** + * Calls setCompleted() on the item, to make sure it goes away. + * Provided for convenience. + * @param item the canceled item. + */ + void slotStandardCancelHandler( KPIM::ProgressItem* item ); + + /** + * Aborts all running jobs. Bound to "Esc" + */ + void slotAbortAll(); + + private slots: + void slotTransactionCompleted( KPIM::ProgressItem *item ); + + private: + ProgressManager(); + // prevent unsolicited copies + ProgressManager( const ProgressManager& ); + + virtual ProgressItem* createProgressItemImpl( + ProgressItem* parent, const TQString& id, + const TQString& label, const TQString& status, + bool cancellable, bool usesCrypto ); + virtual ProgressItem* createProgressItemImpl( + const TQString& parent, const TQString& id, + const TQString& label, const TQString& status, + bool cancellable, bool usesCrypto ); + void emitShowProgressDialogImpl(); + + TQDict< ProgressItem > mTransactions; + static ProgressManager *mInstance; + static unsigned int uID; +}; + +} + +#endif // __KPIM_PROGRESSMANAGER_H__ diff --git a/libtdepim/qutf7codec.cpp b/libtdepim/qutf7codec.cpp new file mode 100644 index 000000000..dc8ef2a2f --- /dev/null +++ b/libtdepim/qutf7codec.cpp @@ -0,0 +1,550 @@ +/* + qutf7codec.cpp + + A TQTextCodec for UTF-7 (rfc2152). + Copyright (c) 2001 Marc Mutz <mutz@kde.org> + See file COPYING for details + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License, version 2.0, + as published by the Free Software Foundation. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, US + + As a special exception, permission is granted to use this plugin + with any version of TQt by TrollTech AS, Norway. In this case, the + use of this plugin doesn't cause the resulting executable to be + covered by the GNU General Public License. + This exception does not however tqinvalidate any other reasons why the + executable file might be covered by the GNU General Public License. +*/ + + +#include "qutf7codec.h" + +#ifndef TQT_NO_TEXTCODEC + +int TQUtf7Codec::mibEnum() const { + return 1012; +} + +int TQStrictUtf7Codec::mibEnum() const { + return -1012; +} + +const char* TQUtf7Codec::name() const { + return "UTF-7"; +} + +const char* TQStrictUtf7Codec::name() const { + return "X-QT-UTF-7-STRICT"; +} + +const char* TQUtf7Codec::mimeName() const { + return "UTF-7"; +} + +bool TQUtf7Codec::canEncode( TQChar ) const { + return TRUE; +} + +bool TQUtf7Codec::canEncode( const TQString & ) const { + return TRUE; +} + +static uchar base64Set[] = { + 0x00, 0x00, 0x00, 0x00, // '\0' ... + 0x00, 0x11, 0xFF, 0xC0, // ' ' ... '?' + 0x7F, 0xFF, 0xFF, 0xE0, // '@' ... '_' + 0x7F, 0xFF, 0xFF, 0xE0 // '`' ... DEL +}; + +static uchar base64SetWithLastTwoBitsZero[] = { + 0x00, 0x00, 0x00, 0x00, // '\0' ... + 0x00, 0x00, 0x88, 0x80, // ' ' ... '?' + 0x44, 0x44, 0x44, 0x40, // '@' ... '_' + 0x11, 0x11, 0x11, 0x00 // '`' ... DEL +}; + +static uchar directSet[] = { + 0x00, 0x00, 0x00, 0x00, // '\0' ... + 0x01, 0xCF, 0xFF, 0xE1, // ' ' ... '?' + 0x7F, 0xFF, 0xFF, 0xE0, // '@' ... '_' + 0x7F, 0xFF, 0xFF, 0xE0 // '`' ... DEL +}; + +static uchar optDirectSet[] = { + 0x00, 0x00, 0x00, 0x00, // '\0' ... + 0x7E, 0x20, 0x00, 0x1E, // ' ' ... '?' + 0x80, 0x00, 0x00, 0x17, // '@' ... '_' + 0x80, 0x00, 0x00, 0x1C // '`' ... DEL +}; + +static inline bool isOfSet(uchar ch, uchar* set) { + return set[ ch/8 ] & (0x80 >> ( ch%8 )); +} + +int TQUtf7Codec::heuristicContentMatch(const char* chars, int len) const +{ + int stepNo = 0; + int i; + bool shifted = FALSE; + bool rightAfterEscape = FALSE; + bool onlyNullBitsSinceLastBoundary = TRUE; + for ( i = 0; i < len ; i++ ) { + if ((unsigned char)chars[i] >= 128) // 8bit chars not allowed. + break; + if (shifted) { + if ( isOfSet(chars[i],base64Set) ) { + switch (stepNo) { + case 0: + onlyNullBitsSinceLastBoundary = TRUE; + break; + case 3: + onlyNullBitsSinceLastBoundary + = isOfSet(chars[i],base64SetWithLastTwoBitsZero); + break; + case 6: + onlyNullBitsSinceLastBoundary + = ( chars[i] == 'A' || chars[i] == 'Q' || + chars[i] == 'g' || chars[i] == 'w' ); + break; + default: + onlyNullBitsSinceLastBoundary + = onlyNullBitsSinceLastBoundary && (chars[i] == 'A'); + } + stepNo = (stepNo + 1) % 8; + rightAfterEscape = FALSE; + } else { + if (rightAfterEscape && chars[i] != '-') + break; // a '+' must be followed by '-' or a base64 char + if (!onlyNullBitsSinceLastBoundary) + break; // non-zero bits in the tail of the base64 encoding + shifted = FALSE; + stepNo = 0; + } + } else { + if (chars[i] == '+') { + shifted = TRUE; + rightAfterEscape = TRUE; + } + } + } + return i; +} + +class TQUtf7Decoder : public TQTextDecoder { + // the storage for our tqunicode char until it's finished + ushort uc; + // the state of the base64 decoding + // can be 0 (just finished three tqunicode chars) + // 1 (have the upper 6 bits of uc already) + // 2 (have the upper 12 bits of uc already) + // 3 (have the upper 2 bits of uc already) + // .......... + // 7 (have the upper 10 bits of uc already) + // => n (have the upper (n * 6) % 16 bits of uc already) + // "stepNo" cycles through all it's values every three + // tqunicode chars. + char stepNo; + // remembers if we are in shifted-sequence mode + bool shifted; + // remembers if we're just after the initial '+' + // of a shifted-sequence. + bool rightAfterEscape; +public: + TQUtf7Decoder() : uc(0), stepNo(0), shifted(FALSE), rightAfterEscape(FALSE) + { + } + +private: + inline void resetParser() + { + uc = 0; + stepNo = 0; + shifted = FALSE; + rightAfterEscape = FALSE; + } + +public: + TQString toUnicode(const char* chars, int len) + { + TQString result = ""; + for (int i=0; i<len; i++) { + uchar ch = chars[i]; + + // + // check for 8bit char's: + // + if ( ch > 127 ) { + qWarning("TQUtf7Decoder: 8bit char found in input. " + "Parser has been re-initialized!"); + resetParser(); + result += TQChar::replacement; + continue; + } + + if (shifted) { // in shifted mode + + // + // first, we check specialities that only occur + // right after the escaping '+': + // + if ( rightAfterEscape && ch == '-' ) { + // a "+-" sequence is a short-circuit encoding + // for just '+': + resetParser(); + result += TQChar('+'); + // we're already done for this "ch", so + continue; + } + + // + // Here we're going to extract the bits represented by "ch": + // + ushort bits; + if ( ch >= 'A' && ch <= 'Z' ) { + bits = ch - 'A'; + } else if ( ch >= 'a' && ch <= 'z' ) { + bits = ch - 'a' + 26; + } else if ( ch >= '0' && ch <= '9' ) { + bits = ch - '0' + 52; + } else if ( ch == '+' ) { + bits = 62; + } else if ( ch == '/' ) { + bits = 63; + } else { + bits = 0; // keep compiler happy + + // + // ch is not of the base64 alphabet. + // Here we are going to check the sequence's validity: + // + if ( rightAfterEscape ) { + // any non-base64 char following an escaping '+' + // makes for an ill-formed sequence. + // Note that we catch (the valid) "+-" pair + // right at the beginning. + qWarning("TQUtf7Decoder: ill-formed input: " + "non-base64 char after escaping \"+\"!"); + } + // pending bits from base64 encoding must be all 0: + if (stepNo >= 1 && uc) { + qWarning("TQUtf7Decoder: ill-formed sequence: " + "non-zero bits in shifted-sequence tail!"); + } + resetParser(); + + // a '-' signifies the end of the shifted-sequence, + // so we just swallow it. + if ( ch == '-' ) + continue; + // end of validity checking. Process ch now... + } + + if ( /*still*/ shifted ) { + // + // now we're going to stuff the "bits" bit bucket into + // the right position inside "uc", emitting a resulting + // TQChar if possible. + // + switch (stepNo) { + // "bits" are the 6 msb's of uc + case 0: uc = bits << 10; break; + + case 1: uc |= bits << 4; break; + + // 4 bits of "bits" complete the first ushort + case 2: uc |= bits >> 2; result += TQChar(uc); + // 2 bits of "bits" make the msb's of the next ushort + uc = bits << 14; break; + case 3: uc |= bits << 8; break; + case 4: uc |= bits << 2; break; + + // 2 bits of "bits" complete the second ushort + case 5: uc |= bits >> 4; result += TQChar(uc); + // 4 bits of "bits" make the msb's of the next ushort + uc = bits << 12; break; + case 6: uc |= bits << 6; break; + + // these 6 bits complete the third ushort + // and also one round of 8 chars -> 3 ushort decoding + case 7: uc |= bits; result += TQChar(uc); + uc = 0; break; + default: ; + } // switch (stepNo) + // increase the step counter + stepNo++; + stepNo %= 8; + rightAfterEscape = FALSE; + // and look at the next char. + continue; + } // fi (still) shifted + } // fi shifted + + // + // if control reaches here, we either weren't in a + // shifted sequence or we just left one by seeing + // a non-base64-char. + // Either way, we have to process "ch" outside + // a shifted-sequence now: + // + if ( ch == '+' ) { + // '+' is the escape char for entering a + // shifted sequence: + shifted = TRUE; + stepNo = 0; + // also, we're right at the beginning where + // special rules apply: + rightAfterEscape = TRUE; + } else { + // US-ASCII values are directly used + result += TQChar(ch); + } + } + + return result; + + } // toUnicode() + +}; // class TQUtf7Decoder + +TQTextDecoder* TQUtf7Codec::makeDecoder() const +{ + return new TQUtf7Decoder; +} + + +class TQUtf7Encoder : public TQTextEncoder { + uchar dontNeedEncodingSet[16]; + ushort outbits; + uint stepNo : 2; + bool shifted : 1; + bool mayContinueShiftedSequence : 1; +public: + TQUtf7Encoder(bool encOpt, bool encLwsp) + : outbits(0), stepNo(0), + shifted(FALSE), mayContinueShiftedSequence(FALSE) + { + for ( int i = 0; i < 16 ; i++) { + dontNeedEncodingSet[i] = directSet[i]; + if (!encOpt) + dontNeedEncodingSet[i] |= optDirectSet[i]; + } + if(!encLwsp) { + dontNeedEncodingSet[' '/8] |= 0x80 >> (' '%8); + dontNeedEncodingSet['\n'/8] |= 0x80 >> ('\n'%8); + dontNeedEncodingSet['\r'/8] |= 0x80 >> ('\r'%8); + dontNeedEncodingSet['\t'/8] |= 0x80 >> ('\t'%8); + } + } + +private: + + char toBase64( ushort u ) { + if ( u < 26 ) + return (char)u + 'A'; + else if ( u < 52 ) + return (char)u - 26 + 'a'; + else if ( u < 62 ) + return (char)u - 52 + '0'; + else if ( u == 62 ) + return '+'; + else + return '/'; + } + + void addToShiftedSequence(TQCString::Iterator & t, ushort u) { + switch (stepNo) { + // no outbits; use uppermost 6 bits of u + case 0: + *t++ = toBase64( u >> 10 ); + *t++ = toBase64( (u & 0x03FF /* umask top 6 bits */ ) >> 4 ); + // save 4 lowest-order bits in outbits[5..2] + outbits = (u & 0x000F) << 2; + break; + + // outbits available; use top two bits of u to complete + // the previous char + case 1: + if (!mayContinueShiftedSequence) { + // if mayContinue, this char has already been written + *t++ = toBase64( outbits | ( u >> 14 ) ); + } + *t++ = toBase64( (u & 0x3F00 /* mask top 2 bits */ ) >> 8 ); + *t++ = toBase64( (u & 0x00FC /* mask msbyte */ ) >> 2 ); + // save 2 lowest-significant bits in outbits[5..4] + outbits = (u & 0x0003) << 4; + break; + + // outbits available; use top four bits of u to complete + // the previous char + case 2: + if (!mayContinueShiftedSequence) { + // if mayContinue, this char has already been written + *t++ = toBase64( outbits | ( u >> 12 ) ); + } + *t++ = toBase64( (u & 0x0FFF) >> 6 ); + *t++ = toBase64( u & 0x003F ); + break; + + default: ; + } + stepNo = (stepNo + 1) % 3; + } + + void endShiftedSequence(TQCString::Iterator & t) { + switch (stepNo) { + case 1: // four outbits still to be written + case 2: // two outbits still to be written + *t++ = toBase64( outbits ); + break; + case 0: // nothing to do + default: ; + } + outbits = 0; + } + + // depending on the stepNo, checks whether we can continue + // an already ended shifted-sequence with char "u". + // This is only possible if the topmost bits fit the + // already written ones (which are all 0 between calls) + bool continueOK( ushort u ) { + return stepNo == 0 || + ( stepNo == 1 && (u & 0xF000) == 0 ) || + ( stepNo == 2 && (u & 0xC000) == 0 ); + } + + void processDoesntNeedEncoding(TQCString::Iterator & t, ushort ch) { + // doesn't need encoding + if (shifted) { + endShiftedSequence(t); + // add "lead-out" to dis-ambiguate following chars: + if (isOfSet((char)ch,base64Set) || ch == '-' ) { + *t++ = '-'; + } + } else if (mayContinueShiftedSequence) { + // if mayContinue is set, this means the + // shifted-sequence needs a lead-out. + mayContinueShiftedSequence = FALSE; + if (isOfSet(ch,base64Set) || ch == '-' ) { + *t++ = '-'; + } + } + *t++ = (uchar)ch; + shifted = FALSE; + stepNo = 0; + } + +public: + TQCString fromUnicode(const TQString & uc, int & len_in_out) + { + // allocate place for worst case: + // len/2 * (5+1) for an alternating sequence of e.g. "A\", + // + 4 for a worst-case of another +ABC encoded char + // + 1 for the trailing \0 + // + int maxreslen = 3 * len_in_out + 5; + TQCString result( maxreslen ); + +#if 0 + // if (len_in_out == 1) { + cout << "\nlen_in_out: " << len_in_out + <<"; shifted: " << (shifted ? "true" : "false") + << ";\n" << "mayContinue: " + << (mayContinueShiftedSequence ? "true" : "false") + << "; stepNo: " << stepNo << ";\n" + << "outbits: " << outbits << endl; + // } +#endif + + // source and destination cursor + const TQChar * s = uc.tqunicode(); + TQCString::Iterator t = result.data(); + + if ( uc.isNull() ) { + // return to ascii requested: + if ( mayContinueShiftedSequence ) + *t++ = '-'; + } else { + // normal operation: + for (int i = 0 ; i < len_in_out ; + i++/*, checkOutBuf(result,maxreslen,t,i,len_in_out,5)*/ ) { + ushort ch = s[i].tqunicode(); + + // + // first, we check whether we might get around encoding: + // + if ( ch < 128 ) { + // + // ch is usAscii, so we have a chance that we don't + // need to encode it. + // + if ( isOfSet((uchar)ch,dontNeedEncodingSet) ) { + processDoesntNeedEncoding(t,ch); + continue; + } else if ( ch == '+' ) { + // '+' is the shift escape character + if (shifted || mayContinueShiftedSequence) { + // if we are already in shifted mode, we just + // encode the '+', too. Compare + // 24bits ("-+-") + some from ending the shifted-sequence + // with 21,33 bits + addToShiftedSequence(t,ch); + mayContinueShiftedSequence = FALSE; + shifted = TRUE; + } else { + // shortcut encoding of '+': + *t++ = '+'; + *t++ = '-'; + } + continue; // done + } // else fall through to encoding + } + // + // need encoding + // + if (!shifted && (!mayContinueShiftedSequence || !continueOK(ch) ) ) { + *t++ = '+'; + stepNo = 0; + } + addToShiftedSequence(t,ch); + shifted = TRUE; + mayContinueShiftedSequence = FALSE; + } + + if ( shifted ) { + endShiftedSequence(t); + mayContinueShiftedSequence = TRUE; + }; + shifted = FALSE; + } + + *t = '\0'; + len_in_out = t - result.data(); + +#if 0 + cout << "len_in_out: " << len_in_out << "; " + << "mayContinue: " << (mayContinueShiftedSequence ? "true" : "false") + << "; stepNo: " << stepNo << endl; +#endif + + Q_ASSERT(len_in_out <= maxreslen-1); + + return result; + } // fromUnicode() + +}; // class TQUtf7Encoder + +TQTextEncoder* TQUtf7Codec::makeEncoder() const { + return new TQUtf7Encoder( false, false ); +} + +TQTextEncoder* TQStrictUtf7Codec::makeEncoder() const { + return new TQUtf7Encoder( true, false ); +} + +#endif // TQT_NO_TEXTCODEC diff --git a/libtdepim/qutf7codec.h b/libtdepim/qutf7codec.h new file mode 100644 index 000000000..4057d276c --- /dev/null +++ b/libtdepim/qutf7codec.h @@ -0,0 +1,96 @@ +/* + qutf7codec.h + + A TQTextCodec for UTF-7 (rfc2152). + Copyright (c) 2001 Marc Mutz <mutz@kde.org> + See file COPYING for details + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License, version 2.0, + as published by the Free Software Foundation. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, US + + As a special exception, permission is granted to use this plugin + with any version of TQt by TrollTech AS, Norway. In this case, the + use of this plugin doesn't cause the resulting executable to be + covered by the GNU General Public License. + This exception does not however tqinvalidate any other reasons why the + executable file might be covered by the GNU General Public License. +*/ + +#ifndef TQUTF7CODEC_H +#define TQUTF7CODEC_H + +#include "tqtextcodec.h" + +#include <tdepimmacros.h> + +#ifndef TQT_NO_TEXTCODEC + +/** @short A TQTextCodec for the UTF-7 transformation of Unicode. + + This is a TQTextCodec for the UTF-7 transformation of Unicode, + described in RFC2152. + + Use it as you would use any other TQTextCodec. Only if you use the + encoder directly (via makeEncoder), you should bear in mind + that if your application needs the encoder to return to ASCII mode + (like it's the case for RFC2047 mail header encoded words), you + have to tell the encoder by requesting the encoding of a @em null + TQString. + + @author Marc Mutz <mutz@kde.org> */ + +class KDE_EXPORT TQUtf7Codec : public TQTextCodec { + bool encOpt, encLwsp; +public: + TQUtf7Codec() : TQTextCodec() {} + + int mibEnum() const; + const char* name() const; + const char* mimeName() const; + + TQTextDecoder* makeDecoder() const; + TQTextEncoder* makeEncoder() const; + + bool canEncode( TQChar ) const; + bool canEncode( const TQString& ) const; + + int heuristicContentMatch( const char* chars, int len ) const; +}; + +/** This is a version of @ref TQUtf7Codec, which should only be used in + MIME transfer. It differs from @ref TQUtf7Codec only in that the + encoder escapes additional characters (the RFC2152 "optional + direct set"), which might not be allowed in RFC822/RFC2047 header + fields. + + You should only use this codec for @em encoding, since it's output + is pure UTF-7 and can equally well be decoded by @ref TQUtf7Codec's + decoder. + + To distinguish between the two variants, this class has MIB enum + -1012 (the nagative of UTF-7) and the somewhat awkward name + "X-QT-UTF-7-STRICT". The MIME preferred charset name is still + "UTF-7", though. + + @short A variant of @ref TQUtf7Codec, which protectes certain + characters in MIME transport + @author Marc Mutz <mutz@kde.org> */ +class KDE_EXPORT TQStrictUtf7Codec : public TQUtf7Codec { +public: + TQStrictUtf7Codec() : TQUtf7Codec() {} + + const char* name() const; + int mibEnum() const; + + TQTextEncoder* makeEncoder() const; +}; + +#endif // TQT_NO_TEXTCODEC + +#endif // TQUTF7CODEC_H diff --git a/libtdepim/qutf7codecplugin.cpp b/libtdepim/qutf7codecplugin.cpp new file mode 100644 index 000000000..49eb92c8f --- /dev/null +++ b/libtdepim/qutf7codecplugin.cpp @@ -0,0 +1,62 @@ +/* + qutf7codecplugin.cpp + + A TQTextCodec for UTF-7 (rfc2152). + Copyright (c) 2001 Marc Mutz <mutz@kde.org> + See file COPYING for details + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License, version 2.0, + as published by the Free Software Foundation. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, US + + As a special exception, permission is granted to use this plugin + with any version of TQt by TrollTech AS, Norway. In this case, the + use of this plugin doesn't cause the resulting executable to be + covered by the GNU General Public License. + This exception does not however tqinvalidate any other reasons why the + executable file might be covered by the GNU General Public License. +*/ + +#include "qutf7codec.h" + +#include <tqtextcodecplugin.h> +#include <tqstring.h> +#include <tqstringlist.h> +#include <tqvaluelist.h> + +class TQTextCodec; + +// ######### This file isn't compiled currently + +class TQUtf7CodecPlugin : public TQTextCodecPlugin { +public: + TQUtf7CodecPlugin() {} + + TQStringList names() const { return TQStringList() << "UTF-7" << "X-QT-UTF-7-STRICT"; } + TQValueList<int> mibEnums() const { return TQValueList<int>() << 1012 << -1012; } + TQTextCodec * createForMib( int ); + TQTextCodec * createForName( const TQString & ); +}; + +TQTextCodec * TQUtf7CodecPlugin::createForMib( int mib ) { + if ( mib == 1012 ) + return new TQUtf7Codec(); + else if ( mib == -1012 ) + return new TQStrictUtf7Codec(); + return 0; +} + +TQTextCodec * TQUtf7CodecPlugin::createForName( const TQString & name ) { + if ( name == "UTF-7" ) + return new TQUtf7Codec(); + else if ( name == "X-QT-UTF-7-STRICT" ) + return new TQStrictUtf7Codec(); + return 0; +} + +KDE_TQ_EXPORT_PLUGIN( TQUtf7CodecPlugin ); diff --git a/libtdepim/recentaddresses.cpp b/libtdepim/recentaddresses.cpp new file mode 100644 index 000000000..ac790693d --- /dev/null +++ b/libtdepim/recentaddresses.cpp @@ -0,0 +1,181 @@ +/* -*- mode: C++; c-file-style: "gnu" -*- + * + * Copyright (c) 2001-2003 Carsten Pfeiffer <pfeiffer@kde.org> + * Copyright (c) 2003 Zack Rusin <zack@kde.org> + * + * KMail is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * KMail 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of this program with any edition of + * the TQt library by Trolltech AS, Norway (or with modified versions + * of TQt that use the same license as TQt), and distribute linked + * combinations including the two. You must obey the GNU General + * Public License in all respects for all of the code used other than + * TQt. If you modify this file, you may extend this exception to + * your version of the file, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from + * your version. + */ +#include "recentaddresses.h" +#include "libemailfunctions/email.h" + +#include <kstaticdeleter.h> +#include <kconfig.h> +#include <kglobal.h> + +#include <kdebug.h> +#include <klocale.h> +#include <keditlistbox.h> + + +#include <tqlayout.h> + +using namespace KRecentAddress; + +static KStaticDeleter<RecentAddresses> sd; + +RecentAddresses * RecentAddresses::s_self = 0; + +RecentAddresses * RecentAddresses::self( KConfig *config) +{ + if ( !s_self ) + sd.setObject( s_self, new RecentAddresses(config) ); + return s_self; +} + +RecentAddresses::RecentAddresses(KConfig * config) +{ + if ( !config ) + load( KGlobal::config() ); + else + load( config ); +} + +RecentAddresses::~RecentAddresses() +{ + // if you want this destructor to get called, use a KStaticDeleter + // on s_self +} + +void RecentAddresses::load( KConfig *config ) +{ + TQStringList addresses; + TQString name; + TQString email; + + m_addresseeList.clear(); + KConfigGroupSaver cs( config, "General" ); + m_maxCount = config->readNumEntry( "Maximum Recent Addresses", 40 ); + addresses = config->readListEntry( "Recent Addresses" ); + for ( TQStringList::Iterator it = addresses.begin(); it != addresses.end(); ++it ) { + KABC::Addressee::parseEmailAddress( *it, name, email ); + if ( !email.isEmpty() ) { + KABC::Addressee addr; + addr.setNameFromString( name ); + addr.insertEmail( email, true ); + m_addresseeList.append( addr ); + } + } + + adjustSize(); +} + +void RecentAddresses::save( KConfig *config ) +{ + KConfigGroupSaver cs( config, "General" ); + config->writeEntry( "Recent Addresses", addresses() ); +} + +void RecentAddresses::add( const TQString& entry ) +{ + if ( !entry.isEmpty() && m_maxCount > 0 ) { + TQStringList list = KPIM::splitEmailAddrList( entry ); + for( TQStringList::const_iterator e_it = list.begin(); e_it != list.end(); ++e_it ) { + KPIM::EmailParseResult errorCode = KPIM::isValidEmailAddress( *e_it ); + if ( errorCode != KPIM::AddressOk ) + continue; + TQString email; + TQString fullName; + KABC::Addressee addr; + + KABC::Addressee::parseEmailAddress( *e_it, fullName, email ); + + for ( KABC::Addressee::List::Iterator it = m_addresseeList.begin(); + it != m_addresseeList.end(); ++it ) + { + if ( email == (*it).preferredEmail() ) { + //already inside, remove it here and add it later at pos==1 + m_addresseeList.remove( it ); + break; + } + } + addr.setNameFromString( fullName ); + addr.insertEmail( email, true ); + m_addresseeList.prepend( addr ); + adjustSize(); + } + } +} + +void RecentAddresses::setMaxCount( int count ) +{ + m_maxCount = count; + adjustSize(); +} + +void RecentAddresses::adjustSize() +{ + while ( m_addresseeList.count() > m_maxCount ) + m_addresseeList.remove( m_addresseeList.fromLast() ); +} + +void RecentAddresses::clear() +{ + m_addresseeList.clear(); + adjustSize(); +} + +TQStringList RecentAddresses::addresses() const +{ + TQStringList addresses; + for ( KABC::Addressee::List::ConstIterator it = m_addresseeList.begin(); + it != m_addresseeList.end(); ++it ) + { + addresses.append( (*it).fullEmail() ); + } + return addresses; +} + +RecentAddressDialog::RecentAddressDialog( TQWidget *parent, const char *name ) + : KDialogBase( Plain, i18n( "Edit Recent Addresses" ), Ok | Cancel, Ok, + parent, name, true ) +{ + TQWidget *page = plainPage(); + TQVBoxLayout *tqlayout = new TQVBoxLayout( page, 0, spacingHint() ); + + mEditor = new KEditListBox( i18n( "Recent Addresses" ), page, "", false, + KEditListBox::Add | KEditListBox::Remove ); + tqlayout->addWidget( mEditor ); +} + +void RecentAddressDialog::setAddresses( const TQStringList &addrs ) +{ + mEditor->clear(); + mEditor->insertStringList( addrs ); +} + +TQStringList RecentAddressDialog::addresses() const +{ + return mEditor->items(); +} diff --git a/libtdepim/recentaddresses.h b/libtdepim/recentaddresses.h new file mode 100644 index 000000000..446f6c684 --- /dev/null +++ b/libtdepim/recentaddresses.h @@ -0,0 +1,132 @@ +/* -*- mode: C++; c-file-style: "gnu" -*- + * + * Copyright (c) 2001-2003 Carsten Pfeiffer <pfeiffer@kde.org> + * Copyright (c) 2003 Zack Rusin <zack@kde.org> + * + * KMail is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * KMail 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of this program with any edition of + * the TQt library by Trolltech AS, Norway (or with modified versions + * of TQt that use the same license as TQt), and distribute linked + * combinations including the two. You must obey the GNU General + * Public License in all respects for all of the code used other than + * TQt. If you modify this file, you may extend this exception to + * your version of the file, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from + * your version. + */ +#ifndef RECENTADDRESSES_H +#define RECENTADDRESSES_H + +#include <tqstringlist.h> +#include <kabc/addressee.h> + +#include <kdialogbase.h> + +#include <tdepimmacros.h> + +class KConfig; +class KEditListBox; +namespace KRecentAddress { + +class KDE_EXPORT RecentAddressDialog : public KDialogBase +{ + public: + RecentAddressDialog( TQWidget *parent, const char *name = 0 ); + void setAddresses( const TQStringList &addrs ); + TQStringList addresses() const; + private: + KEditListBox *mEditor; +}; + +/** + * Handles a list of "recent email-addresses". Simply set a max-count and + * call @ref add() to add entries. + * + * @author Carsten Pfeiffer <pfeiffer@kde.org> + */ + +class KDE_EXPORT RecentAddresses +{ +public: + ~RecentAddresses(); + /** + * @returns the only possible instance of this class. + */ + static RecentAddresses * self(KConfig *config = 0L); + + /* + * @return true if self() was called, i.e. a RecentAddresses instance exists + */ + static bool exists() { return s_self != 0; } + + /** + * @returns the list of recent addresses. + * Note: an entry doesn't have to be one email address, it can be multiple, + * like "Foo <foo@bar.org>, Bar Baz <bar@baz.org>". + */ + TQStringList addresses() const; + const KABC::Addressee::List& kabcAddresses() const { return m_addresseeList; } + + /** + * Adds an entry to the list. + * Note: an entry doesn't have to be one email address, it can be multiple, + * like "Foo <foo@bar.org>, Bar Baz <bar@baz.org>". + */ + void add( const TQString& entry ); + + /** + * Sets the maximum number, the list can hold. The list adjusts to this + * size if necessary. Default maximum is 40. + */ + void setMaxCount( int count ); + + /** + * @returns the current maximum number of entries. + */ + uint maxCount() const { return m_maxCount; } + + /** + * Loads the list of recently used addresses from the configfile. + * Automatically done on startup. + */ + void load( KConfig * ); + + /** + * Saves the list of recently used addresses to the configfile. + * Make sure to call KGlobal::config()->sync() afterwards, to really save. + */ + void save( KConfig * ); + + /** + * Removes all entries from the history. + */ + void clear(); + +private: + RecentAddresses(KConfig *config = 0L); + + KABC::Addressee::List m_addresseeList; + + void adjustSize(); + + uint m_maxCount; + + static RecentAddresses *s_self; +}; + +} + +#endif // KMRECENTADDR_H diff --git a/libtdepim/resourceabc.cpp b/libtdepim/resourceabc.cpp new file mode 100644 index 000000000..dbcc98f87 --- /dev/null +++ b/libtdepim/resourceabc.cpp @@ -0,0 +1,38 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Bo Thorsen <bo@sonofthor.dk> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + As a special exception, permission is given to link this program + with any edition of TQt, and distribute the resulting executable, + without including the source code for TQt in the source distribution. +*/ + +#include "resourceabc.h" + +using namespace KPIM; + +ResourceABC::ResourceABC( const KConfig* config ) + : KABC::Resource( config ) +{ +} + +ResourceABC::~ResourceABC() +{ +} + +#include "resourceabc.moc" diff --git a/libtdepim/resourceabc.h b/libtdepim/resourceabc.h new file mode 100644 index 000000000..b6ef3ec98 --- /dev/null +++ b/libtdepim/resourceabc.h @@ -0,0 +1,116 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Bo Thorsen <bo@sonofthor.dk> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + As a special exception, permission is given to link this program + with any edition of TQt, and distribute the resulting executable, + without including the source code for TQt in the source distribution. +*/ + +#ifndef RESOURCEABC_H +#define RESOURCEABC_H + +#include <kabc/resource.h> +#include <tqmap.h> +#include <tdepimmacros.h> + +// This is here because it can't go to kdelibs/kabc yet, but ultimately +// it should probably go there (maybe redesigned to have a real object +// for subresources). + +namespace KPIM { + +/** + * This class is the implementation of subfolder resources for KABC. + * More methods will be added to give KAddressBook the possibility to + * handle subresources. + */ + +class KDE_EXPORT ResourceABC : public KABC::Resource +{ + Q_OBJECT + TQ_OBJECT + +public: + ResourceABC( const KConfig* ); + virtual ~ResourceABC(); + + /** + * Get the UID to subresource map. This is necessary to implement + * the search order. + * The returned map has the UID as key and the resource it's in as + * the data. + */ + virtual TQMap<TQString, TQString> uidToResourceMap() const = 0; + + /** + * If this resource has subresources, return a TQStringList of them. + * In most cases, resources do not have subresources, so this is + * by default just empty. + */ + virtual TQStringList subresources() const { return TQStringList(); } + + /** + * Is this subresource active or not? + */ + virtual bool subresourceActive( const TQString& ) const { return true; } + + /** + * Is the given subresource writable? + */ + virtual bool subresourceWritable( const TQString& ) const = 0; + + /** + * Completion weight for a given subresource + */ + virtual int subresourceCompletionWeight( const TQString& ) const = 0; + + /** + * Label for a given subresource + */ + virtual TQString subresourceLabel( const TQString& ) const = 0; + +public slots: + /** + * (De-)activate a subresource. + */ + virtual void setSubresourceActive( const TQString &, bool active ) = 0; + + /** + * Set completion weight for a given subresource + */ + virtual void setSubresourceCompletionWeight( const TQString&, int weight ) = 0; + +signals: + /** + * This signal is emitted when a subresource is added. + */ + void signalSubresourceAdded( KPIM::ResourceABC *, const TQString &type, + const TQString &subResource ); + + /** + * This signal is emitted when a subresource is removed. + */ + void signalSubresourceRemoved( KPIM::ResourceABC *, const TQString &type, + const TQString &subResource ); + +}; + +} + +#endif // RESOURCEABC_H diff --git a/libtdepim/sendsmsdialog.cpp b/libtdepim/sendsmsdialog.cpp new file mode 100644 index 000000000..218f9617d --- /dev/null +++ b/libtdepim/sendsmsdialog.cpp @@ -0,0 +1,85 @@ +/* + This file is part of libtdepim. + + Copyright (C) 2005 Con Hennessy <cp.hennessy@iname.com> + Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <tqlabel.h> +#include <tqlayout.h> +#include <tqtextedit.h> + +#include <klocale.h> + +#include "sendsmsdialog.h" + +SendSMSDialog::SendSMSDialog( const TQString &recipientName, TQWidget *parent, const char *name ) + : KDialogBase( Plain, i18n( "Send SMS" ), Ok | Cancel, Ok, parent, name, true, true ) +{ + TQWidget *page = plainPage(); + + TQGridLayout *tqlayout = new TQGridLayout( page, 3, 3, marginHint(), spacingHint() ); + + tqlayout->addWidget( new TQLabel( i18n( "Message" ), page ), 0, 0 ); + + mMessageLength = new TQLabel( "0/160", page ); + mMessageLength->tqsetAlignment( TQt::AlignRight ); + tqlayout->addWidget( mMessageLength, 0, 2 ); + + mText = new TQTextEdit( page ); + tqlayout->addMultiCellWidget( mText, 1, 1, 0, 2 ); + + tqlayout->addWidget( new TQLabel( i18n( "Recipient:" ), page ), 2, 0 ); + tqlayout->addWidget( new TQLabel( recipientName, page ), 2, 2 ); + + setButtonText( Ok, i18n( "Send" ) ); + + connect( mText, TQT_SIGNAL( textChanged() ), + this, TQT_SLOT( updateMessageLength() ) ); + connect( mText, TQT_SIGNAL( textChanged() ), + this, TQT_SLOT( updateButtons() ) ); + + updateButtons(); + + mText->setFocus(); +} + +TQString SendSMSDialog::text() const +{ + return mText->text(); +} + +void SendSMSDialog::updateMessageLength() +{ + int length = mText->length(); + + if( length > 480 ) + mMessageLength->setText( TQString( "%1/%2 (%3)" ).tqarg( length ).tqarg( 500 ).tqarg( 4 ) ); + else if( length > 320 ) + mMessageLength->setText( TQString( "%1/%2 (%3)" ).tqarg( length ).tqarg( 480 ).tqarg( 3 ) ); + else if( length > 160 ) + mMessageLength->setText( TQString( "%1/%2 (%3)" ).tqarg( length ).tqarg( 320 ).tqarg( 2 ) ); + else + mMessageLength->setText( TQString( "%1/%2" ).tqarg( length ).tqarg( 160 ) ); +} + +void SendSMSDialog::updateButtons() +{ + enableButton( Ok, mText->length() > 0 ); +} + +#include "sendsmsdialog.moc" diff --git a/libtdepim/sendsmsdialog.h b/libtdepim/sendsmsdialog.h new file mode 100644 index 000000000..af7a97e9b --- /dev/null +++ b/libtdepim/sendsmsdialog.h @@ -0,0 +1,50 @@ +/* + This file is part of libtdepim. + + Copyright (C) 2005 Con Hennessy <cp.hennessy@iname.com> + Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + */ +#ifndef SENDSMSDIALOG_H +#define SENDSMSDIALOG_H + +#include <kdialogbase.h> + +class TQLabel; +class TQTextEdit; + +class SendSMSDialog : public KDialogBase +{ + Q_OBJECT + TQ_OBJECT + + public: + SendSMSDialog( const TQString &recipientName, TQWidget *parent, const char *name = 0 ); + + TQString text() const; + + private slots: + void updateMessageLength(); + void updateButtons(); + + private: + TQLabel *mMessageLength; + TQTextEdit *mText; +}; + +#endif + diff --git a/libtdepim/sidebarextension.cpp b/libtdepim/sidebarextension.cpp new file mode 100644 index 000000000..db49dc71f --- /dev/null +++ b/libtdepim/sidebarextension.cpp @@ -0,0 +1,40 @@ +/* + This file is part of libtdepim. + + Copyright (C) 2003 Daniel Molkentin <molkentin@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "sidebarextension.h" + +#include <tqwidget.h> +#include <kparts/part.h> + +using namespace KParts; + +SideBarExtension::SideBarExtension(TQWidget *exported, KParts::ReadOnlyPart *parent, const char* name) +: TQObject(parent, name), m_exported(exported), d(0) +{ +} + +SideBarExtension::~SideBarExtension() +{ +} + +#include "sidebarextension.moc" + +// vim: ts=2 sw=2 et diff --git a/libtdepim/sidebarextension.h b/libtdepim/sidebarextension.h new file mode 100644 index 000000000..fc39ef6c6 --- /dev/null +++ b/libtdepim/sidebarextension.h @@ -0,0 +1,69 @@ +/* + This file is part of libtdepim. + + Copyright (C) 2003 Daniel Molkentin <molkentin@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + */ +#ifndef SIDEBAREXTENSION_H +#define SIDEBAREXTENSION_H + +#include <tqobject.h> +#include <tdepimmacros.h> + +class TQWidget; + +namespace KParts +{ + + class ReadOnlyPart; + + /** + * Provides a way to export a widget which will be displayed in Kontacts + * stackview at the left + **/ + class KDE_EXPORT SideBarExtension : public TQObject + { + Q_OBJECT + TQ_OBJECT + + public: + /** + * Constucts a SideBarExtension. + * + * @param exported A TQWidget derived widget that will be provided for the + * users of SideBarExtension. + * @param parent The parent widget. + * @param name The name of the class. + **/ + SideBarExtension(TQWidget *exported, KParts::ReadOnlyPart *parent, const char* name); + ~SideBarExtension(); + + /** + * Retrieve a pointer to the widget. May be 0 if 0 was passed in the constructor + **/ + TQWidget* widget() const { return m_exported; } + + private: + TQWidget* m_exported; + + class SideBarExtensionPrivate; + SideBarExtensionPrivate *d; + }; +} +#endif // SIDEBAREXTENSION_H + +// vim: ts=2 sw=2 et diff --git a/libtdepim/spellingfilter.cpp b/libtdepim/spellingfilter.cpp new file mode 100644 index 000000000..cc94968d4 --- /dev/null +++ b/libtdepim/spellingfilter.cpp @@ -0,0 +1,220 @@ +/** + * spellingfilter.cpp + * + * Copyright (c) 2002 Dave Corrie <kde@davecorrie.com> + * + * This file is part of KMail. + * + * KMail is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <kdebug.h> +#include "spellingfilter.h" + +//----------------------------------------------------------------------------- +// SpellingFilter implementation +// + +SpellingFilter::SpellingFilter(const TQString& text, const TQString& quotePrefix, + UrlFiltering filterUrls, EmailAddressFiltering filterEmailAddresses, + const TQStringList& filterStrings) + : mOriginal(text) +{ + TextCensor c(text); + + if(!quotePrefix.isEmpty()) + c.censorQuotations(quotePrefix); + + if(filterUrls) + c.censorUrls(); + + if(filterEmailAddresses) + c.censorEmailAddresses(); + + TQStringList::const_iterator iter = filterStrings.begin(); + while(iter != filterStrings.end()) + { + c.censorString(*iter); + ++iter; + } + + mFiltered = c.censoredText(); +} + +TQString SpellingFilter::originalText() const +{ + return mOriginal; +} + +TQString SpellingFilter::filteredText() const +{ + return mFiltered; +} + +//----------------------------------------------------------------------------- +// SpellingFilter::TextCensor implementation +// + +SpellingFilter::TextCensor::TextCensor(const TQString& s) + : LinkLocator(s) +{ + +} + +void SpellingFilter::TextCensor::censorQuotations(const TQString& quotePrefix) +{ + mPos = 0; + while(mPos < static_cast<int>(mText.length())) + { + // Find start of quotation + findQuotation(quotePrefix); + if(mPos < static_cast<int>(mText.length())) + { + int start = mPos; + skipQuotation(quotePrefix); + + // Replace quotation with spaces + int len = mPos - start; + TQString spaces; + spaces.fill(' ', len); + mText.replace(start, len, spaces); + + //kdDebug(5006) << "censored quotation [" + // << start << ", " << mPos << ")" << endl; + } + } +} + +void SpellingFilter::TextCensor::censorUrls() +{ + mPos = 0; + while(mPos < static_cast<int>(mText.length())) + { + // Find start of url + TQString url; + while(mPos < static_cast<int>(mText.length()) && url.isEmpty()) + { + url = getUrl(); + ++mPos; + } + + if(mPos < static_cast<int>(mText.length()) && !url.isEmpty()) + { + int start = mPos - url.length(); + + // Replace url with spaces + url.fill(' '); + mText.replace(start, url.length(), url); + + //kdDebug(5006) << "censored url [" + // << start << ", " << mPos << ")" << endl; + } + } +} + +void SpellingFilter::TextCensor::censorEmailAddresses() +{ + mPos = 0; + while(mPos < static_cast<int>(mText.length())) + { + // Find start of email address + findEmailAddress(); + if(mPos < static_cast<int>(mText.length())) + { + TQString address = getEmailAddress(); + ++mPos; + if(!address.isEmpty()) + { + int start = mPos - address.length(); + + // Replace address with spaces + address.fill(' '); + mText.replace(start, address.length(), address); + + //kdDebug(5006) << "censored addr [" + // << start << ", "<< mPos << ")" << endl; + } + } + } +} + +void SpellingFilter::TextCensor::censorString(const TQString& s) +{ + mPos = 0; + while(mPos != -1) + { + // Find start of string + mPos = mText.find(s, mPos); + if(mPos != -1) + { + // Replace string with spaces + TQString spaces; + spaces.fill(' ', s.length()); + mText.replace(mPos, s.length(), spaces); + mPos += s.length(); + + //kdDebug(5006) << "censored string [" + // << mPos << ", "<< mPos+s.length() << ")" << endl; + } + } +} + +TQString SpellingFilter::TextCensor::censoredText() const +{ + return mText; +} + +//----------------------------------------------------------------------------- +// text censorship helper functions +// + +bool SpellingFilter::TextCensor::atLineStart() const +{ + return (mPos == 0 && static_cast<int>(mText.length()) > 0) || (mText[mPos - 1] == '\n'); +} + +void SpellingFilter::TextCensor::skipLine() +{ + mPos = mText.find('\n', mPos); + if(mPos == -1) + mPos = static_cast<int>(mText.length()); + else + ++mPos; +} + +bool SpellingFilter::TextCensor::atQuotation(const TQString& quotePrefix) const +{ + return atLineStart() && + mText.mid(mPos, quotePrefix.length()) == quotePrefix; +} + +void SpellingFilter::TextCensor::skipQuotation(const TQString& quotePrefix) +{ + while(atQuotation(quotePrefix)) + skipLine(); +} + +void SpellingFilter::TextCensor::findQuotation(const TQString& quotePrefix) +{ + while(mPos < static_cast<int>(mText.length()) && !atQuotation(quotePrefix)) + skipLine(); +} + +void SpellingFilter::TextCensor::findEmailAddress() +{ + while(mPos < static_cast<int>(mText.length()) && mText[mPos] != '@') + ++mPos; +} + diff --git a/libtdepim/spellingfilter.h b/libtdepim/spellingfilter.h new file mode 100644 index 000000000..4178a888e --- /dev/null +++ b/libtdepim/spellingfilter.h @@ -0,0 +1,77 @@ +/* + * spellingfilter.h + * + * Copyright (c) 2002 Dave Corrie <kde@davecorrie.com> + * + * This file is part of KMail. + * + * KMail is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef SPELLINGFILTER_H_INCLUDED +#define SPELLINGFILTER_H_INCLUDED + +#include <tqstring.h> +#include <tqstringlist.h> +#include "linklocator.h" + +#include <tdepimmacros.h> + +class KDE_EXPORT SpellingFilter +{ +public: + enum UrlFiltering { DontFilterUrls, FilterUrls }; + enum EmailAddressFiltering { DontFilterEmailAddresses, FilterEmailAddresses }; + + SpellingFilter(const TQString& text, const TQString& quotePrefix, + UrlFiltering filterUrls = FilterUrls, + EmailAddressFiltering filterEmailAddresses = FilterEmailAddresses, + const TQStringList& filterStrings = TQStringList()); + + TQString originalText() const; + TQString filteredText() const; + + class TextCensor; + +private: + const TQString mOriginal; + TQString mFiltered; +}; + +class SpellingFilter::TextCensor : public LinkLocator +{ +public: + TextCensor(const TQString& s); + + void censorQuotations(const TQString& quotePrefix); + void censorUrls(); + void censorEmailAddresses(); + void censorString(const TQString& s); + + TQString censoredText() const; + +private: + bool atLineStart() const; + void skipLine(); + + bool atQuotation(const TQString& quotePrefix) const; + void skipQuotation(const TQString& quotePrefix); + void findQuotation(const TQString& quotePrefix); + + void findEmailAddress(); +}; + +#endif // SPELLINGFILTER_H_INCLUDED + diff --git a/libtdepim/ssllabel.cpp b/libtdepim/ssllabel.cpp new file mode 100644 index 000000000..8068aa001 --- /dev/null +++ b/libtdepim/ssllabel.cpp @@ -0,0 +1,97 @@ +/* -*- mode: C++; c-file-style: "gnu" -*- + * + * This file is part of KMail, the KDE mail client. + * + * Copyright (c) 2003 Zack Rusin <zack@kde.org> + * + * KMail is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * KMail 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of this program with any edition of + * the TQt library by Trolltech AS, Norway (or with modified versions + * of TQt that use the same license as TQt), and distribute linked + * combinations including the two. You must obey the GNU General + * Public License in all respects for all of the code used other than + * TQt. If you modify this file, you may extend this exception to + * your version of the file, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from + * your version. + */ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "ssllabel.h" + +#include <kiconloader.h> +#include <klocale.h> + +#include <tqtooltip.h> + +namespace KPIM { + +SSLLabel::SSLLabel( TQWidget* parent ) + : TQLabel( parent ) +{ + setState( Done ); +} + +void SSLLabel::setEncrypted( bool enc ) +{ + if ( enc ) { + m_lastEncryptionState = Encrypted; + } else { + m_lastEncryptionState = Unencrypted; + } +} + +SSLLabel::State SSLLabel::lastState() const +{ + return m_lastEncryptionState; +} + +void SSLLabel::setState( State state ) +{ + switch( state ) { + case Encrypted: + TQToolTip::remove( this ); + TQToolTip::add( this, i18n("Connection is encrypted") ); + setPixmap( SmallIcon( "encrypted", KGlobal::instance() ) ); + show(); + break; + case Unencrypted: + TQToolTip::remove( this ); + TQToolTip::add( this, i18n("Connection is unencrypted") ); + setPixmap( SmallIcon( "decrypted" ) ); + show(); + break; + case Done: + TQToolTip::remove( this ); + hide(); + break; + case Clean: + default: + TQToolTip::remove( this ); + hide(); + //we return because we do not save the state as the only + //action we want to perform is to hide ourself + return; + } + m_lastEncryptionState = state; +} + + +} //end namespace KPIM + +//#include "ssllabel.moc" diff --git a/libtdepim/ssllabel.h b/libtdepim/ssllabel.h new file mode 100644 index 000000000..03a841bed --- /dev/null +++ b/libtdepim/ssllabel.h @@ -0,0 +1,59 @@ +/* -*- mode: C++; c-file-style: "gnu" -*- + * + * This file is part of KMail, the KDE mail client. + * + * Copyright (c) 2003 Zack Rusin <zack@kde.org> + * + * KMail is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * KMail 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of this program with any edition of + * the TQt library by Trolltech AS, Norway (or with modified versions + * of TQt that use the same license as TQt), and distribute linked + * combinations including the two. You must obey the GNU General + * Public License in all respects for all of the code used other than + * TQt. If you modify this file, you may extend this exception to + * your version of the file, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from + * your version. + */ + +#ifndef SSLLABEL_H +#define SSLLABEL_H + +#include <tqlabel.h> + +namespace KPIM { + + class SSLLabel : public TQLabel + { + public: + enum State { + Encrypted, + Unencrypted, + Clean, + Done + }; + SSLLabel( TQWidget* parent ); + + void setEncrypted( bool enc=true ); + void setState( State state ); + State lastState() const; + private: + State m_lastEncryptionState; + }; + +} + +#endif diff --git a/libtdepim/statusbarprogresswidget.cpp b/libtdepim/statusbarprogresswidget.cpp new file mode 100644 index 000000000..c417905a0 --- /dev/null +++ b/libtdepim/statusbarprogresswidget.cpp @@ -0,0 +1,296 @@ +/* + statusbarprogresswidget.cpp + + This file is part of KMail, the KDE mail client. + + (C) 2004 KMail Authors + Includes StatusbarProgressWidget which is based on KIOLittleProgressDlg + by Matt Koss <koss@miesto.sk> + + KMail is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License, version 2, as + published by the Free Software Foundation. + + KMail 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the TQt library by Trolltech AS, Norway (or with modified versions + of TQt that use the same license as TQt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + TQt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + + +#include "ssllabel.h" +using KPIM::SSLLabel; +#include "progressmanager.h" +using KPIM::ProgressItem; +using KPIM::ProgressManager; + +#include <kprogress.h> +#include <kiconloader.h> +#include <kdebug.h> + +#include <tqtimer.h> +#include <tqlabel.h> +#include <tqpushbutton.h> +#include <tqtooltip.h> +#include <klocale.h> +#include <tqlayout.h> +#include <tqwidgetstack.h> +#include <tqframe.h> + +#include "progressdialog.h" +#include "statusbarprogresswidget.h" + +using namespace KPIM; + +//----------------------------------------------------------------------------- +StatusbarProgressWidget::StatusbarProgressWidget( ProgressDialog* progressDialog, TQWidget* parent, bool button ) + : TQFrame( parent ), mCurrentItem( 0 ), mProgressDialog( progressDialog ), + mDelayTimer( 0 ), mBusyTimer( 0 ) +{ + m_bShowButton = button; + int w = fontMetrics().width( " 999.9 kB/s 00:00:01 " ) + 8; + box = new TQHBoxLayout( this, 0, 0 ); + + m_pButton = new TQPushButton( this ); + m_pButton->tqsetSizePolicy( TQSizePolicy( TQSizePolicy::Minimum, + TQSizePolicy::Minimum ) ); + m_pButton->setPixmap( SmallIcon( "up" ) ); + box->addWidget( m_pButton ); + stack = new TQWidgetStack( this ); + stack->setMaximumHeight( fontMetrics().height() ); + box->addWidget( stack ); + + m_sslLabel = new SSLLabel( this ); + box->addWidget( m_sslLabel ); + + TQToolTip::add( m_pButton, i18n("Open detailed progress dialog") ); + + m_pProgressBar = new KProgress( this ); + m_pProgressBar->setLineWidth( 1 ); + m_pProgressBar->setFrameStyle( TQFrame::Box ); + m_pProgressBar->installEventFilter( this ); + m_pProgressBar->setMinimumWidth( w ); + stack->addWidget( m_pProgressBar, 1 ); + + m_pLabel = new TQLabel( TQString(), this ); + m_pLabel->tqsetAlignment( AlignHCenter | AlignVCenter ); + m_pLabel->installEventFilter( this ); + m_pLabel->setMinimumWidth( w ); + stack->addWidget( m_pLabel, 2 ); + m_pButton->setMaximumHeight( fontMetrics().height() ); + setMinimumWidth( tqminimumSizeHint().width() ); + + mode = None; + setMode(); + + connect( m_pButton, TQT_SIGNAL( clicked() ), + progressDialog, TQT_SLOT( slotToggleVisibility() ) ); + + connect ( ProgressManager::instance(), TQT_SIGNAL( progressItemAdded( KPIM::ProgressItem * ) ), + this, TQT_SLOT( slotProgressItemAdded( KPIM::ProgressItem * ) ) ); + connect ( ProgressManager::instance(), TQT_SIGNAL( progressItemCompleted( KPIM::ProgressItem * ) ), + this, TQT_SLOT( slotProgressItemCompleted( KPIM::ProgressItem * ) ) ); + connect ( ProgressManager::instance(), TQT_SIGNAL(progressItemUsesBusyIndicator(KPIM::ProgressItem*,bool)), + this, TQT_SLOT( updateBusyMode() ) ); + + connect ( progressDialog, TQT_SIGNAL( visibilityChanged( bool )), + this, TQT_SLOT( slotProgressDialogVisible( bool ) ) ); + + mDelayTimer = new TQTimer( this ); + connect ( mDelayTimer, TQT_SIGNAL( timeout() ), + this, TQT_SLOT( slotShowItemDelayed() ) ); +} + +// There are three cases: no progressitem, one progressitem (connect to it directly), +// or many progressitems (display busy indicator). Let's call them 0,1,N. +// In slot..Added we can only end up in 1 or N. +// In slot..Removed we can end up in 0, 1, or we can stay in N if we were already. + +void StatusbarProgressWidget::updateBusyMode() +{ + connectSingleItem(); // if going to 1 item + if ( mCurrentItem ) { // Exactly one item + delete mBusyTimer; + mBusyTimer = 0; + mDelayTimer->start( 1000, true ); + } + else { // N items + if ( !mBusyTimer ) { + mBusyTimer = new TQTimer( this ); + connect( mBusyTimer, TQT_SIGNAL( timeout() ), + this, TQT_SLOT( slotBusyIndicator() ) ); + mDelayTimer->start( 1000, true ); + } + } +} + +void StatusbarProgressWidget::slotProgressItemAdded( ProgressItem *item ) +{ + if ( item->parent() ) + return; // we are only interested in top level items + + updateBusyMode(); +} + +void StatusbarProgressWidget::slotProgressItemCompleted( ProgressItem *item ) +{ + if ( item->parent() ) return; // we are only interested in top level items + connectSingleItem(); // if going back to 1 item + if ( ProgressManager::instance()->isEmpty() ) { // No item + // Done. In 5s the progress-widget will close, then we can clean up the statusbar + TQTimer::singleShot( 5000, this, TQT_SLOT( slotClean() ) ); + } else if ( mCurrentItem ) { // Exactly one item + delete mBusyTimer; + mBusyTimer = 0; + activateSingleItemMode(); + } +} + +void StatusbarProgressWidget::connectSingleItem() +{ + if ( mCurrentItem ) { + disconnect ( mCurrentItem, TQT_SIGNAL( progressItemProgress( KPIM::ProgressItem *, unsigned int ) ), + this, TQT_SLOT( slotProgressItemProgress( KPIM::ProgressItem *, unsigned int ) ) ); + mCurrentItem = 0; + } + mCurrentItem = ProgressManager::instance()->singleItem(); + if ( mCurrentItem ) { + connect ( mCurrentItem, TQT_SIGNAL( progressItemProgress( KPIM::ProgressItem *, unsigned int ) ), + this, TQT_SLOT( slotProgressItemProgress( KPIM::ProgressItem *, unsigned int ) ) ); + } +} + +void StatusbarProgressWidget::activateSingleItemMode() +{ + m_pProgressBar->setTotalSteps( 100 ); + m_pProgressBar->setProgress( mCurrentItem->progress() ); + m_pProgressBar->setPercentageVisible( true ); +} + +void StatusbarProgressWidget::slotShowItemDelayed() +{ + bool noItems = ProgressManager::instance()->isEmpty(); + if ( mCurrentItem ) { + activateSingleItemMode(); + } else if ( !noItems ) { // N items + m_pProgressBar->setTotalSteps( 0 ); + m_pProgressBar->setPercentageVisible( false ); + Q_ASSERT( mBusyTimer ); + if ( mBusyTimer ) + mBusyTimer->start( 100 ); + } + + if ( !noItems && mode == None ) { + mode = Progress; + setMode(); + } +} + +void StatusbarProgressWidget::slotBusyIndicator() +{ + int p = m_pProgressBar->progress(); + m_pProgressBar->setProgress( p + 10 ); +} + +void StatusbarProgressWidget::slotProgressItemProgress( ProgressItem *item, unsigned int value ) +{ + Q_ASSERT( item == mCurrentItem); // the only one we should be connected to + m_pProgressBar->setProgress( value ); +} + +void StatusbarProgressWidget::slotSetSSL( bool ssl ) +{ + m_sslLabel->setEncrypted( ssl ); +} + +void StatusbarProgressWidget::setMode() { + switch ( mode ) { + case None: + if ( m_bShowButton ) { + m_pButton->hide(); + } + m_sslLabel->setState( SSLLabel::Done ); + // show the empty label in order to make the status bar look better + stack->show(); + stack->raiseWidget( m_pLabel ); + break; + +#if 0 + case Label: + if ( m_bShowButton ) { + m_pButton->show(); + } + m_sslLabel->setState( m_sslLabel->lastState() ); + stack->show(); + stack->raiseWidget( m_pLabel ); + break; +#endif + + case Progress: + stack->show(); + stack->raiseWidget( m_pProgressBar ); + if ( m_bShowButton ) { + m_pButton->show(); + } + m_sslLabel->setState( m_sslLabel->lastState() ); + break; + } +} + +void StatusbarProgressWidget::slotClean() +{ + // check if a new item showed up since we started the timer. If not, clear + if ( ProgressManager::instance()->isEmpty() ) { + m_pProgressBar->setProgress( 0 ); + //m_pLabel->clear(); + mode = None; + setMode(); + } +} + +bool StatusbarProgressWidget::eventFilter( TQObject *, TQEvent *ev ) +{ + if ( ev->type() == TQEvent::MouseButtonPress ) { + TQMouseEvent *e = (TQMouseEvent*)ev; + + if ( e->button() == Qt::LeftButton && mode != None ) { // toggle view on left mouse button + // Consensus seems to be that we should show/hide the fancy dialog when the user + // clicks anywhere in the small one. + mProgressDialog->slotToggleVisibility(); + return true; + } + } + return false; +} + +void StatusbarProgressWidget::slotProgressDialogVisible( bool b ) +{ + // Update the hide/show button when the detailed one is shown/hidden + if ( b ) { + m_pButton->setPixmap( SmallIcon( "down" ) ); + TQToolTip::remove( m_pButton ); + TQToolTip::add( m_pButton, i18n("Hide detailed progress window") ); + setMode(); + } else { + m_pButton->setPixmap( SmallIcon( "up" ) ); + TQToolTip::remove( m_pButton ); + TQToolTip::add( m_pButton, i18n("Show detailed progress window") ); + } +} + +#include "statusbarprogresswidget.moc" diff --git a/libtdepim/statusbarprogresswidget.h b/libtdepim/statusbarprogresswidget.h new file mode 100644 index 000000000..8b08d2cf9 --- /dev/null +++ b/libtdepim/statusbarprogresswidget.h @@ -0,0 +1,106 @@ +#ifndef __KPIM_STATUSBARPROGRESSWIDGET_H +#define __KPIM_STATUSBARPROGRESSWIDGET_H +/* + statusbarprogresswidget.h + + This file is part of KMail, the KDE mail client. + + (C) 2004 KMail Authors + + KMail is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License, version 2, as + published by the Free Software Foundation. + + KMail 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the TQt library by Trolltech AS, Norway (or with modified versions + of TQt that use the same license as TQt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + TQt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ +/** + * A specialized progress widget class, heavily based on + * kio_littleprogress_dlg (it looks similar) + */ + +#include <tdepimmacros.h> + +class KMMainWidget; +class KProgress; +class TQPushButton; +class TQWidgetStack; +class TQBoxLayout; +class TQLabel; +class TQTimer; + +namespace KPIM { +class SSLLabel; +class ProgressItem; +class ProgressDialog; + +class KDE_EXPORT StatusbarProgressWidget : public TQFrame { + + Q_OBJECT + TQ_OBJECT + +public: + + StatusbarProgressWidget( ProgressDialog* progressDialog, TQWidget* parent, bool button = true ); + +public slots: + + void slotClean(); + void slotSetSSL( bool ); + + void slotProgressItemAdded( KPIM::ProgressItem *i ); + void slotProgressItemCompleted( KPIM::ProgressItem *i ); + void slotProgressItemProgress( KPIM::ProgressItem *i, unsigned int value ); + +protected slots: + void slotProgressDialogVisible( bool ); + void slotShowItemDelayed(); + void slotBusyIndicator(); + void updateBusyMode(); + +protected: + void setMode(); + void connectSingleItem(); + void activateSingleItemMode(); + + virtual bool eventFilter( TQObject *, TQEvent * ); + +private: + KProgress* m_pProgressBar; + TQLabel* m_pLabel; + SSLLabel* m_sslLabel; + TQPushButton* m_pButton; + + enum Mode { None, /*Label,*/ Progress }; + + uint mode; + bool m_bShowButton; + + TQBoxLayout *box; + TQWidgetStack *stack; + ProgressItem *mCurrentItem; + ProgressDialog* mProgressDialog; + TQTimer *mDelayTimer; + TQTimer *mBusyTimer; +}; + +} // namespace + +#endif diff --git a/libtdepim/tdepim.widgets b/libtdepim/tdepim.widgets new file mode 100644 index 000000000..765abc23b --- /dev/null +++ b/libtdepim/tdepim.widgets @@ -0,0 +1,29 @@ +[Global] +PluginName=KDEPimWidgets +Includes=kinstance.h +Init=new KInstance("tdepimwidgets"); + +[KPIM::AddresseeLineEdit] +ToolTip=Addressee Line Edit (KDE-PIM) +WhatsThis=A line edit which lets you select addressees from the addressbook +IncludeFile=libtdepim/addresseelineedit.h +Group=Input (KDE-PIM) + +[KPIM::ClickLineEdit] +ToolTip=Click Line Edit (KDE-PIM) +WhatsThis=A line edit that shows grayed-out default text when no text is set. +IncludeFile=libtdepim/clicklineedit.h +Group=Input (KDE-PIM) + +[KDateEdit] +ToolTip=Date Edit Combobox (KDE-PIM) +WhatsThis=A widget to enter a date. It can also be selected from a date picker calendar. +IncludeFile=libtdepim/kdateedit.h +Group=Input (KDE-PIM) + +[KTimeEdit] +ToolTip=Time Edit Combobox (KDE-PIM) +WhatsThis=A widget to enter a time. It can also be selected from a drop-down list. +IncludeFile=libtdepim/ktimeedit.h +ConstructorArgs=(parent, TQTime::currentTime(), name) +Group=Input (KDE-PIM) diff --git a/libtdepim/tdepimmacros.h b/libtdepim/tdepimmacros.h new file mode 100644 index 000000000..b341ee7ba --- /dev/null +++ b/libtdepim/tdepimmacros.h @@ -0,0 +1,35 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2002-2003 KDE Team + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +// WARNING Don't add include guards here, they were removed on purpose + +#include <kdeversion.h> +#include <kdemacros.h> + +#if KDE_IS_VERSION( 3,3,90 ) +/* life is great */ +#else +/* workaround typo that breaks compilation with newer gcc */ +#undef KDE_EXPORT +#define KDE_EXPORT +#undef KDE_NO_EXPORT +#define KDE_NO_EXPORT +#endif diff --git a/libtdepim/tdepimprotocols.h b/libtdepim/tdepimprotocols.h new file mode 100644 index 000000000..e269fcabd --- /dev/null +++ b/libtdepim/tdepimprotocols.h @@ -0,0 +1,32 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef KDEPIM_KDEPIMPROTOCOLS_H +#define KDEPIM_KDEPIMPROTOCOLS_H + +/* a central place to store protocol strings to avoid knowledge duplication */ + +#define KDEPIMPROTOCOL_CONTACT "uid:" +#define KDEPIMPROTOCOL_EMAIL "kmail:" +#define KDEPIMPROTOCOL_INCIDENCE "urn:x-ical" +#define KDEPIMPROTOCOL_NEWSARTICLE "news:" + +#endif diff --git a/libtdepim/tests/Makefile.am b/libtdepim/tests/Makefile.am new file mode 100644 index 000000000..217676cae --- /dev/null +++ b/libtdepim/tests/Makefile.am @@ -0,0 +1,35 @@ +AM_CPPFLAGS = -I$(top_builddir)/libtdepim -I$(top_srcdir)/libtdepim -I$(top_srcdir)/libemailfunctions $(all_includes) +AM_LDFLAGS = $(all_libraries) $(KDE_RPATH) $(LIB_QT) -lDCOP $(LIB_KDECORE) $(LIB_KDEUI) -ltdefx $(LIB_KIO) -lktexteditor +LDADD = ../libtdepim.la $(LIB_KDECORE) + +check_PROGRAMS = testwizard testaddresseelineedit \ + testaddresseeselector \ + testutf7decoder \ + testutf7encoder \ + test_kregexp \ + testdateedit \ + testlinklocator \ + testkincidencechooser + +# disabled because of X dependency +# testdistrlist + +testwizard_SOURCES = testwizard.cpp myconfig.kcfgc +testaddresseelineedit_SOURCES = testaddresseelineedit.cpp +testaddresseeselector_SOURCES = testaddresseeselector.cpp +testutf7encoder_SOURCES = testutf7encoder2.cpp +testutf7decoder_SOURCES = testutf7decoder.cpp +test_kregexp_SOURCES = test_kregexp.cpp +testdateedit_SOURCES = testdateedit.cpp +testlinklocator_SOURCES = testlinklocator.cpp +#testdistrlist_SOURCES = testdistrlist.cpp +testkincidencechooser_SOURCES = testkincidencechooser.cpp + +#TESTS = testdistrlist + +METASOURCES = AUTO + +#example_LDFLAGS = $(all_libraries) $(KDE_RPATH) $(LIB_QT) -lDCOP $(LIB_KDECORE) $(LIB_KDEUI) -ltdefx $(LIB_KIO) -lktexteditor +#example_LDADD = ../libtdepim.la $(LIB_KDECORE) +#example_SOURCES = example.cpp exampleprefs_base.kcfgc + diff --git a/libtdepim/tests/myconfig.kcfgc b/libtdepim/tests/myconfig.kcfgc new file mode 100644 index 000000000..35e26c996 --- /dev/null +++ b/libtdepim/tests/myconfig.kcfgc @@ -0,0 +1,11 @@ +# Code generation options for kconfig_compiler +File=propagator_test.kcfg +ClassName=MyConfig +Singleton=true +Mutators=true +#Inherits=KPimPrefs +#IncludeFiles=libtdepim/kpimprefs.h +MemberVariables=public +GlobalEnums=true +ItemAccessors=true +SetUserTexts=true diff --git a/libtdepim/tests/propagator_test.kcfg b/libtdepim/tests/propagator_test.kcfg new file mode 100644 index 000000000..8fc971771 --- /dev/null +++ b/libtdepim/tests/propagator_test.kcfg @@ -0,0 +1,44 @@ +<?xml version="1.0" encoding="UTF-8"?> +<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0 + http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" > + <kcfgfile name="testwizardrc"/> + + <group name="MySettings"> + <entry name="FixKMail" type="Bool"> + <label>Fix KMail</label> + <default>true</default> + </entry> + <entry name="BreakKMail" type="Bool"> + <label>Fix KMail</label> + <default>false</default> + </entry> + <entry name="DisableDefaultSigning" type="Int"> + <default>0</default> + </entry> + <entry name="EnableDefaultSigning" type="Int"> + <default>1</default> + </entry> + <entry name="ThisIsAString" type="String"> + <default>hallo</default> + </entry> + </group> + + <condition key="testwizardrc/MySettings/FixKMail" value="true"> + <propagation source="testwizardrc/MySettings/DisableDefaultSigning" + target="kmailrc/CryptPlug #0/SignEmail" /> + </condition> + + <condition key="testwizardrc/MySettings/BreakKMail" value="true"> + <propagation source="testwizardrc/MySettings/EnableDefaultSigning" + target="kmailrc/CryptPlug #0/SignEmail" /> + </condition> + + <propagation source="file1/group1/entry1" target="file2/group2/entry2" /> + <propagation source="file1/group1/entry3" target="file2/group2/entry4" /> + <condition key="myfile/mygroup/myentry" value="123"> + <propagation source="file1/group1/entry1" target="filex/groupx/entryx" /> + </condition> + +</kcfg> diff --git a/libtdepim/tests/test_kregexp.cpp b/libtdepim/tests/test_kregexp.cpp new file mode 100644 index 000000000..511687d07 --- /dev/null +++ b/libtdepim/tests/test_kregexp.cpp @@ -0,0 +1,16 @@ +#include <kregexp3.h> +#include <kdebug.h> +#include <kinstance.h> + +int +main() +{ + KInstance app("# "); + + // test for http://bugs.kde.org/show_bug.cgi?id=54886 + KRegExp3 reg("^"); + TQString res = reg.replace(TQString::tqfromLatin1("Fun stuff"), + TQString::tqfromLatin1("[fun] ")); + kdDebug() << res << endl; + +} diff --git a/libtdepim/tests/testaddresseelineedit.cpp b/libtdepim/tests/testaddresseelineedit.cpp new file mode 100644 index 000000000..12eb3c6b1 --- /dev/null +++ b/libtdepim/tests/testaddresseelineedit.cpp @@ -0,0 +1,44 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Daniel Molkentin <danimo@klaralvdalens-datakonsult.se> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <kaboutdata.h> +#include <kapplication.h> +#include <kdebug.h> +#include <klocale.h> +#include <kcmdlineargs.h> + +#include "../addresseelineedit.h" + +int main(int argc, char* argv[]) +{ + KAboutData aboutData("testaddresseelineedit","Test AddresseeLineEdit","0.1"); + KCmdLineArgs::init(argc,argv,&aboutData); + + KApplication app; + + KPIM::AddresseeLineEdit *kale = new KPIM::AddresseeLineEdit(0); + kale->resize( 400, 20 ); + kale->show(); + + return app.exec(); + +} + diff --git a/libtdepim/tests/testaddresseeselector.cpp b/libtdepim/tests/testaddresseeselector.cpp new file mode 100644 index 000000000..6b910e4b9 --- /dev/null +++ b/libtdepim/tests/testaddresseeselector.cpp @@ -0,0 +1,52 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Daniel Molkentin <danimo@klaralvdalens-datakonsult.se> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <kaboutdata.h> +#include <kapplication.h> +#include <kdebug.h> +#include <klocale.h> +#include <kcmdlineargs.h> + +#include "../addresseeselector.h" +#include "../addresseeemailselection.h" + +int main( int argc, char **argv ) +{ + KAboutData aboutData( "testaddresseeseletor", "Test AddresseeSelector", "0.1" ); + KCmdLineArgs::init( argc, argv, &aboutData ); + + KApplication app; + + KPIM::AddresseeEmailSelection selection; + + KPIM::AddresseeSelectorDialog dlg( &selection ); + dlg.exec(); + + kdDebug() << "to: " << selection.to() << endl; + kdDebug() << "cc: " << selection.cc() << endl; + kdDebug() << "bcc: " << selection.bcc() << endl; + + kdDebug() << "toDistlists: " << selection.toDistributionLists() << endl; + kdDebug() << "ccDistlists: " << selection.ccDistributionLists() << endl; + kdDebug() << "bccDistlists: " << selection.bccDistributionLists() << endl; + + return 0; +} diff --git a/libtdepim/tests/testdateedit.cpp b/libtdepim/tests/testdateedit.cpp new file mode 100644 index 000000000..46fabcfe1 --- /dev/null +++ b/libtdepim/tests/testdateedit.cpp @@ -0,0 +1,68 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <tqlayout.h> + +#include <kaboutdata.h> +#include <kapplication.h> +#include <kcmdlineargs.h> +#include <kdebug.h> +#include <klocale.h> + +#include "kdateedit.h" + +#include "testdateedit.h" + +DateEdit::DateEdit( TQWidget *parent, const char *name ) + : TQWidget( parent, name ) +{ + TQVBoxLayout *tqlayout = new TQVBoxLayout( this ); + + KDateEdit *edit = new KDateEdit( this ); + tqlayout->addWidget( edit ); + + connect( edit, TQT_SIGNAL( dateChanged( const TQDate& ) ), + this, TQT_SLOT( dateChanged( const TQDate& ) ) ); +} + +void DateEdit::dateChanged( const TQDate &date ) +{ + if ( date.isValid() ) + qDebug( "%s", date.toString().latin1() ); + else + qDebug( "invalid date entered" ); +} + +int main(int argc,char **argv) +{ + KAboutData aboutData( "testdateedit", "Test KDateEdit", "0.1" ); + KCmdLineArgs::init( argc, argv, &aboutData ); + + KApplication app; + + DateEdit dateEdit; + app.setMainWidget( &dateEdit ); + dateEdit.show(); + + app.exec(); +} + +#include "testdateedit.moc" diff --git a/libtdepim/tests/testdateedit.h b/libtdepim/tests/testdateedit.h new file mode 100644 index 000000000..8693ffa00 --- /dev/null +++ b/libtdepim/tests/testdateedit.h @@ -0,0 +1,39 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2004 Tobias Koenig <tokoe@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. +*/ + +#ifndef TESTDATEEDIT_H +#define TESTDATEEDIT_H + +#include <tqwidget.h> + +class DateEdit : public TQWidget +{ + Q_OBJECT + TQ_OBJECT + + public: + DateEdit( TQWidget *parent = 0, const char *name = 0 ); + + public slots: + void dateChanged( const TQDate& ); +}; + +#endif diff --git a/libtdepim/tests/testdistrlist.cpp b/libtdepim/tests/testdistrlist.cpp new file mode 100644 index 000000000..f588f9a93 --- /dev/null +++ b/libtdepim/tests/testdistrlist.cpp @@ -0,0 +1,332 @@ +/* This file is part of the KDE project + Copyright (C) 2004 David Faure <faure@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 "testdistrlist.h" + +#include <distributionlist.h> +using KPIM::DistributionList; + +#include <config.h> + +#include <kabc/stdaddressbook.h> +#include <kurl.h> +#include <kapplication.h> +#include <kio/netaccess.h> +#include <kio/job.h> +#include <kdebug.h> +#include <kcmdlineargs.h> + +#include <tqdir.h> +#include <tqfileinfo.h> + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <assert.h> + +int main(int argc, char *argv[]) +{ + // Use another directory than the real one, just to keep things clean + // KDEHOME needs to be writable though, for a ksycoca database + setenv( "KDEHOME", TQFile::encodeName( TQDir::homeDirPath() + "/.kde-testdistrlist" ), true ); + setenv( "KDE_FORK_SLAVES", "yes", true ); // simpler, for the final cleanup + + KApplication::disableAutoDcopRegistration(); + KCmdLineArgs::init(argc,argv,"testdistrlist", 0, 0, 0, 0); + KApplication app( false, false ); + + TestDistrList test; + test.setup(); + test.runAll(); + test.cleanup(); + kdDebug() << "All tests OK." << endl; + return 0; +} + +void TestDistrList::setup() +{ + // We need a std addressbook + KABC::AddressBook *ab = KABC::StdAddressBook::self(); + KABC::StdAddressBook::setAutomaticSave( false ); + + // and two contacts + KABC::Addressee addr1; + addr1.setName( "addr1" ); + addr1.setFormattedName( "addr1" ); + addr1.insertEmail( "addr1@kde.org", true ); + addr1.insertEmail( "addr1-alternate@kde.org" ); + ab->insertAddressee( addr1 ); + assert( addr1.emails().count() == 2 ); + + KABC::Addressee addr2; + addr2.setName( "addr2" ); + addr2.insertEmail( "addr2@kde.org", true ); + addr2.insertEmail( "addr2-alternate@kde.org" ); + ab->insertAddressee( addr2 ); + assert( addr2.emails().count() == 2 ); + + assert( !ab->findByName( "addr1" ).isEmpty() ); + assert( !ab->findByName( "addr2" ).isEmpty() ); +} + +void TestDistrList::runAll() +{ + testEmpty(); + testNewList(); + testInsertEntry(); + testRemoveEntry(); + testDuplicate(); + testDeleteList(); +} + +bool TestDistrList::check(const TQString& txt, TQString a, TQString b) +{ + if (a.isEmpty()) + a = TQString(); + if (b.isEmpty()) + b = TQString(); + if (a == b) { + kdDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'... " << "ok" << endl; + } + else { + kdDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'... " << "KO !" << endl; + cleanup(); + exit(1); + } + return true; +} + +// taken from KMail +#include <sys/types.h> +#include <dirent.h> +static bool removeDirAndContentsRecursively( const TQString & path ) +{ + kdDebug() << k_funcinfo << path << endl; + bool success = true; + + TQDir d; + d.setPath( path ); + d.setFilter( TQDir::Files | TQDir::Dirs | TQDir::Hidden ); + + const TQFileInfoList *list = d.entryInfoList(); + TQFileInfoListIterator it( *list ); + TQFileInfo *fi; + + while ( (fi = it.current()) != 0 ) { + if( fi->isDir() && !fi->isSymLink() ) { + if ( fi->fileName() != "." && fi->fileName() != ".." ) + success = success && removeDirAndContentsRecursively( fi->absFilePath() ); + } else { + success = success && d.remove( fi->absFilePath() ); + } + ++it; + } + + if ( success ) { + success = success && d.rmdir( path ); // nuke ourselves, we should be empty now + } + return success; +} + +void TestDistrList::cleanup() +{ + kdDebug() << k_funcinfo << endl; + KABC::AddressBook *ab = KABC::StdAddressBook::self(); + ab->clear(); + KABC::StdAddressBook::close(); + + TQString kdehome = TQFile::decodeName( getenv("KDEHOME") ); + KURL urlkdehome; urlkdehome.setPath( kdehome ); + // don't use KIO::NetAccess here since it needs X + // KIO::NetAccess::del( urlkdehome, 0 )i; + assert( removeDirAndContentsRecursively( kdehome ) ); +} + +void TestDistrList::testEmpty() +{ + kdDebug() << k_funcinfo << endl; + DistributionList dl; + assert( dl.isEmpty() ); +} + +void TestDistrList::testNewList() +{ + kdDebug() << k_funcinfo << endl; + DistributionList dl; + dl.setName( "foo" ); + assert( !dl.isEmpty() ); + check( "name set", dl.formattedName(), "foo" ); + assert( DistributionList::isDistributionList( dl ) ); + + KABC::AddressBook *ab = KABC::StdAddressBook::self(); + ab->insertAddressee( dl ); +#if 0 // can't do that until we have KABC::AddressBook::findByFormattedName, or we use setName() + KABC::Addressee::List addrList = ab->findByName( "foo" ); + assert( addrList.count() == 1 ); + KABC::Addressee addr = addrList.first(); + assert( !addr.isEmpty() ); + check( "correct name", addr.name(), "foo" ); + assert( DistributionList::isDistributionList( addr ) ); +#else + KABC::Addressee addr = dl; +#endif + + DistributionList dl2 = DistributionList::findByName( ab, "foo" ); + assert( !dl2.isEmpty() ); + check( "correct name", dl2.formattedName(), "foo" ); + assert( DistributionList::isDistributionList( dl2 ) ); + + // Test the ctor that takes an addressee + DistributionList dl3( addr ); + assert( !dl3.isEmpty() ); + assert( DistributionList::isDistributionList( dl3 ) ); + check( "correct name", dl3.formattedName(), "foo" ); +} + +void TestDistrList::testInsertEntry() +{ + kdDebug() << k_funcinfo << endl; + KABC::AddressBook *ab = KABC::StdAddressBook::self(); + DistributionList dl = DistributionList::findByName( ab, "foo" ); + assert( !dl.isEmpty() ); + +#if 0 // the usual method + KABC::Addressee addr1 = ab->findByName( "addr1" ).first(); + assert( !addr1.isEmpty() ); + dl.insertEntry( addr1 ); +#else // the kolab-resource method + dl.insertEntry( "addr1" ); +#endif + + KABC::Addressee addr2 = ab->findByName( "addr2" ).first(); + assert( !addr2.isEmpty() ); + dl.insertEntry( addr2, "addr2-alternate@kde.org" ); + + // Try inserting it again, should do nothing + dl.insertEntry( addr2, "addr2-alternate@kde.org" ); + + // And insert it with another email address + dl.insertEntry( addr2, "addr2@kde.org" ); + + // Test entries() + DistributionList::Entry::List entries = dl.entries( ab ); + check( "entries count", TQString::number( entries.count() ), "3" ); + check( "first entry", entries[0].addressee.name(), "addr1" ); + check( "first entry", entries[0].email, TQString() ); + check( "second entry", entries[1].addressee.name(), "addr2" ); + check( "second entry", entries[1].email, "addr2-alternate@kde.org" ); + check( "third entry", entries[2].addressee.name(), "addr2" ); + check( "third entry", entries[2].email, "addr2@kde.org" ); + + // Test emails() + TQStringList emails = dl.emails( ab ); + kdDebug() << emails << endl; + assert( emails.count() == 3 ); + check( "first email", emails[0], "addr1 <addr1@kde.org>" ); + check( "second email", emails[1], "addr2 <addr2-alternate@kde.org>" ); + check( "third email", emails[2], "addr2 <addr2@kde.org>" ); + + // Commit changes to the addressbook !! + ab->insertAddressee( dl ); +} + +void TestDistrList::testRemoveEntry() +{ + kdDebug() << k_funcinfo << endl; + KABC::AddressBook *ab = KABC::StdAddressBook::self(); + DistributionList dl = DistributionList::findByName( ab, "foo" ); + assert( !dl.isEmpty() ); + DistributionList::Entry::List entries = dl.entries( ab ); + check( "entries count before removeEntry", TQString::number( entries.count() ), "3" ); + + // Removing an empty entry shouldn't do anything + dl.removeEntry( KABC::Addressee() ); + check( "entries count after removing empty entry", TQString::number( dl.entries(ab).count() ), "3" ); + + KABC::Addressee addr1 = ab->findByName( "addr1" ).first(); + assert( !addr1.isEmpty() ); + // Removing an entry with the wrong email passed, shouldn't do anything + dl.removeEntry( addr1, "foo@foobar.com" ); + check( "entries count after removing entry with invalid email", TQString::number( dl.entries(ab).count() ), "3" ); + + // Now remove entry correctly + dl.removeEntry( addr1 ); + check( "removeEntry(addr1) worked", TQString::number( dl.entries(ab).count() ), "2" ); + TQStringList emails = dl.emails( ab ); + assert( emails.count() == 2 ); + check( "first email", emails[0], "addr2 <addr2-alternate@kde.org>" ); + + // Now move on to addr2. First remove with no or a wrong email (nothing should happen) + KABC::Addressee addr2 = ab->findByName( "addr2" ).first(); + assert( !addr2.isEmpty() ); + dl.removeEntry( addr2 ); + check( "entries count after removing entry with no email", TQString::number( dl.entries(ab).count() ), "2" ); + + // Now remove addr2 correctly + dl.removeEntry( addr2, "addr2@kde.org" ); + check( "entries count after removing addr2", TQString::number( dl.entries(ab).count() ), "1" ); + dl.removeEntry( addr2, "addr2-alternate@kde.org" ); + check( "entries count after removing alternate addr2", TQString::number( dl.entries(ab).count() ), "0" ); + assert( dl.entries(ab).isEmpty() ); + assert( dl.emails(ab).isEmpty() ); + assert( DistributionList::isDistributionList( dl ) ); + + ab->insertAddressee( dl ); +} + +void TestDistrList::testDuplicate() +{ + kdDebug() << k_funcinfo << endl; + // This is a special test for the case where we have a contact and a distr list with the same name + KABC::AddressBook *ab = KABC::StdAddressBook::self(); + KABC::Addressee addr; + addr.setName( "foo" ); + addr.insertEmail( "foo@kde.org", true ); + ab->insertAddressee( addr ); + +#if 0 // we need a findByFormattedName + KABC::Addressee::List addrList = ab->findByName( "foo" ); + assert( addrList.count() == 2 ); + + bool a = DistributionList::isDistributionList( addrList.first() ); + bool b = DistributionList::isDistributionList( addrList.last() ); + // one is a distr list, but not both + assert( a || b ); + // + assert( ! ( a && b ) ); +#endif + + DistributionList dl = DistributionList::findByName( ab, "foo" ); + assert( !dl.isEmpty() ); + assert( DistributionList::isDistributionList( dl ) ); + assert( dl.formattedName() == "foo" ); +} + +void TestDistrList::testDeleteList() +{ + kdDebug() << k_funcinfo << endl; + + KABC::AddressBook *ab = KABC::StdAddressBook::self(); + DistributionList dl = DistributionList::findByName( ab, "foo" ); + assert( !dl.isEmpty() ); + ab->removeAddressee( dl ); + dl = DistributionList::findByName( ab, "foo" ); + assert( dl.isEmpty() ); +} + +#include "testdistrlist.moc" diff --git a/libtdepim/tests/testdistrlist.h b/libtdepim/tests/testdistrlist.h new file mode 100644 index 000000000..3f64527e6 --- /dev/null +++ b/libtdepim/tests/testdistrlist.h @@ -0,0 +1,47 @@ +/* This file is part of the KDE project + Copyright (C) 2004 David Faure <faure@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. +*/ + +#ifndef TESTDISTRLIST_H +#define TESTDISTRLIST_H + +#include <tqobject.h> + +class TestDistrList : public TQObject +{ + Q_OBJECT + TQ_OBJECT + +public: + TestDistrList() {} + void setup(); + void runAll(); + void cleanup(); + + // tests + void testEmpty(); + void testNewList(); + void testInsertEntry(); + void testRemoveEntry(); + void testDuplicate(); + void testDeleteList(); + +private: + bool check(const TQString& txt, TQString a, TQString b); +}; + +#endif diff --git a/libtdepim/tests/testkincidencechooser.cpp b/libtdepim/tests/testkincidencechooser.cpp new file mode 100644 index 000000000..1204f8dc2 --- /dev/null +++ b/libtdepim/tests/testkincidencechooser.cpp @@ -0,0 +1,45 @@ +/* + Copyright (C) 2009 Allen Winter <winter@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 <kapplication.h> +#include <kcmdlineargs.h> +#include <klocale.h> + +#include <libkcal/event.h> +using namespace KCal; + +#include "kincidencechooser.h" +using namespace KPIM; + +int main( int argc, char **argv ) +{ + KCmdLineArgs::init( argc, argv, "testkincidencechooser", 0, + "KIncidenceChooserTest", "1.0", + "kincidencechooser test app" ); + KApplication app; + KIncidenceChooser *chooser = new KIncidenceChooser(); + + Event event; + event.setSummary( i18n( "Meeting" ) ); + event.setDescription( i18n( "Discuss foo" ) ); + chooser->setIncidence( &event, &event ); + chooser->resize( 600, 600 ); + chooser->show(); + return app.exec(); +} diff --git a/libtdepim/tests/testlinklocator.cpp b/libtdepim/tests/testlinklocator.cpp new file mode 100644 index 000000000..548f61a9b --- /dev/null +++ b/libtdepim/tests/testlinklocator.cpp @@ -0,0 +1,114 @@ +/* This file is part of the KDE project + Copyright (C) 2005 Ingo Kloecker <kloecker@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. +*/ + +// Test program for libtdepim/linklocator.* +#include <linklocator.h> + +#include <kcmdlineargs.h> +#include <kapplication.h> +#include <kdebug.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> + +static bool check(const TQString& txt, const TQString& a, const TQString& b) +{ + if (a == b) { + kdDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'... " << "ok" << endl; + } + else { + kdDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'... " << "KO !" << endl; + exit(1); + } + return true; +} + +static bool checkGetEmailAddress( const TQString & input, + int atPos, + const TQString & expRetVal, + bool allowBadAtPos = false ) +{ + if ( !allowBadAtPos && ( input[atPos] != '@' ) ) { + kdDebug() << "atPos (" << atPos << ") doesn't point to '@' in \"" + << input << "\". Fix the check!" << endl; + exit(1); + } + LinkLocator ll( input, atPos ); + const TQString retVal = ll.getEmailAddress(); + check( "getEmailAddress() \"" + input + "\", " + TQString::number( atPos ), + retVal, expRetVal ); + return true; +} + +int main(int argc, char *argv[]) +{ + KApplication::disableAutoDcopRegistration(); + KCmdLineArgs::init( argc, argv, "testlinklocator", 0, 0, 0, 0 ); + KApplication app( false, false ); + + // empty input + checkGetEmailAddress( TQString(), 0, TQString(), true ); + + // no '@' at scan position + checkGetEmailAddress( "foo@bar.baz", 0, TQString(), true ); + + // '@' in local part + checkGetEmailAddress( "foo@bar@bar.baz", 7, TQString() ); + + // empty local part + checkGetEmailAddress( "@bar.baz", 0, TQString() ); + checkGetEmailAddress( ".@bar.baz", 1, TQString() ); + checkGetEmailAddress( " @bar.baz", 1, TQString() ); + checkGetEmailAddress( ".!#$%&'*+-/=?^_`{|}~@bar.baz", strlen(".!#$%&'*+-/=?^_`{|}~"), TQString() ); + + // allowed special chars in local part of address + checkGetEmailAddress( "a.!#$%&'*+-/=?^_`{|}~@bar.baz", strlen("a.!#$%&'*+-/=?^_`{|}~"), "a.!#$%&'*+-/=?^_`{|}~@bar.baz" ); + + // '@' in domain part + checkGetEmailAddress( "foo@bar@bar.baz", 3, TQString() ); + + // domain part without dot + checkGetEmailAddress( "foo@bar", 3, TQString() ); + checkGetEmailAddress( "foo@bar.", 3, TQString() ); + checkGetEmailAddress( ".foo@bar", 4, TQString() ); + checkGetEmailAddress( "foo@bar ", 3, TQString() ); + checkGetEmailAddress( " foo@bar", 4, TQString() ); + checkGetEmailAddress( "foo@bar-bar", 3, TQString() ); + + // empty domain part + checkGetEmailAddress( "foo@", 3, TQString() ); + checkGetEmailAddress( "foo@.", 3, TQString() ); + checkGetEmailAddress( "foo@-", 3, TQString() ); + + // simple address + checkGetEmailAddress( "foo@bar.baz", 3, "foo@bar.baz" ); + checkGetEmailAddress( "foo@bar.baz.", 3, "foo@bar.baz" ); + checkGetEmailAddress( ".foo@bar.baz", 4, "foo@bar.baz" ); + checkGetEmailAddress( "foo@bar.baz-", 3, "foo@bar.baz" ); + checkGetEmailAddress( "-foo@bar.baz", 4, "foo@bar.baz" ); + checkGetEmailAddress( "foo@bar.baz ", 3, "foo@bar.baz" ); + checkGetEmailAddress( " foo@bar.baz", 4, "foo@bar.baz" ); + checkGetEmailAddress( "foo@bar-bar.baz", 3, "foo@bar-bar.baz" ); + + printf("\nTest OK !\n"); + + return 0; +} + diff --git a/libtdepim/tests/testutf7decoder.cpp b/libtdepim/tests/testutf7decoder.cpp new file mode 100644 index 000000000..b13fb30c3 --- /dev/null +++ b/libtdepim/tests/testutf7decoder.cpp @@ -0,0 +1,26 @@ +#include "qutf7codec.h" +#include "qutf7codec.cpp" +#include <tqtextstream.h> +#include <string.h> +#include <assert.h> + +int main( int argc, char * argv[] ) { + if ( argc == 1 ) { + (void)new TQUtf7Codec; + + TQTextCodec * codec = TQTextCodec::codecForName("utf-7"); + assert(codec); + + TQTextIStream my_cin(stdin); + my_cin.setCodec(codec); + + TQTextOStream my_cout(stdout); + + TQString buffer = my_cin.read(); + + my_cout << buffer; + } else { + qWarning("usage: testutf7decoder string_to_decode\n"); + } + TQTextCodec::deleteAllCodecs(); +} diff --git a/libtdepim/tests/testutf7encoder.cpp b/libtdepim/tests/testutf7encoder.cpp new file mode 100644 index 000000000..2938cd660 --- /dev/null +++ b/libtdepim/tests/testutf7encoder.cpp @@ -0,0 +1,93 @@ +#include "qutf7codec.h" +#include "qutf7codec.cpp" +#include <iostream.h> +#include <string.h> + +void main( int argc, char * argv[] ) { + if ( argc == 2 ) { + TQUtf7Codec * codec = new TQUtf7Codec; + + TQTextEncoder * enc; + + TQString arg = TQString::tqfromLatin1( argv[1] ); + int len; + + cout << "Original string:\n" + << "\"" << argv[1] << "\"\n" << endl; + + cout << "Encode optional direct set and whitespace:\n" << endl; + codec->setEncodeWhitespace(TRUE); + codec->setEncodeOptionalDirect(TRUE); + enc = codec->makeEncoder(); + + len = arg.length(); + cout << (enc->fromUnicode( arg, len )).data() + << "\n" << endl; + + cout << "Same as above, but call fromUnicode() char-wise:\n" << endl; + + delete enc; + enc = codec->makeEncoder(); + + for ( int i = 0 ; i < arg.length() ; i++ ) { + len = 1; + cout << (enc->fromUnicode( TQString(arg[i]), len )).data(); + } + cout << "\n" << endl; + + + + delete enc; + + cout << "Encode optional direct set and not whitespace:\n" << endl; + codec->setEncodeWhitespace(FALSE); + codec->setEncodeOptionalDirect(TRUE); + enc = codec->makeEncoder(); + + len = arg.length(); + cout << (enc->fromUnicode( arg, len )).data() + << "\n" << endl; + + delete enc; + + + cout << "Don't encode optional direct set, but whitespace:\n" << endl; + codec->setEncodeWhitespace(TRUE); + codec->setEncodeOptionalDirect(FALSE); + enc = codec->makeEncoder(); + + len = arg.length(); + cout << (enc->fromUnicode( arg, len )).data() + << "\n" << endl; + + delete enc; + + + cout << "Encode neither optional direct set, nor whitespace:\n" << endl; + codec->setEncodeWhitespace(FALSE); + codec->setEncodeOptionalDirect(FALSE); + enc = codec->makeEncoder(); + + len = arg.length(); + cout << (enc->fromUnicode( arg, len )).data() + << "\n" << endl; + + cout << "Same as above, but call fromUnicode() char-wise:\n" << endl; + + delete enc; + enc = codec->makeEncoder(); + + for ( int i = 0 ; i < arg.length() ; i++ ) { + len = 1; + cout << (enc->fromUnicode( TQString(arg[i]), len )).data(); + } + cout << "\n" << endl; + + + delete enc; + + delete codec; + } else { + qWarning("usage: testutf7encoder string_to_encode\n"); + } +} diff --git a/libtdepim/tests/testutf7encoder2.cpp b/libtdepim/tests/testutf7encoder2.cpp new file mode 100644 index 000000000..65cf8b461 --- /dev/null +++ b/libtdepim/tests/testutf7encoder2.cpp @@ -0,0 +1,45 @@ +#include "qutf7codec.h" +#include "qutf7codec.cpp" +#include <tqtextstream.h> +#include <string.h> +#include <assert.h> +#include <iostream> + +int main( int argc, char * argv[] ) { + if ( argc == 1 ) { + (void)new TQUtf7Codec; + + TQTextCodec * codec = TQTextCodec::codecForName("utf-7"); + assert(codec); + + TQTextIStream my_cin(stdin); + + TQTextOStream my_cout(stdout); + my_cout.setCodec(codec); + + TQString buffer = my_cin.read(); + + // qDebug("buffer == " + buffer); + +#ifdef USE_STREAM + my_cout << buffer << endl; +#else + TQTextEncoder * enc = codec->makeEncoder(); +#ifdef CHAR_WISE + int len; + for ( int i = 0 ; i < buffer.length() ; i++ ) { + len = 1; + cout << (enc->fromUnicode(TQString(buffer[i]),len)).data(); + } + std::cout << std::endl; +#else + int len = buffer.length(); + std::cout << (enc->fromUnicode(buffer,len)).data() << std::endl;; +#endif // CHAR_WISE + delete enc; +#endif // else USE_STREAM + } else { + qWarning("usage: testutf7encoder2 < infile > outfile\n"); + } + TQTextCodec::deleteAllCodecs(); +} diff --git a/libtdepim/tests/testwizard.cpp b/libtdepim/tests/testwizard.cpp new file mode 100644 index 000000000..d9c9d806c --- /dev/null +++ b/libtdepim/tests/testwizard.cpp @@ -0,0 +1,100 @@ +/* + This file is part of libtdepim. + + Copyright (c) 2003 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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 "myconfig.h" + +#include <kconfigwizard.h> + +#include <kaboutdata.h> +#include <kapplication.h> +#include <kdebug.h> +#include <klocale.h> +#include <kcmdlineargs.h> + +#include <tqlayout.h> +#include <tqcheckbox.h> + +class TestConfigWizard : public KConfigWizard +{ + public: + TestConfigWizard() : + KConfigWizard( new KConfigPropagator( MyConfig::self(), + "propagator_test.kcfg" ) ) + { + TQFrame *page = createWizardPage( "My Wizard Page" ); + TQBoxLayout *topLayout = new TQVBoxLayout( page ); + + mFixKMailCheckBox = new TQCheckBox( i18n("Fix KMail"), page ); + topLayout->addWidget( mFixKMailCheckBox ); + + mFixKMailCheckBox->setChecked( MyConfig::fixKMail() ); + + mBreakKMailCheckBox = new TQCheckBox( i18n("Break KMail"), page ); + topLayout->addWidget( mBreakKMailCheckBox ); + + mBreakKMailCheckBox->setChecked( MyConfig::breakKMail() ); + + setupRulesPage(); + setupChangesPage(); + } + + ~TestConfigWizard() + { + } + + void usrReadConfig() + { + } + + void usrWriteConfig() + { + MyConfig::self()->setFixKMail( mFixKMailCheckBox->isChecked() ); + MyConfig::self()->setBreakKMail( mBreakKMailCheckBox->isChecked() ); + } + + private: + TQCheckBox *mFixKMailCheckBox; + TQCheckBox *mBreakKMailCheckBox; +}; + +static const KCmdLineOptions options[] = +{ + {"verbose", "Verbose output", 0}, + KCmdLineLastOption +}; + +int main(int argc,char **argv) +{ + KAboutData aboutData("testwizard","Test KConfigWizard","0.1"); + KCmdLineArgs::init(argc,argv,&aboutData); + KCmdLineArgs::addCmdLineOptions( options ); + + KApplication app; + + KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); + + bool verbose = false; + if ( args->isSet( "verbose" ) ) verbose = true; + + TestConfigWizard wizard; + + wizard.exec(); +} diff --git a/libtdepim/weaver.cpp b/libtdepim/weaver.cpp new file mode 100644 index 000000000..b1dba8de7 --- /dev/null +++ b/libtdepim/weaver.cpp @@ -0,0 +1,550 @@ +/* -*- C++ -*- + + This file implements the Weaver, Job and Thread classes. + + $ Author: Mirko Boehm $ + $ Copyright: (C) 2004, Mirko Boehm $ + $ Contact: mirko@kde.org + http://www.kde.org + http://www.hackerbuero.org $ + $ License: LGPL with the following explicit clarification: + This code may be linked against any version of the TQt toolkit + from Troll Tech, Norway. $ + +*/ + +extern "C" { +#include <signal.h> +} + +#include <tqevent.h> +#include <tqapplication.h> + +#include "weaver.h" + +namespace KPIM { +namespace ThreadWeaver { + + bool Debug = true; + int DebugLevel = 2; + + Job::Job (TQObject* parent, const char* name) + : TQObject (parent, name), + m_finished (false), + m_mutex (new TQMutex (true) ), + m_thread (0) + { + } + + Job::~Job() + { + } + + void Job::lock() + { + m_mutex->lock(); + } + + void Job::unlock() + { + m_mutex->unlock(); + } + + void Job::execute(Thread *th) + { + m_mutex->lock(); + m_thread = th; + m_mutex->unlock(); + + run (); + + m_mutex->lock(); + setFinished (true); + m_thread = 0; + m_mutex->unlock(); + } + + Thread *Job::thread () + { + TQMutexLocker l (m_mutex); + return m_thread; + } + + bool Job::isFinished() const + { + TQMutexLocker l (m_mutex); + return m_finished; + } + + void Job::setFinished(bool status) + { + TQMutexLocker l (m_mutex); + m_finished = status; + } + + void Job::processEvent (Event *e) + { + switch ( e->action() ) + { + case Event::JobStarted: + emit ( started() ); + break; + case Event::JobFinished: + emit ( done() ); + break; + case Event::JobSPR: + emit ( SPR () ); + m_wc->wakeOne (); + break; + case Event::JobAPR: + emit ( APR () ); + // no wake here ! + break; + default: + break; + } + } + + void Job::triggerSPR () + { + m_mutex->lock (); + m_wc = new TQWaitCondition; + m_mutex->unlock (); + + thread()->post (KPIM::ThreadWeaver::Event::JobSPR, this); + m_wc->wait (); + + m_mutex->lock (); + delete m_wc; + m_wc = 0; + m_mutex->unlock (); + } + + void Job::triggerAPR () + { + m_mutex->lock (); + m_wc = new TQWaitCondition; + m_mutex->unlock (); + + thread()->post (KPIM::ThreadWeaver::Event::JobAPR, this); + m_wc->wait (); + } + + void Job::wakeAPR () + { + TQMutexLocker l(m_mutex); + if ( m_wc!=0 ) + { + m_wc->wakeOne (); + delete m_wc; + m_wc = 0; + } + } + + const int Event::Type = TQEvent::User + 1000; + + Event::Event ( Action action, Thread *thread, Job *job) + : TQCustomEvent ( type () ), + m_action (action), + m_thread (thread), + m_job (job) + { + } + + int Event::type () + { + return Type; + } + + Thread* Event::thread () const + { + if ( m_thread != 0) + { + return m_thread; + } else { + return 0; + } + } + + Job* Event::job () const + { + return m_job; + } + + Event::Action Event::action () const + { + return m_action; + } + + unsigned int Thread::sm_Id; + + Thread::Thread (Weaver *parent) + : TQThread (), + m_parent ( parent ), + m_id ( makeId() ) + { + } + + Thread::~Thread() + { + } + + unsigned int Thread::makeId() + { + static TQMutex mutex; + TQMutexLocker l (&mutex); + + return ++sm_Id; + } + + unsigned int Thread::id() const + { + return m_id; + } + + void Thread::run() + { + Job *job = 0; + + post ( Event::ThreadStarted ); + + while (true) + { + debug ( 3, "Thread::run [%u]: trying to execute the next job.\n", id() ); + + job = m_parent->applyForWork ( this, job ); + + if (job == 0) + { + break; + } else { + post ( Event::JobStarted, job ); + job->execute (this); + post ( Event::JobFinished, job ); + } + } + + post ( Event::ThreadExiting ); + } + + void Thread::post (Event::Action a, Job *j) + { + m_parent->post ( a, this, j); + } + + void Thread::msleep(unsigned long msec) + { + TQThread::msleep(msec); + } + + Weaver::Weaver(TQObject* parent, const char* name, + int inventoryMin, int inventoryMax) + : TQObject(parent, name), + m_active(0), + m_inventoryMin(inventoryMin), + m_inventoryMax(inventoryMax), + m_shuttingDown(false), + m_running (false), + m_suspend (false), + m_mutex ( new TQMutex(true) ) + { + lock(); + + for ( int count = 0; count < m_inventoryMin; ++count) + { + Thread *th = new Thread(this); + m_inventory.append(th); + // this will idle the thread, waiting for a job + th->start(); + + emit (threadCreated (th) ); + } + + unlock(); + } + + Weaver::~Weaver() + { + lock(); + + debug ( 1, "Weaver dtor: destroying inventory.\n" ); + + m_shuttingDown = true; + + unlock(); + + m_jobAvailable.wakeAll(); + + // problem: Some threads might not be asleep yet, just finding + // out if a job is available. Those threads will suspend + // waiting for their next job (a rare case, but not impossible). + // Therefore, if we encounter a thread that has not exited, we + // have to wake it again (which we do in the following for + // loop). + + for ( Thread *th = m_inventory.first(); th; th = m_inventory.next() ) + { + if ( !th->finished() ) + { + m_jobAvailable.wakeAll(); + th->wait(); + } + + emit (threadDestroyed (th) ); + delete th; + + } + + m_inventory.clear(); + + delete m_mutex; + + debug ( 1, "Weaver dtor: done\n" ); + + } + + void Weaver::lock() + { + debug ( 3 , "Weaver::lock: lock (mutex is %s).\n", + ( m_mutex->locked() ? "locked" : "not locked" ) ); + m_mutex->lock(); + } + + void Weaver::unlock() + { + m_mutex->unlock(); + + debug ( 3 , "Weaver::unlock: unlock (mutex is %s).\n", + ( m_mutex->locked() ? "locked" : "not locked" ) ); + } + + int Weaver::threads () const + { + TQMutexLocker l (m_mutex); + return m_inventory.count (); + } + + void Weaver::enqueue(Job* job) + { + lock(); + + m_assignments.append(job); + m_running = true; + + unlock(); + + assignJobs(); + } + + void Weaver::enqueue (TQPtrList <Job> jobs) + { + lock(); + + for ( Job * job = jobs.first(); job; job = jobs.next() ) + { + m_assignments.append (job); + } + + unlock(); + + assignJobs(); + } + + bool Weaver::dequeue ( Job* job ) + { + TQMutexLocker l (m_mutex); + return m_assignments.remove (job); + } + + void Weaver::dequeue () + { + TQMutexLocker l (m_mutex); + m_assignments.clear(); + } + + void Weaver::suspend (bool state) + { + lock(); + + if (state) + { + // no need to wake any threads here + m_suspend = true; + if ( m_active == 0 && isEmpty() ) + { // instead of waking up threads: + post (Event::Suspended); + } + } else { + m_suspend = false; + // make sure we emit suspended () even if all threads are sleeping: + assignJobs (); + debug (2, "Weaver::suspend: queueing resumed.\n" ); + } + + unlock(); + } + + void Weaver::assignJobs() + { + m_jobAvailable.wakeAll(); + } + + bool Weaver::event (TQEvent *e ) + { + if ( e->type() >= TQEvent::User ) + { + + if ( e->type() == Event::type() ) + { + Event *event = (Event*) e; + + switch (event->action() ) + { + case Event::JobFinished: + if ( event->job() !=0 ) + { + emit (jobDone (event->job() ) ); + } + break; + case Event::Finished: + emit ( finished() ); + break; + case Event::Suspended: + emit ( suspended() ); + break; + case Event::ThreadSuspended: + if (!m_shuttingDown ) + { + emit (threadSuspended ( event->thread() ) ); + } + break; + case Event::ThreadBusy: + if (!m_shuttingDown ) + { + emit (threadBusy (event->thread() ) ); + } + break; + default: + break; + } + + if ( event->job() !=0 ) + { + event->job()->processEvent (event); + } + } else { + debug ( 0, "Weaver::event: Strange: received unknown user event.\n" ); + } + return true; + } else { + // others - please make sure we are a TQObject! + return TQObject::event ( e ); + } + } + + void Weaver::post (Event::Action a, Thread* t, Job* j) + { + Event *e = new Event ( a, t, j); + TQApplication::postEvent (this, e); + } + + bool Weaver::isEmpty() const + { + TQMutexLocker l (m_mutex); + return m_assignments.count()==0; + } + + Job* Weaver::applyForWork(Thread *th, Job* previous) + { + Job *rc = 0; + bool lastjob = false; + bool suspended = false; + + while (true) + { + lock(); + + if (previous != 0) + { // cleanup and send events: + --m_active; + + debug ( 3, "Weaver::applyForWork: job done, %i jobs left, " + "%i active jobs left.\n", + queueLength(), m_active ); + + if ( m_active == 0 && isEmpty() ) + { + lastjob = true; + m_running = false; + post (Event::Finished); + debug ( 3, "Weaver::applyForWork: last job.\n" ); + } + + if (m_active == 0 && m_suspend == true) + { + suspended = true; + post (Event::Suspended); + debug ( 2, "Weaver::applyForWork: queueing suspended.\n" ); + } + + m_jobFinished.wakeOne(); + } + + previous = 0; + + if (m_shuttingDown == true) + { + unlock(); + + return 0; + } else { + if ( !isEmpty() && m_suspend == false ) + { + rc = m_assignments.getFirst(); + m_assignments.removeFirst (); + ++m_active; + + debug ( 3, "Weaver::applyForWork: job assigned, " + "%i jobs in queue (%i active).\n", + m_assignments.count(), m_active ); + unlock(); + + post (Event::ThreadBusy, th); + + return rc; + } else { + unlock(); + + post (Event::ThreadSuspended, th); + m_jobAvailable.wait(); + } + } + } + } + + int Weaver::queueLength() + { + TQMutexLocker l (m_mutex); + return m_assignments.count(); + } + + bool Weaver::isIdle () const + { + TQMutexLocker l (m_mutex); + return isEmpty() && m_active == 0; + } + + void Weaver::finish() + { + while ( !isIdle() ) + { + debug (2, "Weaver::finish: not done, waiting.\n" ); + m_jobFinished.wait(); + } + debug (1, "Weaver::finish: done.\n\n\n" ); + } + +} +} + +#include "weaver.moc" diff --git a/libtdepim/weaver.h b/libtdepim/weaver.h new file mode 100644 index 000000000..8e82b5811 --- /dev/null +++ b/libtdepim/weaver.h @@ -0,0 +1,451 @@ +/* -*- C++ -*- + + This file declares the Weaver, Job and Thread classes. + + $ Author: Mirko Boehm $ + $ Copyright: (C) 2004, Mirko Boehm $ + $ Contact: mirko@kde.org + http://www.kde.org + http://www.hackerbuero.org $ + $ License: LGPL with the following explicit clarification: + This code may be linked against any version of the TQt toolkit + from Troll Tech, Norway. $ + +*/ + +#ifndef WEAVER_H +#define WEAVER_H + +extern "C" +{ +#include <stdarg.h> +#include <unistd.h> +#include <stdio.h> +} + +#include <tqobject.h> +#include <tqptrlist.h> +#include <tqthread.h> +#include <tqwaitcondition.h> +#include <tqmutex.h> +#include <tqevent.h> + +#include <tdepimmacros.h> + +namespace KPIM { +namespace ThreadWeaver { + + /** This method prints a text message on the screen, if debugging is + enabled. Otherwise, it does nothing. The message is thread safe, + therefore providing that the messages appear in the order they where + issued by the different threads. + All messages are suppressed when Debug is false. All messages with a + lower importance (higher number) than DebugLevel will be suppressed, + too. Debug level 0 messages will always be printed as long as + Debug is true. + We use our own debugging method, since debugging threads is a more + complicated experience than debugging single threaded + contexts. This might change in future in the way that debug + prints it's messages to another logging facility provided by + the platform. + Use setDebugLevel () to integrate adapt debug () to your platform. + */ + + KDE_EXPORT extern bool Debug; + KDE_EXPORT extern int DebugLevel; + + KDE_EXPORT inline void setDebugLevel (bool debug, int level) + { + Debug = debug; + DebugLevel = level; + } + + KDE_EXPORT inline void debug(int severity, const char * cformat, ...) +#ifdef __GNUC__ + __attribute__ ( (format (printf, 2, 3 ) ) ) +#endif +; + + KDE_EXPORT inline void debug(int severity, const char * cformat, ...) + { + if ( Debug == true && ( severity<=DebugLevel || severity == 0) ) + { + static TQMutex mutex; + TQString text; + + mutex.lock(); + va_list ap; + va_start( ap, cformat ); + vprintf (cformat, ap); + va_end (ap); + mutex.unlock(); + } + } + + + class Thread; + class Job; + + /** A class to represent the events threads generate and send to the + Weaver object. Examples include the start or end of the processing of a + job. Threads create the event objects and discard them after posting + the event, since the event receiver will assume ownership of the + event. + Events are associated to the sending thread and possibly to a + processed job. + + Note: Do not create and use SPR/APR events, use Job::triggerSPR or + Job::triggerAPR to create the requests. */ + + class KDE_EXPORT Event : public TQCustomEvent + { + public: + enum Action { + NoAction = 0, + Finished, /// All jobs in the queue are done. + Suspended, /// Thread queueing halted. + ThreadStarted, + ThreadExiting, + ThreadBusy, + ThreadSuspended, + JobStarted, + JobFinished, + JobSPR, /// Synchronous Process Request + JobAPR /// Asynchronous Process Request + }; + Event ( Action = NoAction, Thread * = 0, Job *job = 0); + /** Return the (custom defined) event type. */ + static int type (); + /** The ID of the sender thread. */ + Thread* thread () const; + /** The associated job. */ + Job* job () const; + /** The action. */ + Action action () const; + private: + Action m_action; + Thread *m_thread; + Job *m_job; + static const int Type; + }; + + /** A Job is a simple abstraction of an action that is to be + executed in a thread context. + It is essential for the ThreadWeaver library that as a kind of + convention, the different creators of Job objects do not touch the + protected data members of the Job until somehow notified by the + Job. See the SPR signal for an example. + + Jobs may emit process requests as signals. Consider process requests + as a kind of synchronized call to the main thread. + Process Requests are a generic means for Job derivate programmers to have + the jobs interact with the creators (in the main thread) during + processing time. To avoid race + conditions and extensive locking and unlocking, the thread executing the + job is suspended during the period needed to process the request. + + There are two kinds of process requests (we introduce abbreviations, + also in the signal names and the code, + only to save typing). Both are emitted by signals in the main thread: + - Synchronous Process Requests (SPR): Synchronous requests expect that the + complete request is performed in the slots connected to the signals. For + example, to update a widget according to the progress of the job, a SPR + may be used. In such cases, the Job's execution will be resumed + immediately after the signal has been processed. + - Asynchronous Process Requests (APR): For APRs, the job emitting the + signal does not assume anything about the amount of time needed to + perform the operation. Therefore, the thread is not waked after the + signal returns. The creator has to wake to thread whenever it is + ready by calling the wakeAPR method. + + Note: When using an APR, you better make sure to receive the signal + with some object, otherwise the calling thread will block forever! + */ + class KDE_EXPORT Job : public TQObject + { + Q_OBJECT + TQ_OBJECT + public: + /** Construct a Job object. */ + Job(TQObject* parent=0, const char* name=0); + + /** Destructor. */ + virtual ~Job(); + + /** Perform the job. The thread in which this job is executed + is given as a parameter. + Do not overload this method to create your own Job + implementation, overload run(). */ + virtual void execute(Thread*); + + /** Returns true if the jobs's execute method finished. */ + virtual bool isFinished() const; + + /** Wake the thread after an APR has been processed. */ + void wakeAPR (); + + /** Process events related to this job (created by the processing + thread or the weaver or whoever). */ + virtual void processEvent ( Event* ); + + signals: + /** This signal is emitted when a thread starts to process a job. */ + void started (); + /** This signal is emitted when a job has been finished. */ + void done (); + /** This signal is emitted when the job needs some operation done by + the main thread (usually the creator of the job). + It is important to understand that the emitting thread is + suspended until the signal returns. + When + the operation requested has been performed and this signal is + finished, the thread is automatically waked. + What operation needs to be performed has to be negotiated between + the two objects. + Note: This signal is an attempt to provide job programmers with a + generic way to interact while the job is executed. I am interested + in feedback about it's use. */ + void SPR (); + /** Perform an Asynchronous Process Request. See SPR and the generic + Job documentation for a comparison. */ + void APR (); + protected: + /** Lock this Job's mutex. */ + void lock(); + /** Unlock this Job's mutex. */ + void unlock(); + /** The method that actually performs the job. It is called from + execute(). This method is the one to overload it with the + job's task. */ + virtual void run () = 0; + /** Return the thread that executes this job. + Returns zero of the job is not currently executed. */ + Thread *thread(); + /** Call with status = true to mark this job as done. */ + virtual void setFinished(bool status); + /** Trigger a SPR. + This emits a signal in the main thread indicating the necessity of + a synchronized operation. */ + void triggerSPR (); + /** Trigger an APR. + This emit a signal in the main thread indicating the necessity of + an unsynchronized operation. + The calling thread needs to ensure to wake the thread when the + operation is done. */ + void triggerAPR (); + + bool m_finished; + + TQMutex *m_mutex; + + Thread * m_thread; + + TQWaitCondition *m_wc; + }; + + class Weaver; + + /** The class Thread is used to represent the worker threads in + the weaver's inventory. It is not meant to be overloaded. */ + class KDE_EXPORT Thread : public TQThread + { + public: + /** Create a thread. + These thread objects are only used inside the Weaver parent + object. */ + Thread(Weaver *parent); + + /** The destructor. */ + ~Thread(); + + /** Overloaded to execute the assigned job. + This will NOT return until shutdown() is called. The + thread will try to execute one job after the other, asking + the Weaver parent for a new job when the assigned one is + finished. + If no jobs are available, the thread will suspend. + After shutdown() is called, the thread will end as soon as + the currently assigned job is done. + */ + void run(); + + /* Provide the msleep() method (protected in TQThread) to be + available for executed jobs. */ + void msleep(unsigned long msec); + + /** Returns the thread id. + This id marks the respective Thread object, and must + therefore not be confused with, e.g., the pthread thread + ID. */ + unsigned int id() const; + + /** Post an event, will be received and processed by the Weaver. */ + void post (Event::Action, Job* = 0); + + private: + Weaver *m_parent; + + const unsigned int m_id; + + static unsigned int sm_Id; + + static unsigned int makeId(); + }; + + /** A weaver is the manager of worker threads (Thread objects) to + which it assigns jobs from it's queue. */ + class KDE_EXPORT Weaver : public TQObject + { + Q_OBJECT + TQ_OBJECT + public: + Weaver (TQObject* parent=0, const char* name=0, + int inventoryMin = 4, // minimal number of provided threads + int inventoryMax = 32); // maximum number of provided threads + virtual ~Weaver (); + /** Add a job to be executed. */ + virtual void enqueue (Job*); + /** Enqueue all jobs in the given list. + This is an atomic operation, no jobs will start + before all jobs in the list are enqueued. + If you need a couple of jobs done and want to receive the + finished () signal afterwards, use this method to queue + them. Otherwise, when enqueueing your jobs + individually, there is a chance that you receive more than + one finished signal. */ + void enqueue (TQPtrList<Job> jobs); + /** Remove a job from the queue. + If the job qas queued but not started so far, it is simple + removed from the queue. For now, it is unsupported to + dequeue a job once its execution has started. + For that case, you will have to provide a method to interrupt your + job's execution (and receive the done signal). + Returns true if the job has been dequeued, false if the + job has already been started or is not found in the + queue. */ + virtual bool dequeue (Job*); + /** Remove all queued jobs. + Please note that this will not kill the threads, therefore + all jobs that are being processed will be continued. */ + virtual void dequeue (); + /** Get notified when a thread has finished a job. + This is done automatically. */ + // virtual void jobFinished(Thread *); + /** Finish all queued operations, then return. + This method is used in imperative programs that cannot react on + events to have the controlling (main) thread wait wait for the + jobs to finish. + Warning: This will suspend your thread! + Warning: If your jobs enter for example an infinite loop, this + will never return! */ + virtual void finish(); + /** Suspend job execution if state = true, otherwise resume + job execution if it was suspended. + When suspending, all threads are allowed to finish the + currently assigned job but will not receive a new + assignment. + When all threads are done processing the assigned job, the + signal suspended will() be emitted. + If you call suspend (true) and there are no jobs left to + be done, you will immidiately receive the suspended() + signal. */ + virtual void suspend (bool state); + /** Is the queue empty? */ + bool isEmpty () const; + /** Is the weaver idle? + The weaver is idle if no jobs are queued and no jobs are processed + by the threads (m_active is zero). */ + bool isIdle () const; + /** Returns the number of pending jobs. */ + int queueLength (); + /** Assign a job to the calling thread. + This is supposed to be called from the Thread objects in + the inventory. + Returns 0 if the weaver is shutting down, telling the + calling thread to finish and exit. + If no jobs are available and shut down is not in progress, + the calling thread is suspended until either condition is + met. + In previous, threads give the job they have completed. If this is + the first job, previous is zero. */ + virtual Job* applyForWork (Thread *thread, Job *previous); + /** Lock the mutex for this weaver. The threads in the + inventory need to lock the weaver's mutex to synchronize + the job management. */ + void lock (); + /** Unlock. See lock(). */ + void unlock (); + /** Post an event that is handled by this object, but in the main + (GUI) thread. Different threads may use this method to communicate + with the main thread. + thread and job mark the objects associated with this event. */ + void post (Event::Action, Thread* = 0, Job* = 0); + /** Returns the current number of threads in the inventory. */ + int threads () const; + signals: + /** This signal is emitted when the Weaver has finished ALL currently + queued jobs. + If a number of jobs is enqueued sequentially, this signal might be + emitted a couple of times (what happens is that all already queued + jobs have been processed while you still add new ones). This is + not a bug, but the intended behaviour. */ + void finished (); + /** Thread queueing has been suspended. + When suspend is called with state = true, all threads are + allowed to finish their job. When the last thread + finished, this signal is emitted. */ + void suspended (); + /** This signal is emitted when a job is done. It is up to the + programmer if this signal or the done signal of the job is more + handy. */ + void jobDone (Job*); +// The following signals are used mainly for debugging purposes. + void threadCreated (Thread *); + void threadDestroyed (Thread *); + void threadBusy (Thread *); + void threadSuspended (Thread *); + + protected: + /** Schedule enqueued jobs to be executed by idle threads. + This will try to distribute as many jobs as possible + to all idle threads. */ + void assignJobs(); + /** Check incoming events for user defined ones. The threads use user + defined events to communicate with the Weaver. */ + bool event ( TQEvent* ); + /** The thread inventory. */ + TQPtrList<Thread> m_inventory; + /** The job queue. */ + TQPtrList<Job> m_assignments; + /** The number of jobs that are assigned to the worker + threads, but not finished. */ + int m_active; + /** Stored setting. */ + int m_inventoryMin; + /** Stored setting . */ + int m_inventoryMax; + /** Wait condition all idle or done threads wait for. */ + TQWaitCondition m_jobAvailable; + /** Wait for a job to finish. */ + TQWaitCondition m_jobFinished; + /** Indicates if the weaver is shutting down and exiting it's + threads. */ + bool m_shuttingDown; + /** m_running is set to true when a job is enqueued and set to false + when the job finishes that was the last in the queue. + E.g., this will flip from false to true to false when you + continuously enqueue one single job. */ + bool m_running; + /** If m_suspend is true, no new jobs will be assigned to + threads. + Jobs may be queued, but will not be processed until suspend + (false) is called. */ + bool m_suspend; + private: + /** Mutex to serialize operations. */ + TQMutex *m_mutex; + }; +} // namespace ThreadWeaver +} // namespace KPIM + +#endif // defined WEAVER_H diff --git a/libtdepim/weaverextensions.cpp b/libtdepim/weaverextensions.cpp new file mode 100644 index 000000000..9859b6d6b --- /dev/null +++ b/libtdepim/weaverextensions.cpp @@ -0,0 +1,62 @@ +/* -*- C++ -*- + + This file implements the Weaver Extensions basics. + + $ Author: Mirko Boehm $ + $ Copyright: (C) 2004, Mirko Boehm $ + $ Contact: mirko@kde.org + http://www.kde.org + http://www.hackerbuero.org $ + $ License: LGPL with the following explicit clarification: + This code may be linked against any version of the TQt toolkit + from Troll Tech, Norway. $ + +*/ + +#include "weaverextensions.h" +#include "weaver.h" + +namespace KPIM { +namespace ThreadWeaver { + + WeaverExtension::WeaverExtension ( TQObject *parent, const char *name) + : TQObject (parent, name) + { + } + + void WeaverExtension::attach (Weaver *w) + { + connect (w, TQT_SIGNAL (threadCreated (Thread *) ), + TQT_SLOT (threadCreated (Thread *) ) ); + connect (w, TQT_SIGNAL (threadDestroyed (Thread *) ), + TQT_SLOT (threadDestroyed (Thread *) ) ); + connect (w, TQT_SIGNAL (threadBusy (Thread *) ), + TQT_SLOT (threadBusy (Thread *) ) ); + connect (w, TQT_SIGNAL (threadSuspended (Thread *) ), + TQT_SLOT (threadSuspended (Thread *) ) ); + } + + WeaverExtension::~WeaverExtension() + { + } + + void WeaverExtension::threadCreated (Thread *) + { + } + + void WeaverExtension::threadDestroyed (Thread *) + { + } + + void WeaverExtension::threadBusy (Thread *) + { + } + + void WeaverExtension::threadSuspended (Thread *) + { + } + +} +} + +#include "weaverextensions.moc" diff --git a/libtdepim/weaverextensions.h b/libtdepim/weaverextensions.h new file mode 100644 index 000000000..26bccadea --- /dev/null +++ b/libtdepim/weaverextensions.h @@ -0,0 +1,60 @@ +/* -*- C++ -*- + + This file declares the Weaver Extensions basics. + + $ Author: Mirko Boehm $ + $ Copyright: (C) 2004, Mirko Boehm $ + $ Contact: mirko@kde.org + http://www.kde.org + http://www.hackerbuero.org $ + $ License: LGPL with the following explicit clarification: + This code may be linked against any version of the TQt toolkit + from Troll Tech, Norway. $ + +*/ + +#ifndef WEAVEREXTENSIONS_H +#define WEAVEREXTENSIONS_H + +#include <tqobject.h> + +namespace KPIM { +namespace ThreadWeaver { + + class Weaver; + class Thread; + + /** A WeaverExtension can be attached to an existing Weaver object and + will then receive signals on actions the Weaver takes, like starting + to process a specific job, assigning a job to a thread or suspending a + thread. It can be used to monitor the state of a program, but also, + for example, to provide visualization of the Weaver's work load in GUI + programs. Derive from it to actually create an extension. */ + class WeaverExtension : public TQObject + { + Q_OBJECT + TQ_OBJECT + public: + WeaverExtension ( TQObject *parent = 0, const char *name = 0); + /** Attach() is a convenience method that will connect all our + slots to signals emitted by the weaver. It is also possible to + avoid attach() and only connect necessary signals. */ + void attach (Weaver *); + virtual ~WeaverExtension() = 0; + public slots: + // these methods are implemented, but do nothing in the default configuration + // a thread is created: + virtual void threadCreated (Thread *); + // a thread is destroyed: + virtual void threadDestroyed (Thread *); + // the thread is processing a job + virtual void threadBusy (Thread *); + // the thread is suspended and will be waked when jobs become + // available + virtual void threadSuspended (Thread *); + }; + +} // namespace ThreadWeaver +} + +#endif // WEAVEREXTENSIONS_H diff --git a/libtdepim/weaverlogger.cpp b/libtdepim/weaverlogger.cpp new file mode 100644 index 000000000..e5164c804 --- /dev/null +++ b/libtdepim/weaverlogger.cpp @@ -0,0 +1,60 @@ +/* -*- C++ -*- + + This file implements the Thread Logger. + + $ Author: Mirko Boehm $ + $ Copyright: (C) 2004, Mirko Boehm $ + $ Contact: mirko@kde.org + http://www.kde.org + http://www.hackerbuero.org $ + $ License: LGPL with the following explicit clarification: + This code may be linked against any version of the TQt toolkit + from Troll Tech, Norway. $ + +*/ + +#include <weaver.h> +#include "weaverlogger.h" + +namespace KPIM { +namespace ThreadWeaver { + + extern void debug(int severity, const char * cformat, ...); + + WeaverThreadLogger::WeaverThreadLogger( TQObject *parent, const char *name) + : WeaverExtension (parent, name) + { + } + + WeaverThreadLogger::~WeaverThreadLogger () + { + } + + void WeaverThreadLogger::threadCreated (KPIM::ThreadWeaver::Thread *thread) + { + debug ( 1, "WeaverThreadLogger: thread (ID: %i) created.\n", + thread->id() ); + } + + void WeaverThreadLogger::threadDestroyed (KPIM::ThreadWeaver::Thread *thread) + { + debug ( 1, "WeaverThreadLogger: thread (ID: %i) destroyed.\n", + thread->id() ); + } + + void WeaverThreadLogger::threadBusy (KPIM::ThreadWeaver::Thread *thread) + { + debug ( 1, "WeaverThreadLogger: thread (ID: %i) is processing a job.\n", + thread->id() ); + } + + void WeaverThreadLogger::threadSuspended (KPIM::ThreadWeaver::Thread *thread) + { + debug ( 1, "WeaverThreadLogger: thread (ID: %i) suspended.\n", + thread->id() ); + } + +} +} + +#include "weaverlogger.moc" diff --git a/libtdepim/weaverlogger.h b/libtdepim/weaverlogger.h new file mode 100644 index 000000000..d4d35ff4f --- /dev/null +++ b/libtdepim/weaverlogger.h @@ -0,0 +1,42 @@ +/* -*- C++ -*- + + This file declares the Thread Logger. + + $ Author: Mirko Boehm $ + $ Copyright: (C) 2004, Mirko Boehm $ + $ Contact: mirko@kde.org + http://www.kde.org + http://www.hackerbuero.org $ + $ License: LGPL with the following explicit clarification: + This code may be linked against any version of the TQt toolkit + from Troll Tech, Norway. $ + +*/ + +#ifndef WEAVERLOGGER_H +#define WEAVERLOGGER_H + +#include "weaverextensions.h" + +namespace KPIM { +namespace ThreadWeaver { + + /** A WeaverThreadLogger may be attached to a Weaver to gain debug + information about thread execution. */ + class WeaverThreadLogger : public WeaverExtension + { + Q_OBJECT + TQ_OBJECT + public: + WeaverThreadLogger( TQObject *parent = 0, const char *name = 0); + ~WeaverThreadLogger(); + void threadCreated (Thread *); + void threadDestroyed (Thread *); + void threadBusy (Thread *); + void threadSuspended (Thread *); + }; + +} +} + +#endif // WEAVERLOGGER_H |