File: DiffFoldersFrame.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (406 lines) | stat: -rw-r--r-- 13,541 bytes parent folder | download | duplicates (2)
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
#include "DiffFoldersFrame.h"

#include "DiffSelectFoldersDlg.h"
#include "bitmap_loader.h"
#include "clDiffFrame.h"
#include "clFilesCollector.h"
#include "clToolBarButtonBase.h"
#include "cl_config.h"
#include "fileextmanager.h"
#include "globals.h"

#include <algorithm>
#include <atomic>
#include <imanager.h>
#include <macros.h>
#include <wx/dir.h>
#include <wx/wupdlock.h>
#include <wxStringHash.h>

static int nCallCounter = 0;
static std::atomic_bool checksumThreadStop;

DiffFoldersFrame::DiffFoldersFrame(wxWindow* parent)
    : DiffFoldersBaseDlg(parent)
{
    checksumThreadStop.store(false);
    m_toolbar->SetMiniToolBar(false);

    clBitmapList* images = new clBitmapList;
    m_toolbar->AddTool(wxID_NEW, _("New comparison"), images->Add("file_new"));
    m_toolbar->AddTool(wxID_CLOSE, _("Close"), images->Add("file_close"));
    m_toolbar->AddTool(wxID_REFRESH, _("Refresh"), images->Add("file_reload"));
    m_toolbar->AddSeparator();
    m_toolbar->AddTool(XRCID("diff-intersection"), _("Show similar files only"), images->Add("intersection"), "",
                       wxITEM_CHECK);
    m_toolbar->AddSeparator();
    m_toolbar->AddTool(XRCID("diff-up-folder"), _("Parent folder"), images->Add("up"));
    m_toolbar->AssignBitmaps(images);
    m_toolbar->Realize();

    m_toolbar->Bind(wxEVT_TOOL, &DiffFoldersFrame::OnNewCmparison, this, wxID_NEW);
    m_toolbar->Bind(wxEVT_TOOL, &DiffFoldersFrame::OnClose, this, wxID_CLOSE);
    m_toolbar->Bind(wxEVT_TOOL, &DiffFoldersFrame::OnShowSimilarFiles, this, XRCID("diff-intersection"));
    m_toolbar->Bind(wxEVT_UPDATE_UI, &DiffFoldersFrame::OnShowSimilarFilesUI, this, XRCID("diff-intersection"));
    m_toolbar->Bind(wxEVT_TOOL, &DiffFoldersFrame::OnRefresh, this, wxID_REFRESH);
    m_toolbar->Bind(wxEVT_UPDATE_UI, &DiffFoldersFrame::OnRefreshUI, this, wxID_REFRESH);
    m_toolbar->Bind(wxEVT_TOOL, &DiffFoldersFrame::OnUpFolder, this, XRCID("diff-up-folder"));
    m_toolbar->Bind(wxEVT_UPDATE_UI, &DiffFoldersFrame::OnUpFolderUI, this, XRCID("diff-up-folder"));

    ::clSetTLWindowBestSizeAndPosition(this);

    // Load persistent items
    m_showSimilarItems = clConfig::Get().Read("DiffFolders/ShowSimilarItems", false);
}

DiffFoldersFrame::~DiffFoldersFrame()
{
    clConfig::Get().Write("DiffFolders/ShowSimilarItems", m_showSimilarItems);
    StopChecksumThread();
}

void DiffFoldersFrame::OnClose(wxCommandEvent& event)
{
    wxUnusedVar(event);
    Close();
}

void DiffFoldersFrame::OnNewCmparison(wxCommandEvent& event)
{
    wxString left = clConfig::Get().Read("DiffFolders/Left", wxString());
    wxString right = clConfig::Get().Read("DiffFolders/Right", wxString());
    DiffSelectFoldersDlg dlg(this, left, right);
    if(dlg.ShowModal() == wxID_OK) {
        left = dlg.GetDirPickerLeft()->GetPath();
        right = dlg.GetDirPickerRight()->GetPath();
        clConfig::Get().Write("DiffFolders/Left", left);
        clConfig::Get().Write("DiffFolders/Right", right);
        m_depth = 0;
        CallAfter(&DiffFoldersFrame::BuildTrees, left, right);
    }
}

#define CLOSE_FP(fp)      \
    {                     \
        if(fp) {          \
            fclose(fp);   \
            fp = nullptr; \
        }                 \
    }

static bool CompareFilesCheckSum(const wxString& fn1, const wxString& fn2)
{
    // The sizes are the same
    unsigned char checksum1 = 0;
    unsigned char checksum2 = 0;

    FILE* fp1 = fopen(fn1.mb_str(), "rb");
    FILE* fp2 = fopen(fn2.mb_str(), "rb");
    if(!fp1 || !fp2) {
        CLOSE_FP(fp1);
        CLOSE_FP(fp2);
        return false;
    }

    while(!feof(fp1) && !ferror(fp1) && !feof(fp2) && !ferror(fp2)) {
        checksum1 ^= fgetc(fp1);
        checksum2 ^= fgetc(fp2);
        if(checksum1 != checksum2) {
            break;
        }
    }
    CLOSE_FP(fp1);
    CLOSE_FP(fp2);
    return (checksum1 == checksum2);
}

static void HelperThreadCalculateChecksum(int callId, const wxArrayString& items, const wxString& left,
                                          const wxString& right, DiffFoldersFrame* sink)
{
    wxArrayString results;
    for(size_t i = 0; i < items.size(); ++i) {
        if(checksumThreadStop.load()) {
            break;
        }
        wxFileName fnLeft(left, items.Item(i));
        wxFileName fnRight(right, items.Item(i));
        if(fnLeft.IsOk() && fnLeft.FileExists() && fnRight.IsOk() && fnRight.FileExists()) {
            if(fnLeft.GetSize() != fnRight.GetSize()) {
                // If the size is different, no need to go further
                results.Add("different");
            } else {
                bool isSame = CompareFilesCheckSum(fnLeft.GetFullPath(), fnRight.GetFullPath());
                results.Add(isSame ? "same" : "different");
            }
        } else {
            results.Add("n/a"); // Dont know
        }
    }
    if(!checksumThreadStop.load()) {
        sink->CallAfter(&DiffFoldersFrame::OnChecksum, callId, results);
    }
}

void DiffFoldersFrame::BuildTrees(const wxString& left, const wxString& right)
{
    StopChecksumThread();
    wxBusyCursor bc;
    m_dvListCtrl->DeleteAllItems();
    m_entries.clear();
    m_dvListCtrl->SetSortFunction(nullptr);
    m_leftFolder = left;
    m_rightFolder = right;
    m_dvListCtrl->GetColumn(0)->SetLabel(left);
    m_dvListCtrl->GetColumn(1)->SetLabel(right);
    m_dvListCtrl->SetBitmaps(clGetManager()->GetStdIcons()->GetStandardMimeBitmapListPtr());

    // Set up the roots
    wxVector<wxVariant> cols;

    clFilesScanner::EntryData::Vec_t leftFiles;
    clFilesScanner::EntryData::Vec_t rightFiles;

    clFilesScanner scanner;
    scanner.ScanNoRecurse(left, leftFiles);
    scanner.ScanNoRecurse(right, rightFiles);

    // Get list of all files in the given folders
    DiffView viewList;

    // Add all the files
    size_t count = wxMax(leftFiles.size(), rightFiles.size());
    for(size_t i = 0; i < count; ++i) {
        if(i < leftFiles.size()) {
            const clFilesScanner::EntryData& entry = leftFiles[i];
            wxFileName fn(entry.fullpath);
            wxString fullname = fn.GetFullName();
            if(!viewList.HasFile(fullname)) {
                viewList.CreateEntry(entry, true);
            }
            viewList.GetEntry(fullname).SetLeft(entry);
        }

        if(i < rightFiles.size()) {
            const clFilesScanner::EntryData& entry = rightFiles[i];
            wxFileName fn(entry.fullpath);
            wxString fullname = fn.GetFullName();
            if(!viewList.HasFile(fullname)) {
                viewList.CreateEntry(entry, false);
            }
            viewList.GetEntry(fullname).SetRight(entry);
        }
    }

    // Sort the merged list
    m_entries = viewList.ToSortedVector();
    wxArrayString displayedItems;
    for(size_t i = 0; i < m_entries.size(); ++i) {
        cols.clear();
        const DiffViewEntry& entry = m_entries[i];

        // If the "show similar files" button is clicked, display only files that exists in both lists
        if(m_showSimilarItems && !entry.IsExistsInBoth()) {
            continue;
        }

        // This will be passed to the checksum thread
        displayedItems.Add(entry.GetFullName());

        if(entry.IsExistsInLeft()) {
            cols.push_back(::MakeBitmapIndexText(entry.GetLeft().fullpath, entry.GetImageId(true)));
        } else {
            cols.push_back(::MakeBitmapIndexText("", wxNOT_FOUND));
        }

        if(entry.IsExistsInRight()) {
            cols.push_back(::MakeBitmapIndexText(entry.GetRight().fullpath, entry.GetImageId(false)));
        } else {
            cols.push_back(::MakeBitmapIndexText("", wxNOT_FOUND));
        }
        m_dvListCtrl->AppendItem(cols, (wxUIntPtr)&entry);
    }

    m_checksumThread = new std::thread(&HelperThreadCalculateChecksum, (++nCallCounter), displayedItems, m_leftFolder,
                                       m_rightFolder, this);
}

void DiffFoldersFrame::OnItemActivated(wxDataViewEvent& event)
{
    DiffViewEntry* entry = reinterpret_cast<DiffViewEntry*>(m_dvListCtrl->GetItemData(event.GetItem()));
    if(!entry) {
        return;
    }

    if(entry->IsExistsInBoth() && (entry->GetLeft().flags & clFilesScanner::kIsFolder) &&
       (entry->GetRight().flags & clFilesScanner::kIsFolder)) {
        // Refresh the view to the current folder
        wxFileName left(m_leftFolder, "");
        wxFileName right(m_rightFolder, "");
        left.AppendDir(entry->GetFullName());
        right.AppendDir(entry->GetFullName());
        m_leftFolder = left.GetPath();
        m_rightFolder = right.GetPath();
        ++m_depth;
        CallAfter(&DiffFoldersFrame::BuildTrees, m_leftFolder, m_rightFolder);
    } else {
        DoOpenDiff(event.GetItem());
    }
}

void DiffFoldersFrame::OnItemContextMenu(wxDataViewEvent& event)
{
    wxDataViewItem item = event.GetItem();
    wxString left = m_dvListCtrl->GetItemText(item, 0);
    wxString right = m_dvListCtrl->GetItemText(item, 1);

    wxMenu menu;
    if(!right.IsEmpty()) {
        menu.Append(XRCID("diff-copy-right-to-left"), _("Copy from Right to Left"));
        menu.Bind(wxEVT_MENU, &DiffFoldersFrame::OnCopyToLeft, this, XRCID("diff-copy-right-to-left"));
    }

    if(!left.IsEmpty()) {
        menu.Append(XRCID("diff-copy-left-to-right"), _("Copy from Left to Right"));
        menu.Bind(wxEVT_MENU, &DiffFoldersFrame::OnCopyToRight, this, XRCID("diff-copy-left-to-right"));
    }
    if(menu.GetMenuItemCount()) {
        menu.AppendSeparator();
    }

    if(!right.IsEmpty() && !left.IsEmpty()) {
        menu.Append(XRCID("diff-open-diff"), _("Diff"));
        menu.Bind(wxEVT_MENU, &DiffFoldersFrame::OnMenuDiff, this, XRCID("diff-open-diff"));
    }
    if(menu.GetMenuItemCount() == 0) {
        return;
    }
    m_dvListCtrl->PopupMenu(&menu);
}

void DiffFoldersFrame::OnShowSimilarFiles(wxCommandEvent& event)
{
    event.Skip();
    m_showSimilarItems = event.IsChecked();
    BuildTrees(m_leftFolder, m_rightFolder);
}

void DiffFoldersFrame::OnShowSimilarFilesUI(wxUpdateUIEvent& event)
{
    if(m_leftFolder.IsEmpty() || m_rightFolder.IsEmpty()) {
        event.Check(false);
        event.Enable(false);
    } else {

        event.Enable(true);
        event.Check(m_showSimilarItems);
    }
}

void DiffFoldersFrame::DoOpenDiff(const wxDataViewItem& item)
{
    if(!item.IsOk()) {
        return;
    }
    wxString leftFile = m_dvListCtrl->GetItemText(item, 0);
    wxString rightFile = m_dvListCtrl->GetItemText(item, 1);
    if(leftFile.IsEmpty() || rightFile.IsEmpty()) {
        return;
    }

    wxFileName fnLeft(leftFile);
    wxFileName fnRight(rightFile);
    clDiffFrame* diffFiles = new clDiffFrame(this, fnLeft, fnRight, false);
    //diffFiles->MakeModal(true);
    diffFiles->Show();
}

void DiffFoldersFrame::OnMenuDiff(wxCommandEvent& event) { DoOpenDiff(m_dvListCtrl->GetSelection()); }

void DiffFoldersFrame::OnCopyToRight(wxCommandEvent& event)
{
    wxDataViewItem item = m_dvListCtrl->GetSelection();
    CHECK_ITEM_RET(item);
    wxString fullname = m_dvListCtrl->GetItemText(item, 0);
    wxFileName source(m_leftFolder, fullname);
    wxFileName target(m_rightFolder, fullname);
    if(::wxCopyFile(source.GetFullPath(), target.GetFullPath())) {
        m_dvListCtrl->SetItemText(item, fullname, 1);
    }
}

void DiffFoldersFrame::OnCopyToLeft(wxCommandEvent& event)
{
    wxDataViewItem item = m_dvListCtrl->GetSelection();
    CHECK_ITEM_RET(item);
    wxString fullname = m_dvListCtrl->GetItemText(item, 1);
    wxFileName source(m_rightFolder, fullname);
    wxFileName target(m_leftFolder, fullname);
    if(::wxCopyFile(source.GetFullPath(), target.GetFullPath())) {
        m_dvListCtrl->SetItemText(item, fullname, 0);
    }
}

void DiffFoldersFrame::OnChecksum(int callId, const wxArrayString& checksumArray)
{
    if(callId != nCallCounter) {
        return;
    }
    bool isDark = DrawingUtils::IsDark(m_dvListCtrl->GetColours().GetBgColour());
    wxColour modifiedColour = isDark ? wxColour("rgb(255, 128, 64)") : *wxRED;
    for(size_t i = 0; i < checksumArray.size(); ++i) {
        const wxString& answer = checksumArray.Item(i);
        if(answer == "different") {
            wxDataViewItem item = m_dvListCtrl->RowToItem(i);
            if(item.IsOk()) {
                m_dvListCtrl->SetItemTextColour(item, modifiedColour, 0);
                m_dvListCtrl->SetItemTextColour(item, modifiedColour, 1);
            }
        }
    }
}

void DiffFoldersFrame::OnRefresh(wxCommandEvent& event)
{
    wxUnusedVar(event);
    BuildTrees(m_leftFolder, m_rightFolder);
}

void DiffFoldersFrame::OnRefreshUI(wxUpdateUIEvent& event)
{
    event.Enable(!m_leftFolder.IsEmpty() && !m_rightFolder.IsEmpty());
}

void DiffFoldersFrame::StopChecksumThread()
{
    checksumThreadStop.store(false);
    if(m_checksumThread) {
        m_checksumThread->join();
    }
    checksumThreadStop.store(false);
    wxDELETE(m_checksumThread);
}

void DiffFoldersFrame::OnUpFolder(wxCommandEvent& event)
{
    if(!CanUp()) {
        return;
    }

    wxFileName fnLeft(m_leftFolder, "");
    wxFileName fnRight(m_rightFolder, "");

    fnLeft.RemoveLastDir();
    fnRight.RemoveLastDir();
    --m_depth;

    BuildTrees(fnLeft.GetPath(), fnRight.GetPath());
}

void DiffFoldersFrame::OnUpFolderUI(wxUpdateUIEvent& event) { event.Enable(CanUp()); }

bool DiffFoldersFrame::CanUp() const
{
    wxFileName fnLeft(m_leftFolder, "");
    wxFileName fnRight(m_rightFolder, "");
    return m_depth && fnLeft.GetDirCount() && fnRight.GetDirCount();
}