forked from WinMerge/winmerge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileFiltersDlg.cpp
611 lines (532 loc) · 16.5 KB
/
FileFiltersDlg.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
// SPDX-License-Identifier: GPL-2.0-or-later
/**
* @file FileFiltersDlg.cpp
*
* @brief Implementation of FileFilters -dialog
*/
#include "stdafx.h"
#include "FileFiltersDlg.h"
#include <vector>
#include "UnicodeString.h"
#include "Merge.h"
#include "OptionsMgr.h"
#include "OptionsDef.h"
#include "FileFilterMgr.h"
#include "FileFilterHelper.h"
#include "paths.h"
#include "SharedFilterDlg.h"
#include "TestFilterDlg.h"
#include "FileOrFolderSelect.h"
#include "UniFile.h"
#include "Constants.h"
using std::vector;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/** @brief Template file used when creating new filefilter. */
static const tchar_t FILE_FILTER_TEMPLATE[] = _T("FileFilter.tmpl");
/////////////////////////////////////////////////////////////////////////////
// CFiltersDlg dialog
IMPLEMENT_DYNCREATE(FileFiltersDlg, CTrPropertyPage)
/**
* @brief Constructor.
*/
FileFiltersDlg::FileFiltersDlg() : CTrPropertyPage(FileFiltersDlg::IDD)
{
m_strCaption = theApp.LoadDialogCaption(m_lpszTemplateName).c_str();
m_psp.pszTitle = m_strCaption;
m_psp.dwFlags |= PSP_USETITLE;
m_psp.hIcon = AfxGetApp()->LoadIcon(IDI_FILEFILTER);
m_psp.dwFlags |= PSP_USEHICON;
}
void FileFiltersDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(FileFiltersDlg)
DDX_Control(pDX, IDC_FILTERFILE_LIST, m_listFilters);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(FileFiltersDlg, CTrPropertyPage)
//{{AFX_MSG_MAP(FileFiltersDlg)
ON_BN_CLICKED(IDC_FILTERFILE_EDITBTN, OnFiltersEditbtn)
ON_NOTIFY(NM_DBLCLK, IDC_FILTERFILE_LIST, OnDblclkFiltersList)
ON_WM_MOUSEMOVE()
ON_BN_CLICKED(IDC_FILTERFILE_TEST_BTN, OnBnClickedFilterfileTestButton)
ON_BN_CLICKED(IDC_FILTERFILE_NEWBTN, OnBnClickedFilterfileNewbutton)
ON_BN_CLICKED(IDC_FILTERFILE_DELETEBTN, OnBnClickedFilterfileDelete)
ON_COMMAND(ID_HELP, OnHelp)
//}}AFX_MSG_MAP
ON_NOTIFY(LVN_ITEMCHANGED, IDC_FILTERFILE_LIST, OnLvnItemchangedFilterfileList)
ON_NOTIFY(LVN_GETINFOTIP, IDC_FILTERFILE_LIST, OnInfoTip)
ON_BN_CLICKED(IDC_FILTERFILE_INSTALL, OnBnClickedFilterfileInstall)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFiltersDlg message handlers
/**
* @brief Set array of filters.
* @param [in] fileFilters Array of filters to show in the dialog.
* @note Call this before actually showing the dialog.
*/
void FileFiltersDlg::SetFilterArray(const vector<FileFilterInfo>& fileFilters)
{
m_Filters = fileFilters;
}
/**
* @brief Returns path (cont. filename) of selected filter
* @return Full path to selected filter file.
*/
String FileFiltersDlg::GetSelected()
{
return m_sFileFilterPath;
}
/**
* @brief Set path of selected filter.
* @param [in] Path for selected filter.
* @note Call this before actually showing the dialog.
*/
void FileFiltersDlg::SetSelected(const String & selected)
{
m_sFileFilterPath = selected;
}
/**
* @brief Initialise listcontrol containing filters.
*/
void FileFiltersDlg::InitList()
{
// Show selection across entire row.
// Also enable infotips.
m_listFilters.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);
const int lpx = CClientDC(this).GetDeviceCaps(LOGPIXELSX);
auto pointToPixel = [lpx](int point) { return MulDiv(point, lpx, 72); };
String title = _("Name");
m_listFilters.InsertColumn(0, title.c_str(), LVCFMT_LEFT, pointToPixel(112));
title = _("Description");
m_listFilters.InsertColumn(1, title.c_str(), LVCFMT_LEFT, pointToPixel(262));
title = _("Location");
m_listFilters.InsertColumn(2, title.c_str(), LVCFMT_LEFT, pointToPixel(262));
title = _("<None>");
m_listFilters.InsertItem(1, title.c_str());
m_listFilters.SetItemText(0, 1, title.c_str());
m_listFilters.SetItemText(0, 2, title.c_str());
const int count = (int) m_Filters.size();
for (int i = 0; i < count; i++)
{
AddToGrid(i);
}
}
/**
* @brief Select filter by index in the listview.
* @param [in] index Index of filter to select.
*/
void FileFiltersDlg::SelectFilterByIndex(int index)
{
m_listFilters.SetItemState(index, LVIS_SELECTED, LVIS_SELECTED);
bool bPartialOk = false;
m_listFilters.EnsureVisible(index, bPartialOk);
}
/**
* @brief Select filter by file path in the listview.
* @param [in] path file path
*/
void FileFiltersDlg::SelectFilterByFilePath(const String& path)
{
for (size_t i = 0; i < m_Filters.size(); ++i)
{
if (m_Filters[i].fullpath == path)
{
SelectFilterByIndex(static_cast<int>(i + 1));
break;
}
}
}
/**
* @brief Called before dialog is shown.
* @return Always TRUE.
*/
BOOL FileFiltersDlg::OnInitDialog()
{
CTrPropertyPage::OnInitDialog();
InitList();
if (m_sFileFilterPath.empty())
{
SelectFilterByIndex(0);
return TRUE;
}
int count = m_listFilters.GetItemCount();
for (int i = 0; i < count; i++)
{
String desc = m_listFilters.GetItemText(i, 2);
if (strutils::compare_nocase(desc, m_sFileFilterPath) == 0)
{
SelectFilterByIndex(i);
}
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
/**
* @brief Add filter from filter-list index to dialog.
* @param [in] filterIndex Index of filter to add.
*/
void FileFiltersDlg::AddToGrid(int filterIndex)
{
const FileFilterInfo & filterinfo = m_Filters.at(filterIndex);
const int item = filterIndex + 1;
m_listFilters.InsertItem(item, filterinfo.name.c_str());
m_listFilters.SetItemText(item, 1, filterinfo.description.c_str());
m_listFilters.SetItemText(item, 2, filterinfo.fullpath.c_str());
}
/**
* @brief Called when dialog is closed with "OK" button.
*/
void FileFiltersDlg::OnOK()
{
int sel = m_listFilters.GetNextItem(-1, LVNI_SELECTED);
m_sFileFilterPath = m_listFilters.GetItemText(sel, 2);
AfxGetApp()->WriteProfileInt(_T("Settings"), _T("FilterStartPage"), GetParentSheet()->GetActiveIndex());
CDialog::OnOK();
}
/**
* @brief Open selected filter for editing.
*
* This opens selected file filter file for user to edit. Other WinMerge UI is
* not (anymore) blocked during editing. We let user continue working with
* WinMerge while editing filter(s). Before opening this dialog and before
* doing directory compare we re-load changed filter files from disk. So we
* always compare with latest saved filters.
* @sa CMainFrame::OnToolsFilters()
* @sa CDirDoc::Rescan()
* @sa FileFilterHelper::ReloadUpdatedFilters()
*/
void FileFiltersDlg::OnFiltersEditbtn()
{
int sel =- 1;
sel = m_listFilters.GetNextItem(sel, LVNI_SELECTED);
// Can't edit first "None"
if (sel > 0)
{
String path = m_listFilters.GetItemText(sel, 2);
EditFileFilter(path);
}
}
/**
* @brief Edit file filter in external editor.
* @param [in] path Full path to file filter to edit.
*/
void FileFiltersDlg::EditFileFilter(const String& path)
{
CMergeApp::OpenFileToExternalEditor(path);
}
/**
* @brief Edit selected filter when its double-clicked.
* @param [in] pNMHDR List control item data.
* @param [out] pResult Result of the action is returned in here.
*/
void FileFiltersDlg::OnDblclkFiltersList(NMHDR* pNMHDR, LRESULT* pResult)
{
UNREFERENCED_PARAMETER(pNMHDR);
OnFiltersEditbtn();
*pResult = 0;
}
/**
* @brief Is item in list the <None> item?
* @param [in] item Item to test.
* @return true if item is <None> item.
*/
bool FileFiltersDlg::IsFilterItemNone(int item) const
{
String txtNone = _("<None>");
String txt = m_listFilters.GetItemText(item, 0);
return (strutils::compare_nocase(txt, txtNone) == 0);
}
/**
* @brief Called when item state is changed.
*
* Disable Edit-button when "None" filter is selected.
* @param [in] pNMHDR Listview item data.
* @param [out] pResult Result of the action is returned in here.
*/
void FileFiltersDlg::OnLvnItemchangedFilterfileList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
// If item got selected
if (pNMLV->uNewState & LVIS_SELECTED)
{
String txtNone = _("<None>");
String txt = m_listFilters.GetItemText(pNMLV->iItem, 0);
bool isNone = strutils::compare_nocase(txt, txtNone) == 0;
EnableDlgItem(IDC_FILTERFILE_TEST_BTN, !isNone);
EnableDlgItem(IDC_FILTERFILE_EDITBTN, !isNone);
EnableDlgItem(IDC_FILTERFILE_DELETEBTN, !isNone);
}
*pResult = 0;
}
/**
* @brief Called before infotip is shown to get infotip text.
* @param [in] pNMHDR Listview item data.
* @param [out] pResult Result of the action is returned in here.
*/
void FileFiltersDlg::OnInfoTip(NMHDR * pNMHDR, LRESULT * pResult)
{
LVHITTESTINFO lvhti = {0};
NMLVGETINFOTIP * pInfoTip = reinterpret_cast<NMLVGETINFOTIP*>(pNMHDR);
ASSERT(pInfoTip != nullptr);
// Get subitem under mouse cursor
lvhti.pt = m_ptLastMousePos;
m_listFilters.SubItemHitTest(&lvhti);
if (lvhti.iSubItem > 1)
{
// Check that we are over icon or label
if ((lvhti.flags & LVHT_ONITEMICON) || (lvhti.flags & LVHT_ONITEMLABEL))
{
// Set item text to tooltip
String strText = m_listFilters.GetItemText(lvhti.iItem, lvhti.iSubItem);
_tcscpy_s(pInfoTip->pszText, pInfoTip->cchTextMax, strText.c_str());
}
}
}
/**
* @brief Track mouse position for showing tooltips.
* @param [in] nFlags Mouse movement flags.
* @param [in] point Current mouse position.
*/
void FileFiltersDlg::OnMouseMove(UINT nFlags, CPoint point)
{
m_ptLastMousePos = point;
CDialog::OnMouseMove(nFlags, point);
}
/**
* @brief Called when user presses "Test" button.
*
* Asks filename for new filter from user (using standard
* file picker dialog) and copies template file to that
* name. Opens new filterfile for editing.
* @todo (At least) Warn if user puts filter to outside
* filter directories?
*/
void FileFiltersDlg::OnBnClickedFilterfileTestButton()
{
UpdateData(TRUE);
int sel = m_listFilters.GetNextItem(-1, LVNI_SELECTED);
if (sel == -1)
return;
if (IsFilterItemNone(sel))
return;
m_sFileFilterPath = m_listFilters.GetItemText(sel, 2);
// Ensure filter is up-to-date (user probably just edited it)
auto* pGlobalFileFilter = theApp.GetGlobalFileFilter();
pGlobalFileFilter->ReloadUpdatedFilters();
FileFilterMgr *pMgr = pGlobalFileFilter->GetManager();
FileFilter * pFileFilter = pMgr->GetFilterByPath(m_sFileFilterPath);
if (pFileFilter == nullptr)
return;
CTestFilterDlg dlg(this, pFileFilter, pMgr);
dlg.DoModal();
}
/**
* @brief Called when user presses "New..." button.
*
* Asks filename for new filter from user (using standard
* file picker dialog) and copies template file to that
* name. Opens new filterfile for editing.
* @todo (At least) Warn if user puts filter to outside
* filter directories?
* @todo Can global filter path be empty (I think not - Kimmo).
*/
void FileFiltersDlg::OnBnClickedFilterfileNewbutton()
{
auto* pGlobalFileFilter = theApp.GetGlobalFileFilter();
String globalPath = pGlobalFileFilter->GetGlobalFilterPathWithCreate();
String userPath = pGlobalFileFilter->GetUserFilterPathWithCreate();
if (globalPath.empty() && userPath.empty())
{
AfxMessageBox(
_("User's filter file folder is not defined!\n\nPlease select filter folder in Options/System.").c_str(), MB_ICONSTOP);
return;
}
// Format path to template file
String templatePath = paths::ConcatPath(globalPath, FILE_FILTER_TEMPLATE);
if (paths::DoesPathExist(templatePath) != paths::IS_EXISTING_FILE)
{
String msg = strutils::format_string2(
_("Cannot find file filter template file!\n\nPlease copy file %1 to WinMerge/Filters folder:\n%2."),
FILE_FILTER_TEMPLATE, templatePath);
AfxMessageBox(msg.c_str(), MB_ICONERROR);
return;
}
String path = globalPath.empty() ? userPath : globalPath;
if (!globalPath.empty() && !userPath.empty())
{
CSharedFilterDlg dlg(
GetOptionsMgr()->GetBool(OPT_FILEFILTER_SHARED) ?
CSharedFilterDlg::SHARED : CSharedFilterDlg::PRIVATE);
if (dlg.DoModal() != IDOK)
return;
GetOptionsMgr()->SaveOption(OPT_FILEFILTER_SHARED, (dlg.GetSelectedFilterType() == CSharedFilterDlg::SHARED));
path = dlg.GetSelectedFilterType() == CSharedFilterDlg::SHARED ? globalPath : userPath;
}
if (path.length())
path = paths::AddTrailingSlash(path);
String s;
if (SelectFile(GetSafeHwnd(), s, false, path.c_str(), _("Select filename for new filter"),
_("File Filters (*.flt)|*.flt|All Files (*.*)|*.*||")))
{
// Fix file extension
tchar_t file[_MAX_FNAME] = {0};
tchar_t ext[_MAX_EXT] = {0};
tchar_t dir[_MAX_DIR] = {0};
tchar_t drive[_MAX_DRIVE] = {0};
_tsplitpath_s(s.c_str(), drive, _MAX_DRIVE, dir, _MAX_DIR, file, _MAX_FNAME, ext, _MAX_EXT);
if (ext[0] == '\0')
{
s += FileFilterExt;
}
else if (tc::tcsicmp(ext, FileFilterExt) != 0)
{
s = drive;
s += dir;
s += file;
s += FileFilterExt;
}
// Open-dialog asks about overwriting, so we can overwrite filter file
// user has already allowed it.
UniMemFile fileIn;
UniStdioFile fileOut;
if (!fileIn.OpenReadOnly(templatePath) || !fileOut.OpenCreate(s))
{
String msg = strutils::format_string1(
_( "Cannot copy filter template file to filter folder:\n%1\n\nPlease make sure the folder exists and is writable."),
templatePath);
AfxMessageBox(msg.c_str(), MB_ICONERROR);
return;
}
String lines;
fileIn.ReadStringAll(lines);
strutils::replace(lines, _T("${name}"), file);
fileOut.WriteString(lines);
fileIn.Close();
fileOut.Close();
EditFileFilter(s);
FileFilterMgr *pMgr = pGlobalFileFilter->GetManager();
int retval = pMgr->AddFilter(s);
if (retval == FILTER_OK)
{
// Remove all from filterslist and re-add so we can update UI
String selected;
pGlobalFileFilter->LoadAllFileFilters();
m_Filters = pGlobalFileFilter->GetFileFilters(selected);
UpdateFiltersList();
SelectFilterByFilePath(s);
}
}
}
/**
* @brief Delete selected filter.
*/
void FileFiltersDlg::OnBnClickedFilterfileDelete()
{
String path;
int sel =- 1;
sel = m_listFilters.GetNextItem(sel, LVNI_SELECTED);
// Can't delete first "None"
if (sel > 0)
{
path = m_listFilters.GetItemText(sel, 2);
String sConfirm = strutils::format_string1(_("Are you sure you want to delete\n\n%1 ?"), path);
int res = AfxMessageBox(sConfirm.c_str(), MB_ICONWARNING | MB_YESNO);
if (res == IDYES)
{
if (DeleteFile(path.c_str()))
{
auto* pGlobalFileFilter = theApp.GetGlobalFileFilter();
FileFilterMgr *pMgr = pGlobalFileFilter->GetManager();
pMgr->RemoveFilter(path);
// Remove all from filterslist and re-add so we can update UI
String selected;
m_Filters = pGlobalFileFilter->GetFileFilters(selected);
UpdateFiltersList();
}
else
{
String msg = strutils::format_string1(
_("Failed to delete the filter file:\n%1\n\nMaybe the file is read-only?"),
path);
AfxMessageBox(msg.c_str(), MB_ICONSTOP);
}
}
}
}
/**
* @brief Update filters to list.
*/
void FileFiltersDlg::UpdateFiltersList()
{
int count = (int) m_Filters.size();
m_listFilters.DeleteAllItems();
String title = _("<None>");
m_listFilters.InsertItem(1, title.c_str());
m_listFilters.SetItemText(0, 1, title.c_str());
m_listFilters.SetItemText(0, 2, title.c_str());
for (int i = 0; i < count; i++)
{
AddToGrid(i);
}
}
/**
* @brief Open help from mainframe when user presses F1
*/
void FileFiltersDlg::OnHelp()
{
theApp.ShowHelp(FilterHelpLocation);
}
/**
* @brief Install new filter.
* This function is called when user selects "Install" button from GUI.
* Function allows easy installation of new filters for user. For example
* when user has downloaded filter file from net. First we ask user to
* select filter to install. Then we copy selected filter to private
* filters folder.
*/
void FileFiltersDlg::OnBnClickedFilterfileInstall()
{
auto* pGlobalFileFilter = theApp.GetGlobalFileFilter();
String s;
String path;
if (SelectFile(GetSafeHwnd(), s, true, path.c_str(),_("Locate filter file to install"),
_("File Filters (*.flt)|*.flt|All Files (*.*)|*.*||")))
{
String userPath = pGlobalFileFilter->GetUserFilterPathWithCreate();
userPath = paths::ConcatPath(userPath, paths::FindFileName(s));
if (!CopyFile(s.c_str(), userPath.c_str(), TRUE))
{
// If file already exists, ask from user
// If user wants to, overwrite existing filter
if (paths::DoesPathExist(userPath) == paths::IS_EXISTING_FILE)
{
int res = LangMessageBox(IDS_FILEFILTER_OVERWRITE, MB_YESNO |
MB_ICONWARNING);
if (res == IDYES)
{
if (!CopyFile(s.c_str(), userPath.c_str(), FALSE))
{
LangMessageBox(IDS_FILEFILTER_INSTALLFAIL, MB_ICONSTOP);
}
}
}
else
{
LangMessageBox(IDS_FILEFILTER_INSTALLFAIL, MB_ICONSTOP);
}
}
else
{
FileFilterMgr *pMgr = pGlobalFileFilter->GetManager();
pMgr->AddFilter(userPath);
// Remove all from filterslist and re-add so we can update UI
String selected;
m_Filters = pGlobalFileFilter->GetFileFilters(selected);
UpdateFiltersList();
SelectFilterByFilePath(userPath);
}
}
}