summaryrefslogtreecommitdiffstats
path: root/kmailcvt/filters.cxx
blob: a406f1c3fbabe3e1cc3186075ca1521c17f45f8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/***************************************************************************
                          filters.cxx  -  description
                             -------------------
    begin                : Fri Jun 30 2000
    copyright            : (C) 2000 by Hans Dijkema
    email                : kmailcvt@hum.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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kmessagebox.h>
#include <klocale.h>
#include <dcopclient.h>
#include <dcopref.h>
#include <kurl.h>
#include <kdebug.h>

#include "filters.hxx"
#include "kmailcvt.h"


//////////////////////////////////////////////////////////////////////////////////
//
// The API to the kmailcvt dialog --> Gives the import filter access to
// put information on the dialog.
//
//////////////////////////////////////////////////////////////////////////////////

bool FilterInfo::s_terminateASAP = false;

FilterInfo::FilterInfo( KImportPageDlg* dlg, TQWidget* parent , bool _removeDupMsg)
  : m_dlg( dlg ),
    m_parent( parent )
{
  removeDupMsg = _removeDupMsg;
  s_terminateASAP = false;
}

FilterInfo::~FilterInfo()
{
}

void FilterInfo::seStatusMsg( const TQString& status )
{
  m_dlg->_texStatus->setText( status );
}

void FilterInfo::setFrom( const TQString& from )
{
  m_dlg->_from->setText( from );
}

void FilterInfo::setTo( const TQString& to )
{
  m_dlg->_to->setText( to );
}

void FilterInfo::setCurrent( const TQString& current )
{
  m_dlg->_current->setText( current );
  kapp->processEvents();
}

void  FilterInfo::setCurrent( int percent )
{
  m_dlg->_done_current->setProgress( percent );
  kapp->processEvents(); // Be careful - back & finish buttons disabled, so only user event that can happen is cancel/close button
}

void  FilterInfo::setOverall( int percent )
{
  m_dlg->_done_overall->setProgress( percent );
}

void FilterInfo::addLog( const TQString& log )
{
  m_dlg->_log->insertItem( log );
  m_dlg->_log->setCurrentItem( m_dlg->_log->count() - 1 );
  m_dlg->_log->centerCurrentItem();
  kapp->processEvents();
}

void FilterInfo::clear()
{
  m_dlg->_log->clear();
  setCurrent();
  setOverall();
  setCurrent( TQString() );
  setFrom( TQString() );
  setTo( TQString() );
}

void FilterInfo::alert( const TQString& message )
{
  KMessageBox::information( m_parent, message );
}

void FilterInfo::terminateASAP()
{
  s_terminateASAP = true;
}

bool FilterInfo::shouldTerminate()
{
  return s_terminateASAP;
}

//////////////////////////////////////////////////////////////////////////////////
//
// The generic filter class
//
//////////////////////////////////////////////////////////////////////////////////


Filter::Filter( const TQString& name, const TQString& author,
                const TQString& info )
  : m_name( name ),
    m_author( author ),
    m_info( info )
{
    //public
    count_duplicates = 0;
}

bool Filter::addMessage( FilterInfo* info, const TQString& folderName,
                         const TQString& msgPath, const TQString &  msgStatusFlags)
{
  KURL msgURL;
  msgURL.setPath( msgPath );

  if ( !kapp->dcopClient()->isApplicationRegistered( "kmail" ) )
    KApplication::startServiceByDesktopName( "kmail", TQString() ); // Will wait until kmail is started

  DCOPReply reply = DCOPRef( "kmail", "KMailIface" ).call( "dcopAddMessage", folderName, msgURL, msgStatusFlags );

  if ( !reply.isValid() )
  {
    info->alert( i18n( "<b>Fatal:</b> Unable to start KMail for DCOP communication. "
                       "Make sure <i>kmail</i> is installed." ) );
    return false;
  }

  switch ( int( reply ) )
  {
    case -1:
      info->alert( i18n( "Cannot make folder %1 in KMail" ).arg( folderName ) );
      return false;
    case -2:
      info->alert( i18n( "Cannot add message to folder %1 in KMail" ).arg( folderName ) );
      return false;
    case -4:
      count_duplicates++;
      return false;
    case 0:
      info->alert( i18n( "Error while adding message to folder %1 in KMail" ).arg( folderName ) );
      return false;
  }
  return true;
}

bool Filter::addMessage_fastImport( FilterInfo* info, const TQString& folderName,
                         	        const TQString& msgPath, const TQString& msgStatusFlags )
{
  KURL msgURL;
  msgURL.setPath( msgPath );

  if ( !kapp->dcopClient()->isApplicationRegistered( "kmail" ) )
    KApplication::startServiceByDesktopName( "kmail", TQString() ); // Will wait until kmail is started

  DCOPReply reply = DCOPRef( "kmail", "KMailIface" ).call( "dcopAddMessage_fastImport", folderName, msgURL, msgStatusFlags );
  if ( !reply.isValid() )
  {
    info->alert( i18n( "<b>Fatal:</b> Unable to start KMail for DCOP communication. "
                       "Make sure <i>kmail</i> is installed." ) );
    return false;
  }

  switch ( int( reply ) )
  {
    case -1:
      info->alert( i18n( "Cannot make folder %1 in KMail" ).arg( folderName ) );
      return false;
    case -2:
      info->alert( i18n( "Cannot add message to folder %1 in KMail" ).arg( folderName ) );
      return false;
    case 0:
      info->alert( i18n( "Error while adding message to folder %1 in KMail" ).arg( folderName ) );
      return false;
  }
  return true;
}

void Filter::showKMailImportArchiveDialog( FilterInfo* info )
{
  if ( !kapp->dcopClient()->isApplicationRegistered( "kmail" ) )
    KApplication::startServiceByDesktopName( "kmail", TQString() ); // Will wait until kmail is started

  DCOPReply reply = DCOPRef( "kmail", "KMailIface" ).call( "showImportArchiveDialog" );
  if ( !reply.isValid() )
  {
    info->alert( i18n( "<b>Fatal:</b> Unable to start KMail for DCOP communication. "
                       "Make sure <i>kmail</i> is installed." ) );
  }
}

bool Filter::needsSecondPage()
{
  return true;
}

// vim: ts=2 sw=2 et