-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwxMsDeleteMsgs.cpp
388 lines (373 loc) · 13.3 KB
/
wxMsDeleteMsgs.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*-----------------------------------------------------------------
* Name: wxMsDeleteMsgs.cpp
* Purpose:
* Author: A. Wiegert
*
* Copyright:
* Licence: wxWidgets license
*---------------------------------------------------------------- */
/*----------------------------------------------------------------
* Standard wxWidgets headers
*---------------------------------------------------------------- */
// Note __VISUALC__ is defined by wxWidgets, not by MSVC IDE
// and thus won't be defined until some wxWidgets headers are included
#if defined( _MSC_VER )
# if defined ( _DEBUG )
// this statement NEEDS to go BEFORE all headers
# define _CRTDBG_MAP_ALLOC
# endif
#endif
#include "wxMsPreProcDefsh.h" // needs to be first
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
/* For all others, include the necessary headers
* (this file is usually all you need because it
* includes almost all "standard" wxWidgets headers) */
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
// ------------------------------------------------------------------
#include "wxMsFrameh.h"
// ------------------------------------------------------------------
// Note __VISUALC__ is defined by wxWidgets, not by MSVC IDE
// and thus won't be defined until some wxWidgets headers are included
#if defined( _MSC_VER )
// only good for MSVC
// this block needs to go AFTER all headers
#include <crtdbg.h>
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif
#endif
// ------------------------------------------------------------------
/**
* Toggle the 'delete' check box
*/
void MyFrame::OnMarkToDeleteSelected(wxCommandEvent& event)
{
// Get selected row
wxString wsValue;
wxArrayInt waiRows = m_gridMail->GetSelectedRows();
if ( (waiRows.GetCount() > 1) || ( waiRows.GetCount() == 0 ) )
return;
unsigned long lIndex = waiRows[0];
wsValue = m_gridMail->GetCellValue( lIndex, 0 );
if( wsValue.IsSameAs( _T("1") ) )
{
m_gridMail->SetCellValue( lIndex, 0, _T("0") );
}
else
{
m_gridMail->SetCellValue( lIndex, 0, _T("1") );
}
}
// ------------------------------------------------------------------
void MyFrame::OnMarkDeleteAll(wxCommandEvent& WXUNUSED(event) )
{
m_gridMail->BeginBatch();
int i = 0;
for ( i = 0; i < m_gridMail->GetNumberRows(); i++ )
{
m_gridMail->SetCellValue( i, 0, _T("1") );
}
m_gridMail->EndBatch();
}
// ------------------------------------------------------------------
void MyFrame::OnClearAllDelete( wxCommandEvent& event)
{
m_gridMail->BeginBatch();
int i = 0;
for ( i = 0; i < m_gridMail->GetNumberRows(); i++ )
{
m_gridMail->SetCellValue( i, 0, _T("0") );
}
m_gridMail->EndBatch();
}
// ------------------------------------------------------------------
/**
* Delete all messages marked for deletion.
* The best way to avoid issues because removing messages may change
* the sequence numbers, is to start at the 'bottom; in the list and
* work our way up.
*/
void MyFrame::DeleteMarkedMessages()
{
int iDeletes = 0; // total number of messages deleted
size_t iRow = 0;
wxString wsValue;
wxString wsT;
wxString wsAccount; // curent account name
wxArrayInt waiRows2Delete; // mail grid rows to be deleted when done
wxArrayString wasUidls2Delete;
wxArrayString wasAccounts;
wxString wsUidl;
bool bFound = false;
// Clear all of the command structure
ClearPop3CommandData();
MyAccountList::iterator iter;
for ( iter = wxGetApp().m_AccountList.begin();
iter != wxGetApp().m_AccountList.end(); ++iter )
{
// collect all accounts with messages marked for deletion
MyMsAccountEl *pCurrent = *iter;
if ( pCurrent->m_bEnabled )
{
wsAccount = wxEmptyString;
// MUST start at bottom of grid, because, as messages removed,
// the sequenc numbers within the grid would change
for ( iRow = m_gridMail->GetNumberRows(); iRow > 0; iRow-- )
{
// get account name when we find an enabled account
wsAccount = m_gridMail->GetCellValue( iRow - 1, 9 );
if ( pCurrent->m_wsAcctName.IsSameAs( wsAccount ) )
{
wsValue = m_gridMail->GetCellValue( iRow - 1, 0 );
if( wsValue.IsSameAs( _T("1") ) )
{
wasAccounts.Add( wsAccount );
break; // once we found one message for this account
// we can move on to next account
}
}
}
}
}
if ( wasAccounts.size() == 0 )
return; // nothing to do
// otherwise handle all accounts with messages marked for deletion
for ( size_t i = 0; i < wasAccounts.size(); i++)
{
for ( iter = wxGetApp().m_AccountList.begin();
iter != wxGetApp().m_AccountList.end(); ++iter )
{
// collect all accounts with messages marked for deletion
MyMsAccountEl *pCurrent = *iter;
if ( pCurrent->m_bEnabled )
{
if ( wasAccounts[i].IsSameAs( pCurrent->m_wsAcctName ) )
{
for ( iRow = m_gridMail->GetNumberRows(); iRow > 0; iRow-- )
{
// get account name when we find an enabled account
wsAccount = m_gridMail->GetCellValue( iRow - 1, 9 );
if ( pCurrent->m_wsAcctName.IsSameAs( wsAccount ) )
{
// scan the message list
wsValue = m_gridMail->GetCellValue( iRow - 1, 0 );
if( wsValue.IsSameAs( _T("1") ) )
{
size_t j;
std::list<MyPop3MsgElement>::iterator it;
// find the corrsponding UIDL
for ( j = 0, it = m_Pop3MsgList.begin(); it != m_Pop3MsgList.end(); ++it, j++ )
{
#if defined( _DEBUG )
wxString wsTT = it->m_wsUidl;
#endif
if( j == iRow - 1 )
{
wasUidls2Delete.Add( it->m_wsUidl );
wsUidl = it->m_wsUidl;
waiRows2Delete.Add( iRow - 1 );
iDeletes++;
//continue; // search rest of the grid
}
}
}
}
}
// now setup the DELE commands for the entries in wasUidls2Delete
// we cannnot use FrameDeleteMessage() because we want to delete
// all marked messages in one POP3 session
MyAccountList::iterator iter;
int j;
for ( j = 0, iter = wxGetApp().m_AccountList.begin();
iter != wxGetApp().m_AccountList.end(); ++iter, j++)
{
MyMsAccountEl *pCurrent = *iter;
if ( pCurrent->m_wsAcctName.IsSameAs( wasAccounts[i] ) )
{
wxCriticalSectionLocker lock(m_CS_Pop3MsgList);
m_Pop3CommandData.sm_wsAccountName = pCurrent->m_wsAcctName;
m_Pop3CommandData.sm_wsPop3ServerUrl = pCurrent->m_wsPopServerUrl;
m_Pop3CommandData.sm_wsUserName = pCurrent->m_wsUserName;
m_Pop3CommandData.sm_wsUserPassword = pCurrent->m_wsPassword;
m_Pop3CommandData.sm_iAccountIndex = j;
bFound = true;
break; // no need to check further
}
else
continue; // try next account
}
if ( !bFound ) // can't proceed
{
wxMessageBox( _("Problems processing deletes for account:\n") + wsAccount,
_("Error") );
return;
}
for ( size_t i = 0; i < wasUidls2Delete.GetCount(); i++ )
{
wxCriticalSectionLocker lock(m_CS_Pop3MsgList);
// at this point, the UIDL string still carries the proper prefix
wsT.Printf( _T("DELE %s"), wasUidls2Delete[i] );
m_Pop3CommandData.sm_wasCommands.Add( wsT );
}
bool bLocalRun = true;
{
wxCriticalSectionLocker lock(m_CS_Pop3MsgList);
m_bRunning = true;
}
wxASSERT( m_Pop3CommandData.sm_wasCommands.GetCount() );
wxASSERT_MSG( !m_Pop3CommandData.sm_wsAccountName.IsEmpty(), m_Pop3CommandData.sm_wsAccountName );
wxASSERT_MSG( !m_Pop3CommandData.sm_wsPop3ServerUrl.IsEmpty(), m_Pop3CommandData.sm_wsPop3ServerUrl );
wxASSERT_MSG( !m_Pop3CommandData.sm_wsUserName.IsEmpty(), m_Pop3CommandData.sm_wsUserName );
wxASSERT_MSG( !m_Pop3CommandData.sm_wsUserPassword.IsEmpty(), m_Pop3CommandData.sm_wsUserPassword );
RunLibcurlThread();
// wait until the messages have been deleted
while ( bLocalRun )
{
{
wxCriticalSectionLocker lock(m_CS_Pop3MsgList);
bLocalRun = m_bRunning;
}
{
#if defined( _MSC_VER )
::wxYield();
#endif
::wxMilliSleep( 10 );
#if defined( _DEBUG )
wxLogMessage(_T("3 - %s - Delete - Line %ld"), __FILE__, __LINE__ );
#endif
}
}
}
}
}
}
// independent of verbosity
wxLogMessage( _("%d message(s) deleted"), iDeletes );
}
// ------------------------------------------------------------------
/**
* Called from the mail grid context menu to delete a single message,
* as well as when deleting all messages marked for deletion.
*/
void MyFrame::FrameDeleteMessage( wxString a_wsAccount, long a_lIndex, wxString a_wsUidl )
{
// clear the command structure
ClearPop3CommandData();
long j = 0;
bool bFound = false;
MyAccountList::iterator iter;
for ( j = 0, iter = wxGetApp().m_AccountList.begin();
iter != wxGetApp().m_AccountList.end(); ++iter, j++ )
{
MyMsAccountEl *pCurrent = *iter;
if ( pCurrent->m_wsAcctName.IsSameAs( a_wsAccount ) )
{
wxCriticalSectionLocker lock(m_CS_Pop3MsgList);
m_Pop3CommandData.sm_wsPop3ServerUrl = pCurrent->m_wsPopServerUrl;
m_Pop3CommandData.sm_wsUserName = pCurrent->m_wsUserName;
m_Pop3CommandData.sm_wsUserPassword = pCurrent->m_wsPassword;
m_Pop3CommandData.sm_wsAccountName = a_wsAccount;
bFound = true;
break;
}
}
if ( !bFound ) // can't proceed
return;
// delete the highlighted message
// pass in command + UIDL; the called routine will
// sort out the correspondence between message number & UIDL
wxString wsT = a_wsUidl;
wsT = wsT.AfterFirst( ' ' ); // strip the current sequence number
{
wxCriticalSectionLocker lock(m_CS_Pop3MsgList);
m_Pop3CommandData.sm_wasCommands.Add( _T("DELE ") + wsT );
}
wxASSERT( m_Pop3CommandData.sm_wasCommands.GetCount() );
wxASSERT_MSG( !m_Pop3CommandData.sm_wsAccountName.IsEmpty(), m_Pop3CommandData.sm_wsAccountName );
wxASSERT_MSG( !m_Pop3CommandData.sm_wsPop3ServerUrl.IsEmpty(), m_Pop3CommandData.sm_wsPop3ServerUrl );
wxASSERT_MSG( !m_Pop3CommandData.sm_wsUserName.IsEmpty(), m_Pop3CommandData.sm_wsUserName );
wxASSERT_MSG( !m_Pop3CommandData.sm_wsUserPassword.IsEmpty(), m_Pop3CommandData.sm_wsUserPassword );
RunLibcurlThread(); // handle the mail delete via threads
bool bLocalRun = true;
// make sure we don't start a new check until
// the current one is finished
while ( bLocalRun )
{
{
wxCriticalSectionLocker lock(m_CS_Pop3MsgList);
bLocalRun = m_bRunning;
}
::wxMilliSleep( 10 );
::wxYield(); // wait for the job to finish
}
// need to remove the message from the mail list
// before we refresh the display
std::list<MyPop3MsgElement>::iterator it;
for (j = 1, it = m_Pop3MsgList.begin();
it != m_Pop3MsgList.end(); ++it, j++ )
{
if( j == a_lIndex )
{
m_Pop3MsgList.erase( it );
if ( g_iniPrefs.data[IE_LOG_VERBOSITY].dataCurrent.lVal > 4 )
{
wxLogMessage(_T("Deleted Message: %ld - Line 183 GetCount:%d"),
j, m_Pop3MsgList.size() );
}
break;
}
}
}
// ------------------------------------------------------------------
/**
* Called when user selects a message and presses 'DEL' or wants to
* delete a message via the main menu: Email -> Delete message.
*/
void MyFrame::OnEmailDelMessage( wxCommandEvent& WXUNUSED(event ) )
{
wxString wsT;
long j = 0;
// Get selected row
wxArrayInt waiRows = m_gridMail->GetSelectedRows();
if ( (waiRows.GetCount() > 1) || ( waiRows.GetCount() == 0 ) )
return;
long lIndex = waiRows[0];
wxString wsAccount = m_gridMail->GetCellValue( lIndex, 9 );
wxString wsSubject = m_gridMail->GetCellValue( lIndex, 6 );
if( wsAccount.IsEmpty() )
{
wsT.Printf( _("The account name in row %d is missing!"), lIndex );
wxMessageBox( wsT, "Notice", wxOK );
return;
}
// when POP3 server has UIDL command, we nned to get the UIDL related to
// the chosen message and pass it to the libcurl handler so that
// it can be all processed in one POP3 transaction.
bool bFound = false;
std::list<MyPop3MsgElement>::iterator it;
// Display the message dialog
for (j = 1, it = m_Pop3MsgList.begin(); it != m_Pop3MsgList.end(); ++it, j++ )
{
if( j == lIndex + 1)
{
if ( it->m_wsAcctName.IsSameAs( wsAccount ) )
{
FrameDeleteMessage( wsAccount, lIndex + 1, it->m_wsUidl );
bFound = true;
return;
}
}
}
if ( !bFound )
wxLogMessage( _("Could not delete message in row %d"), lIndex + 1 );
}
// ------------------------------- eof ------------------------------