1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * @brief Implementation of methods of CDirView that copy/move/delete files
8 // It would be nice to make this independent of the UI (CDirView)
9 // but it needs access to the list of selected items.
10 // One idea would be to provide an iterator over them.
14 #include "DirActions.h"
16 #include "UnicodeString.h"
18 #include "ShellFileOperations.h"
20 #include "FileActionScript.h"
22 #include "FileFilterHelper.h"
25 static void ThrowConfirmCopy(const CDiffContext& ctxt, int origin, int destination, int count,
26 const String& src, const String& dest, bool destIsSide);
27 static void ThrowConfirmMove(const CDiffContext& ctxt, int origin, int destination, int count,
28 const String& src, const String& dest, bool destIsSide);
29 static void ThrowConfirmationNeededException(const CDiffContext& ctxt, const String &caption, const String &question,
30 int origin, int destination, size_t count,
31 const String& src, const String& dest, bool destIsSide);
33 ContentsChangedException::ContentsChangedException(const String& failpath)
34 : m_msg(strutils::format_string1(
35 _("Operation aborted!\n\nFolder contents at disks has changed, path\n%1\nwas not found.\n\nPlease refresh the compare."),
40 FileOperationException::FileOperationException(const String& msg)
46 * @brief Ask user a confirmation for copying item(s).
47 * Shows a confirmation dialog for copy operation. Depending ont item count
48 * dialog shows full paths to items (single item) or base paths of compare
50 * @param [in] origin Origin side of the item(s).
51 * @param [in] destination Destination side of the item(s).
52 * @param [in] count Number of items.
53 * @param [in] src Source path.
54 * @param [in] dest Destination path.
55 * @param [in] destIsSide Is destination path either of compare sides?
56 * @return true if copy should proceed, false if aborted.
58 static void ThrowConfirmCopy(const CDiffContext& ctxt, int origin, int destination, size_t count,
59 const String& src, const String& dest, bool destIsSide)
61 String caption = _("Confirm Copy");
62 String strQuestion = count == 1 ? _("Are you sure you want to copy?") :
63 strutils::format(_("Are you sure you want to copy %d items?").c_str(), count);
65 ThrowConfirmationNeededException(ctxt, caption, strQuestion, origin,
66 destination, count, src, dest, destIsSide);
70 * @brief Ask user a confirmation for moving item(s).
71 * Shows a confirmation dialog for move operation. Depending ont item count
72 * dialog shows full paths to items (single item) or base paths of compare
74 * @param [in] origin Origin side of the item(s).
75 * @param [in] destination Destination side of the item(s).
76 * @param [in] count Number of items.
77 * @param [in] src Source path.
78 * @param [in] dest Destination path.
79 * @param [in] destIsSide Is destination path either of compare sides?
80 * @return true if copy should proceed, false if aborted.
82 static void ThrowConfirmMove(const CDiffContext& ctxt, int origin, int destination, size_t count,
83 const String& src, const String& dest, bool destIsSide)
85 String caption = _("Confirm Move");
86 String strQuestion = count == 1 ? _("Are you sure you want to move?") :
87 strutils::format(_("Are you sure you want to move %d items?").c_str(), count);
89 ThrowConfirmationNeededException(ctxt, caption, strQuestion, origin,
90 destination, count, src, dest, destIsSide);
94 * @brief Show a (copy/move) confirmation dialog.
95 * @param [in] caption Caption of the dialog.
96 * @param [in] question Guestion to ask from user.
97 * @param [in] origin Origin side of the item(s).
98 * @param [in] destination Destination side of the item(s).
99 * @param [in] count Number of items.
100 * @param [in] src Source path.
101 * @param [in] dest Destination path.
102 * @param [in] destIsSide Is destination path either of compare sides?
103 * @return true if copy should proceed, false if aborted.
105 static void ThrowConfirmationNeededException(const CDiffContext& ctxt, const String &caption, const String &question,
106 int origin, int destination, size_t count,
107 const String& src, const String& dest, bool destIsSide)
109 ConfirmationNeededException exp;
113 exp.m_caption = caption;
116 sOrig = _("From left:");
117 else if (origin == ctxt.GetCompareDirs() - 1)
118 sOrig = _("From right:");
120 sOrig = _("From middle:");
124 // Copy to left / right
125 if (destination == 0)
126 sDest = _("To left:");
127 else if (destination == ctxt.GetCompareDirs() - 1)
128 sDest = _("To right:");
130 sDest = _("To middle:");
134 // Copy left/right to..
139 if (paths::DoesPathExist(src) == paths::IS_EXISTING_DIR)
140 strSrc = paths::AddTrailingSlash(src);
141 String strDest(dest);
142 if (paths::DoesPathExist(dest) == paths::IS_EXISTING_DIR)
143 strDest = paths::AddTrailingSlash(dest);
145 exp.m_question = question;
146 exp.m_fromText = sOrig;
147 exp.m_toText = sDest;
148 exp.m_fromPath = strSrc;
149 exp.m_toPath = strDest;
155 * @brief Confirm actions with user as appropriate
156 * (type, whether single or multiple).
158 void ConfirmActionList(const CDiffContext& ctxt, const FileActionScript & actionList)
160 // TODO: We need better confirmation for file actions.
161 // Maybe we should show a list of files with actions done..
162 FileActionItem item = actionList.GetHeadActionItem();
164 bool bDestIsSide = true;
166 // special handling for the single item case, because it is probably the most common,
167 // and we can give the user exact details easily for it
170 case FileAction::ACT_COPY:
171 if (item.UIResult == FileActionItem::UI_DONT_CARE)
174 if (actionList.GetActionItemCount() == 1)
176 ThrowConfirmCopy(ctxt, item.UIOrigin, item.UIDestination,
177 actionList.GetActionItemCount(), item.src, item.dest,
182 String src = ctxt.GetPath(item.UIOrigin);
187 dst = ctxt.GetPath(item.UIDestination);
191 if (!actionList.m_destBase.empty())
192 dst = actionList.m_destBase;
197 ThrowConfirmCopy(ctxt, item.UIOrigin, item.UIDestination,
198 actionList.GetActionItemCount(), src, dst, bDestIsSide);
202 case FileAction::ACT_DEL:
205 case FileAction::ACT_MOVE:
207 if (actionList.GetActionItemCount() == 1)
209 ThrowConfirmMove(ctxt, item.UIOrigin, item.UIDestination,
210 actionList.GetActionItemCount(), item.src, item.dest,
215 String src = ctxt.GetPath(item.UIOrigin);;
218 if (!actionList.m_destBase.empty())
219 dst = actionList.m_destBase;
223 ThrowConfirmMove(ctxt, item.UIOrigin, item.UIDestination,
224 actionList.GetActionItemCount(), src, dst, bDestIsSide);
230 LogErrorString(_T("Unknown fileoperation in CDirView::ConfirmActionList()"));
231 throw "Unknown fileoperation in ConfirmActionList()";
237 * @brief Update results for FileActionItem.
238 * This functions is called to update DIFFITEM after FileActionItem.
239 * @param [in] act Action that was done.
240 * @param [in] pos List position for DIFFITEM affected.
242 UPDATEITEM_TYPE UpdateDiffAfterOperation(const FileActionItem & act, CDiffContext& ctxt, DIFFITEM &di)
244 bool bUpdateSrc = false;
245 bool bUpdateDest = false;
246 bool bRemoveItem = false;
248 // Use FileActionItem types for simplicity for now.
249 // Better would be to use FileAction contained, since it is not
251 switch (act.UIResult)
253 case FileActionItem::UI_SYNC:
256 di.diffcode.setSideFlag(act.UIDestination);
257 if (act.dirflag || ctxt.GetCompareDirs() > 2)
258 SetDiffCompare(di, DIFFCODE::NOCMP);
260 SetDiffCompare(di, DIFFCODE::SAME);
261 SetDiffCounts(di, 0, 0);
264 case FileActionItem::UI_DEL:
265 if (di.diffcode.isSideOnly(act.UIOrigin))
271 di.diffcode.unsetSideFlag(act.UIOrigin);
272 SetDiffCompare(di, DIFFCODE::NOCMP);
279 ctxt.UpdateStatusFromDisk(&di, act.UIOrigin);
281 ctxt.UpdateStatusFromDisk(&di, act.UIDestination);
284 return UPDATEITEM_REMOVE;
285 if (bUpdateSrc | bUpdateDest)
286 return UPDATEITEM_UPDATE;
287 return UPDATEITEM_NONE;
291 * @brief Find the CDiffContext diffpos of an item from its left & right paths
292 * @return POSITION to item, `nullptr` if not found.
293 * @note Filenames must be same, if they differ `nullptr` is returned.
295 DIFFITEM *FindItemFromPaths(const CDiffContext& ctxt, const PathContext& paths)
298 String file[3], path[3], base;
299 for (nBuffer = 0; nBuffer < paths.GetSize(); ++nBuffer)
301 String p = paths[nBuffer];
302 file[nBuffer] = paths::FindFileName(p);
303 if (file[nBuffer].empty())
305 // Path can contain (because of difftools?) '/' and '\'
306 // so for comparing purposes, convert whole path to use '\\'
307 path[nBuffer] = paths::ToWindowsPath(String(p, 0, p.length() - file[nBuffer].length())); // include trailing backslash
308 base = ctxt.GetPath(nBuffer); // include trailing backslash
309 if (path[nBuffer].compare(0, base.length(), base.c_str()) != 0)
311 path[nBuffer].erase(0, base.length()); // turn into relative path
312 if (String::size_type length = path[nBuffer].length())
313 path[nBuffer].resize(length - 1); // remove trailing backslash
316 // Filenames must be identical
317 if (std::any_of(file, file + paths.GetSize(), [&](auto& it) { return strutils::compare_nocase(it, file[0]) != 0; }))
320 DIFFITEM *pos = ctxt.GetFirstDiffPosition();
321 if (paths.GetSize() == 2)
323 while (DIFFITEM *currentPos = pos) // Save our current pos before getting next
325 const DIFFITEM &di = ctxt.GetNextDiffPosition(pos);
326 if (di.diffFileInfo[0].path == path[0] &&
327 di.diffFileInfo[1].path == path[1] &&
328 di.diffFileInfo[0].filename == file[0] &&
329 di.diffFileInfo[1].filename == file[1])
337 while (DIFFITEM *currentPos = pos) // Save our current pos before getting next
339 const DIFFITEM &di = ctxt.GetNextDiffPosition(pos);
340 if (di.diffFileInfo[0].path == path[0] &&
341 di.diffFileInfo[1].path == path[1] &&
342 di.diffFileInfo[2].path == path[2] &&
343 di.diffFileInfo[0].filename == file[0] &&
344 di.diffFileInfo[1].filename == file[1] &&
345 di.diffFileInfo[2].filename == file[2])
354 /// is it possible to copy item to left ?
355 bool IsItemCopyable(const DIFFITEM &di, int index)
357 // don't let them mess with error items
358 if (di.diffcode.isResultError()) return false;
359 // can't copy same items
360 if (di.diffcode.isResultSame()) return false;
361 // impossible if not existing
362 if (!di.diffcode.exists(index)) return false;
363 // everything else can be copied to other side
367 /// is it possible to delete item ?
368 bool IsItemDeletable(const DIFFITEM &di, int index)
370 // don't let them mess with error items
371 if (di.diffcode.isResultError()) return false;
372 // impossible if not existing
373 if (!di.diffcode.exists(index)) return false;
374 // everything else can be deleted
378 /// is it possible to delete both items ?
379 bool IsItemDeletableOnBoth(const CDiffContext& ctxt, const DIFFITEM &di)
381 // don't let them mess with error items
382 if (di.diffcode.isResultError()) return false;
383 // impossible if only on right or left
384 for (int i = 0; i < ctxt.GetCompareDirs(); ++i)
385 if (!di.diffcode.exists(i)) return false;
387 // everything else can be deleted on both
391 /// is it possible to compare these two items?
392 bool AreItemsOpenable(const CDiffContext& ctxt, SELECTIONTYPE selectionType, const DIFFITEM &di1, const DIFFITEM &di2, bool openableForDir /*= true*/)
394 String sLeftBasePath = ctxt.GetPath(0);
395 String sRightBasePath = ctxt.GetPath(1);
397 // Must be both directory or neither
398 if (di1.diffcode.isDirectory() != di2.diffcode.isDirectory()) return false;
400 if (!openableForDir && di1.diffcode.isDirectory()) return false;
402 switch (selectionType)
404 case SELECTIONTYPE_NORMAL:
405 // Must be on different sides, or one on one side & one on both
406 if (di1.diffcode.isSideFirstOnly() && (di2.diffcode.isSideSecondOnly() ||
407 di2.diffcode.isSideBoth()))
409 if (di1.diffcode.isSideSecondOnly() && (di2.diffcode.isSideFirstOnly() ||
410 di2.diffcode.isSideBoth()))
412 if (di1.diffcode.isSideBoth() && (di2.diffcode.isSideFirstOnly() ||
413 di2.diffcode.isSideSecondOnly()))
416 case SELECTIONTYPE_LEFT1LEFT2:
417 if (di1.diffcode.exists(0) && di2.diffcode.exists(0))
420 case SELECTIONTYPE_RIGHT1RIGHT2:
421 if (di1.diffcode.exists(1) && di2.diffcode.exists(1))
424 case SELECTIONTYPE_LEFT1RIGHT2:
425 if (di1.diffcode.exists(0) && di2.diffcode.exists(1))
428 case SELECTIONTYPE_LEFT2RIGHT1:
429 if (di1.diffcode.exists(1) && di2.diffcode.exists(0))
434 // Allow to compare items if left & right path refer to same directory
435 // (which means there is effectively two files involved). No need to check
436 // side flags. If files weren't on both sides, we'd have no DIFFITEMs.
437 if (strutils::compare_nocase(sLeftBasePath, sRightBasePath) == 0)
442 /// is it possible to compare these three items?
443 bool AreItemsOpenable(const CDiffContext& ctxt, const DIFFITEM &di1, const DIFFITEM &di2, const DIFFITEM &di3, bool openableForDir /*= true*/)
445 String sLeftBasePath = ctxt.GetPath(0);
446 String sMiddleBasePath = ctxt.GetPath(1);
447 String sRightBasePath = ctxt.GetPath(2);
448 String sLeftPath1 = paths::ConcatPath(di1.getFilepath(0, sLeftBasePath), di1.diffFileInfo[0].filename);
449 String sLeftPath2 = paths::ConcatPath(di2.getFilepath(0, sLeftBasePath), di2.diffFileInfo[0].filename);
450 String sLeftPath3 = paths::ConcatPath(di3.getFilepath(0, sLeftBasePath), di3.diffFileInfo[0].filename);
451 String sMiddlePath1 = paths::ConcatPath(di1.getFilepath(1, sMiddleBasePath), di1.diffFileInfo[1].filename);
452 String sMiddlePath2 = paths::ConcatPath(di2.getFilepath(1, sMiddleBasePath), di2.diffFileInfo[1].filename);
453 String sMiddlePath3 = paths::ConcatPath(di3.getFilepath(1, sMiddleBasePath), di3.diffFileInfo[1].filename);
454 String sRightPath1 = paths::ConcatPath(di1.getFilepath(2, sRightBasePath), di1.diffFileInfo[2].filename);
455 String sRightPath2 = paths::ConcatPath(di2.getFilepath(2, sRightBasePath), di2.diffFileInfo[2].filename);
456 String sRightPath3 = paths::ConcatPath(di3.getFilepath(2, sRightBasePath), di3.diffFileInfo[2].filename);
457 // Must not be binary (unless archive)
460 (di1.diffcode.isBin() || di2.diffcode.isBin() || di3.diffcode.isBin())
463 && (sLeftPath1.empty() || ArchiveGuessFormat(sLeftPath1))
464 && (sMiddlePath1.empty() || ArchiveGuessFormat(sMiddlePath1))
465 && (sLeftPath2.empty() || ArchiveGuessFormat(sLeftPath2))
466 && (sMiddlePath2.empty() || ArchiveGuessFormat(sMiddlePath2))
467 && (sLeftPath2.empty() || ArchiveGuessFormat(sLeftPath2))
468 && (sMiddlePath2.empty() || ArchiveGuessFormat(sMiddlePath2)) /* FIXME: */
475 // Must be both directory or neither
476 if (di1.diffcode.isDirectory() != di2.diffcode.isDirectory() && di1.diffcode.isDirectory() != di3.diffcode.isDirectory()) return false;
478 if (!openableForDir && di1.diffcode.isDirectory()) return false;
480 // Must be on different sides, or one on one side & one on both
481 if (di1.diffcode.exists(0) && di2.diffcode.exists(1) && di3.diffcode.exists(2))
483 if (di1.diffcode.exists(0) && di2.diffcode.exists(2) && di3.diffcode.exists(1))
485 if (di1.diffcode.exists(1) && di2.diffcode.exists(0) && di3.diffcode.exists(2))
487 if (di1.diffcode.exists(1) && di2.diffcode.exists(2) && di3.diffcode.exists(0))
489 if (di1.diffcode.exists(2) && di2.diffcode.exists(0) && di3.diffcode.exists(1))
491 if (di1.diffcode.exists(2) && di2.diffcode.exists(1) && di3.diffcode.exists(0))
494 // Allow to compare items if left & right path refer to same directory
495 // (which means there is effectively two files involved). No need to check
496 // side flags. If files weren't on both sides, we'd have no DIFFITEMs.
497 if (strutils::compare_nocase(sLeftBasePath, sMiddleBasePath) == 0 && strutils::compare_nocase(sLeftBasePath, sRightBasePath) == 0)
502 /// is it possible to open item ?
503 bool IsItemOpenableOn(const DIFFITEM &di, int index)
505 // impossible if not existing
506 if (!di.diffcode.exists(index)) return false;
508 // everything else can be opened on right
512 /// is it possible to open left ... item ?
513 bool IsItemOpenableOnWith(const DIFFITEM &di, int index)
515 return (!di.diffcode.isDirectory() && IsItemOpenableOn(di, index));
517 /// is it possible to copy to... left item?
518 bool IsItemCopyableToOn(const DIFFITEM &di, int index)
520 // impossible if only on right
521 if (!di.diffcode.exists(index)) return false;
523 // everything else can be copied to from left
527 // When navigating differences, do we stop at this one ?
528 bool IsItemNavigableDiff(const CDiffContext& ctxt, const DIFFITEM &di)
530 // Not a valid diffitem, one of special items (e.g "..")
531 if (di.diffcode.diffcode == 0)
533 if (di.diffcode.isResultFiltered() || di.diffcode.isResultError())
535 if (!di.diffcode.isResultDiff() && IsItemExistAll(ctxt, di))
540 bool IsItemExistAll(const CDiffContext& ctxt, const DIFFITEM &di)
542 // Not a valid diffitem, one of special items (e.g "..")
543 if (di.diffcode.diffcode == 0)
545 return di.diffcode.existAll();
550 * @brief Determines if the user wants to see given item.
551 * This function determines what items to show and what items to hide. There
552 * are lots of combinations, but basically we check if menuitem is enabled or
553 * disabled and show/hide matching items. For non-recursive compare we never
554 * hide folders as that would disable user browsing into them. And we even
555 * don't really know if folders are identical or different as we haven't
557 * @param [in] di Item to check.
558 * @return true if item should be shown, false if not.
559 * @sa CDirDoc::Redisplay()
561 bool IsShowable(const CDiffContext& ctxt, const DIFFITEM &di, const DirViewFilterSettings& filter)
563 if (di.customFlags & ViewCustomFlags::HIDDEN)
566 if (di.diffcode.isResultFiltered())
568 // Treat SKIPPED as a 'super'-flag. If item is skipped and user
569 // wants to see skipped items show item regardless of other flags
570 return filter.show_skipped;
573 if (di.diffcode.isDirectory())
575 // Subfolders in non-recursive compare can only be skipped or unique
576 if (!ctxt.m_bRecursive)
578 // left/right filters
579 if (di.diffcode.isSideFirstOnly() && !filter.show_unique_left)
581 if (ctxt.GetCompareDirs() < 3)
583 if (di.diffcode.isSideSecondOnly() && !filter.show_unique_right)
588 if (di.diffcode.isSideSecondOnly() && !filter.show_unique_middle)
590 if (di.diffcode.isSideThirdOnly() && !filter.show_unique_right)
592 if (di.diffcode.isMissingFirstOnly() && !filter.show_missing_left_only)
594 if (di.diffcode.isMissingSecondOnly() && !filter.show_missing_middle_only)
596 if (di.diffcode.isMissingThirdOnly() && !filter.show_missing_right_only)
601 if (di.diffcode.isResultError() && false/* !GetMainFrame()->m_bShowErrors FIXME:*/)
604 else // recursive mode (including tree-mode)
606 // left/right filters
607 if (di.diffcode.isSideFirstOnly() && !filter.show_unique_left)
609 if (ctxt.GetCompareDirs() < 3)
611 if (di.diffcode.isSideSecondOnly() && !filter.show_unique_right)
616 if (di.diffcode.isSideSecondOnly() && !filter.show_unique_middle)
618 if (di.diffcode.isSideThirdOnly() && !filter.show_unique_right)
620 if (di.diffcode.isMissingFirstOnly() && !filter.show_missing_left_only)
622 if (di.diffcode.isMissingSecondOnly() && !filter.show_missing_middle_only)
624 if (di.diffcode.isMissingThirdOnly() && !filter.show_missing_right_only)
628 // ONLY filter folders by result (identical/different) for tree-view.
629 // In the tree-view we show subfolders with identical/different
630 // status. The flat view only shows files inside folders. So if we
631 // filter by status the files inside folder are filtered too and
632 // users see files appearing/disappearing without clear logic.
633 if (filter.tree_mode)
636 if (di.diffcode.isResultError() && false/* !GetMainFrame()->m_bShowErrors FIXME:*/)
640 if (di.diffcode.isResultSame() && !filter.show_identical)
642 if (di.diffcode.isResultDiff() && !filter.show_different)
644 DIFFITEM *diffpos = ctxt.GetFirstChildDiffPosition(&di);
645 while (diffpos != nullptr)
647 const DIFFITEM &dic = ctxt.GetNextSiblingDiffPosition(diffpos);
648 if (IsShowable(ctxt, dic, filter))
658 // left/right filters
659 if (di.diffcode.isSideFirstOnly() && !filter.show_unique_left)
661 if (ctxt.GetCompareDirs() < 3)
663 if (di.diffcode.isSideSecondOnly() && !filter.show_unique_right)
668 if (di.diffcode.isSideSecondOnly() && !filter.show_unique_middle)
670 if (di.diffcode.isSideThirdOnly() && !filter.show_unique_right)
672 if (di.diffcode.isMissingFirstOnly() && !filter.show_missing_left_only)
674 if (di.diffcode.isMissingSecondOnly() && !filter.show_missing_middle_only)
676 if (di.diffcode.isMissingThirdOnly() && !filter.show_missing_right_only)
681 if (di.diffcode.isBin() && !filter.show_binaries)
685 if (di.diffcode.isResultSame() && !filter.show_identical)
687 if (di.diffcode.isResultError() && false/* && !GetMainFrame()->m_bShowErrors FIXME:*/)
689 if (ctxt.GetCompareDirs() < 3)
691 if (di.diffcode.isResultDiff() && !filter.show_different)
696 if ((di.diffcode.diffcode & DIFFCODE::COMPAREFLAGS3WAY) == DIFFCODE::DIFF1STONLY)
698 if (!filter.show_different_left_only)
701 else if ((di.diffcode.diffcode & DIFFCODE::COMPAREFLAGS3WAY) == DIFFCODE::DIFF2NDONLY)
703 if (!filter.show_different_middle_only)
706 else if ((di.diffcode.diffcode & DIFFCODE::COMPAREFLAGS3WAY) == DIFFCODE::DIFF3RDONLY)
708 if (!filter.show_different_right_only)
711 else if (di.diffcode.isResultDiff() && !filter.show_different)
719 * @brief Open one selected item.
720 * @param [in] pos1 Item position.
721 * @param [in,out] di1 Pointer to first diffitem.
722 * @param [in,out] di2 Pointer to second diffitem.
723 * @param [in,out] di3 Pointer to third diffitem.
724 * @param [out] paths First/Second/Third paths.
725 * @param [out] sel1 Item's selection index in listview.
726 * @param [in,out] isDir Is item folder?
727 * @param [in] openableForDir Are items openable if the items are directories?
728 * return false if there was error or item was completely processed.
730 bool GetOpenOneItem(const CDiffContext& ctxt, DIFFITEM *pos1, const DIFFITEM *pdi[3],
731 PathContext & paths, int & sel1, bool & isdir, int nPane[3], String& errmsg, bool openableForDir /*= true*/)
733 pdi[0] = &ctxt.GetDiffAt(pos1);
737 if (!openableForDir && pdi[0]->diffcode.isDirectory()) return false;
739 paths = GetItemFileNames(ctxt, *pdi[0]);
741 for (int nIndex = 0; nIndex < paths.GetSize(); ++nIndex)
742 nPane[nIndex] = nIndex;
744 if (pdi[0]->diffcode.isDirectory())
747 if (isdir && (pdi[0]->diffcode.existsFirst() && pdi[1]->diffcode.existsSecond() && pdi[2]->diffcode.existsThird()))
749 // Check both folders exist. If either folder is missing that means
750 // folder has been changed behind our back, so we just tell user to
751 // refresh the compare.
752 paths::PATH_EXISTENCE path1Exists = paths::DoesPathExist(paths[0]);
753 paths::PATH_EXISTENCE path2Exists = paths::DoesPathExist(paths[1]);
754 if (path1Exists != paths::IS_EXISTING_DIR || path2Exists != paths::IS_EXISTING_DIR)
756 String invalid = path1Exists == paths::IS_EXISTING_DIR ? paths[0] : paths[1];
757 errmsg = strutils::format_string1(
758 _("Operation aborted!\n\nFolder contents at disks has changed, path\n%1\nwas not found.\n\nPlease refresh the compare."),
768 * @brief Open two selected items.
769 * @param [in] pos1 First item position.
770 * @param [in] pos2 Second item position.
771 * @param [in,out] di1 Pointer to first diffitem.
772 * @param [in,out] di2 Pointer to second diffitem.
773 * @param [out] paths First/Second/Third paths.
774 * @param [out] sel1 First item's selection index in listview.
775 * @param [out] sel2 Second item's selection index in listview.
776 * @param [in,out] isDir Is item folder?
777 * @param [in] openableForDir Are items openable if the items are directories?
778 * return false if there was error or item was completely processed.
780 bool GetOpenTwoItems(const CDiffContext& ctxt, SELECTIONTYPE selectionType, DIFFITEM *pos1, DIFFITEM *pos2, const DIFFITEM *pdi[3],
781 PathContext & paths, int & sel1, int & sel2, bool & isDir, int nPane[3], String& errmsg, bool openableForDir /*= true*/)
783 // Two items selected, get their info
784 pdi[0] = &ctxt.GetDiffAt(pos1);
785 pdi[1] = &ctxt.GetDiffAt(pos2);
789 // Check for binary & side compatibility & file/dir compatibility
790 if (!AreItemsOpenable(ctxt, selectionType, *pdi[0], *pdi[1], openableForDir))
795 switch (selectionType)
797 case SELECTIONTYPE_NORMAL:
798 // Ensure that di1 is on left (swap if needed)
799 if (pdi[0]->diffcode.isSideSecondOnly() || (pdi[0]->diffcode.isSideBoth() &&
800 pdi[1]->diffcode.isSideFirstOnly()))
802 std::swap(pdi[0], pdi[1]);
803 std::swap(sel1, sel2);
806 case SELECTIONTYPE_LEFT1LEFT2:
807 nPane[0] = nPane[1] = 0;
809 case SELECTIONTYPE_RIGHT1RIGHT2:
810 nPane[0] = nPane[1] = 1;
812 case SELECTIONTYPE_LEFT1RIGHT2:
814 case SELECTIONTYPE_LEFT2RIGHT1:
815 std::swap(pdi[0], pdi[1]);
816 std::swap(sel1, sel2);
820 PathContext files1, files2;
821 files1 = GetItemFileNames(ctxt, *pdi[0]);
822 files2 = GetItemFileNames(ctxt, *pdi[1]);
823 paths.SetLeft(files1[nPane[0]]);
824 paths.SetRight(files2[nPane[1]]);
826 if (pdi[0]->diffcode.isDirectory())
829 if (paths::GetPairComparability(paths) != paths::IS_EXISTING_DIR)
831 errmsg = _("The selected folder is invalid.");
840 * @brief Open three selected items.
841 * @param [in] pos1 First item position.
842 * @param [in] pos2 Second item position.
843 * @param [in] pos3 Third item position.
844 * @param [in,out] di1 Pointer to first diffitem.
845 * @param [in,out] di2 Pointer to second diffitem.
846 * @param [in,out] di3 Pointer to third diffitem.
847 * @param [out] paths First/Second/Third paths.
848 * @param [out] sel1 First item's selection index in listview.
849 * @param [out] sel2 Second item's selection index in listview.
850 * @param [out] sel3 Third item's selection index in listview.
851 * @param [in,out] isDir Is item folder?
852 * @param [in] openableForDir Are items openable if the items are directories?
853 * return false if there was error or item was completely processed.
855 bool GetOpenThreeItems(const CDiffContext& ctxt, DIFFITEM *pos1, DIFFITEM *pos2, DIFFITEM *pos3, const DIFFITEM *pdi[3],
856 PathContext & paths, int & sel1, int & sel2, int & sel3, bool & isDir, int nPane[3], String& errmsg, bool openableForDir /*= true*/)
858 String pathLeft, pathMiddle, pathRight;
861 for (int nIndex = 0; nIndex < 3; ++nIndex)
862 nPane[nIndex] = nIndex;
865 // Two items selected, get their info
866 pdi[0] = &ctxt.GetDiffAt(pos1);
867 pdi[1] = &ctxt.GetDiffAt(pos2);
869 // Check for binary & side compatibility & file/dir compatibility
870 if (!::AreItemsOpenable(ctxt, *pdi[0], *pdi[1], *pdi[1], openableForDir) &&
871 !::AreItemsOpenable(ctxt, *pdi[0], *pdi[0], *pdi[1], openableForDir))
875 // Ensure that pdi[0] is on left (swap if needed)
876 if (pdi[0]->diffcode.exists(0) && pdi[0]->diffcode.exists(1) && pdi[1]->diffcode.exists(2))
883 else if (pdi[0]->diffcode.exists(0) && pdi[0]->diffcode.exists(2) && pdi[1]->diffcode.exists(1))
888 else if (pdi[0]->diffcode.exists(1) && pdi[0]->diffcode.exists(2) && pdi[1]->diffcode.exists(0))
890 std::swap(pdi[0], pdi[1]);
891 std::swap(sel1, sel2);
895 else if (pdi[1]->diffcode.exists(0) && pdi[1]->diffcode.exists(1) && pdi[0]->diffcode.exists(2))
897 std::swap(pdi[0], pdi[1]);
898 std::swap(sel1, sel2);
904 else if (pdi[1]->diffcode.exists(0) && pdi[1]->diffcode.exists(2) && pdi[0]->diffcode.exists(1))
906 std::swap(pdi[0], pdi[1]);
907 std::swap(sel1, sel2);
911 else if (pdi[1]->diffcode.exists(1) && pdi[1]->diffcode.exists(2) && pdi[0]->diffcode.exists(0))
919 // Three items selected, get their info
920 pdi[0] = &ctxt.GetDiffAt(pos1);
921 pdi[1] = &ctxt.GetDiffAt(pos2);
922 pdi[2] = &ctxt.GetDiffAt(pos3);
924 // Check for binary & side compatibility & file/dir compatibility
925 if (!::AreItemsOpenable(ctxt, *pdi[0], *pdi[1], *pdi[2], openableForDir))
929 // Ensure that pdi[0] is on left (swap if needed)
930 if (pdi[0]->diffcode.exists(0) && pdi[1]->diffcode.exists(1) && pdi[2]->diffcode.exists(2))
933 else if (pdi[0]->diffcode.exists(0) && pdi[1]->diffcode.exists(2) && pdi[2]->diffcode.exists(1))
935 std::swap(pdi[1], pdi[2]);
936 std::swap(sel2, sel3);
938 else if (pdi[0]->diffcode.exists(1) && pdi[1]->diffcode.exists(0) && pdi[2]->diffcode.exists(2))
940 std::swap(pdi[0], pdi[1]);
941 std::swap(sel1, sel2);
943 else if (pdi[0]->diffcode.exists(1) && pdi[1]->diffcode.exists(2) && pdi[2]->diffcode.exists(0))
945 std::swap(pdi[0], pdi[2]);
946 std::swap(sel1, sel3);
947 std::swap(pdi[1], pdi[2]);
948 std::swap(sel2, sel3);
950 else if (pdi[0]->diffcode.exists(2) && pdi[1]->diffcode.exists(0) && pdi[2]->diffcode.exists(1))
952 std::swap(pdi[0], pdi[1]);
953 std::swap(sel1, sel2);
954 std::swap(pdi[1], pdi[2]);
955 std::swap(sel2, sel3);
957 else if (pdi[0]->diffcode.exists(2) && pdi[1]->diffcode.exists(1) && pdi[2]->diffcode.exists(0))
959 std::swap(pdi[0], pdi[2]);
960 std::swap(sel1, sel3);
964 // Fill in pathLeft & & pathMiddle & pathRight
965 PathContext pathsTemp = GetItemFileNames(ctxt, *pdi[0]);
966 pathLeft = pathsTemp[0];
967 pathsTemp = GetItemFileNames(ctxt, *pdi[1]);
968 pathMiddle = pathsTemp[1];
969 pathsTemp = GetItemFileNames(ctxt, *pdi[2]);
970 pathRight = pathsTemp[2];
972 paths.SetLeft(pathLeft);
973 paths.SetMiddle(pathMiddle);
974 paths.SetRight(pathRight);
976 if (pdi[0]->diffcode.isDirectory())
979 if (paths::GetPairComparability(paths) != paths::IS_EXISTING_DIR)
981 errmsg = _("The selected folder is invalid.");
990 * @brief Get the file names on both sides for specified item.
991 * @note Return empty strings if item is special item.
993 void GetItemFileNames(const CDiffContext& ctxt, const DIFFITEM &di, String& strLeft, String& strRight)
995 const String leftrelpath = paths::ConcatPath(di.diffFileInfo[0].path, di.diffFileInfo[0].filename);
996 const String rightrelpath = paths::ConcatPath(di.diffFileInfo[1].path, di.diffFileInfo[1].filename);
997 const String & leftpath = ctxt.GetPath(0);
998 const String & rightpath = ctxt.GetPath(1);
999 strLeft = paths::ConcatPath(leftpath, leftrelpath);
1000 strRight = paths::ConcatPath(rightpath, rightrelpath);
1003 String GetItemFileName(const CDiffContext& ctxt, const DIFFITEM &di, int index)
1005 return paths::ConcatPath(ctxt.GetPath(index), paths::ConcatPath(di.diffFileInfo[index].path, di.diffFileInfo[index].filename));
1008 PathContext GetItemFileNames(const CDiffContext& ctxt, const DIFFITEM &di)
1011 for (int nIndex = 0; nIndex < ctxt.GetCompareDirs(); nIndex++)
1013 const String relpath = paths::ConcatPath(di.diffFileInfo[nIndex].path, di.diffFileInfo[nIndex].filename);
1014 const String & path = ctxt.GetPath(nIndex);
1015 paths.SetPath(nIndex, paths::ConcatPath(path, relpath));
1021 * @brief Return image index appropriate for this row
1023 int GetColImage(const DIFFITEM &di)
1025 // Must return an image index into image list created above in OnInitDialog
1026 if (di.diffcode.isResultError())
1027 return DIFFIMG_ERROR;
1028 if (di.diffcode.isResultAbort())
1029 return DIFFIMG_ABORT;
1030 if (di.diffcode.isResultFiltered())
1031 return (di.diffcode.isDirectory() ? DIFFIMG_DIRSKIP : DIFFIMG_SKIP);
1032 if (di.diffcode.isSideFirstOnly())
1033 return (di.diffcode.isDirectory() ? DIFFIMG_LDIRUNIQUE : DIFFIMG_LUNIQUE);
1034 if (di.diffcode.isSideSecondOnly())
1035 return ((di.diffcode.diffcode & DIFFCODE::THREEWAY) == 0 ?
1036 (di.diffcode.isDirectory() ? DIFFIMG_RDIRUNIQUE : DIFFIMG_RUNIQUE) :
1037 (di.diffcode.isDirectory() ? DIFFIMG_MDIRUNIQUE : DIFFIMG_MUNIQUE));
1038 if (di.diffcode.isSideThirdOnly())
1039 return (di.diffcode.isDirectory() ? DIFFIMG_RDIRUNIQUE : DIFFIMG_RUNIQUE);
1040 if ((di.diffcode.diffcode & DIFFCODE::THREEWAY) != 0)
1042 if (!di.diffcode.exists(0))
1043 return (di.diffcode.isDirectory() ? DIFFIMG_LDIRMISSING : DIFFIMG_LMISSING);
1044 if (!di.diffcode.exists(1))
1045 return (di.diffcode.isDirectory() ? DIFFIMG_MDIRMISSING : DIFFIMG_MMISSING);
1046 if (!di.diffcode.exists(2))
1047 return (di.diffcode.isDirectory() ? DIFFIMG_RDIRMISSING : DIFFIMG_RMISSING);
1049 if (di.diffcode.isResultSame())
1051 if (di.diffcode.isDirectory())
1052 return DIFFIMG_DIRSAME;
1055 if (di.diffcode.isText())
1056 return DIFFIMG_TEXTSAME;
1057 else if (di.diffcode.isBin())
1058 return DIFFIMG_BINSAME;
1059 else if (di.diffcode.isImage())
1060 return DIFFIMG_IMAGESAME;
1062 return DIFFIMG_SAME;
1066 if (di.diffcode.isResultDiff())
1068 if (di.diffcode.isDirectory())
1069 return DIFFIMG_DIRDIFF;
1072 if (di.diffcode.isText())
1073 return DIFFIMG_TEXTDIFF;
1074 else if (di.diffcode.isBin())
1075 return DIFFIMG_BINDIFF;
1076 else if (di.diffcode.isImage())
1077 return DIFFIMG_IMAGEDIFF;
1079 return DIFFIMG_DIFF;
1082 return (di.diffcode.isDirectory() ? DIFFIMG_DIR : DIFFIMG_FILE );
1086 * @brief Set side status of diffitem
1087 * @note This does not update UI - ReloadItemStatus() does
1088 * @sa CDirDoc::ReloadItemStatus()
1090 void SetDiffSide(DIFFITEM& di, unsigned diffcode)
1092 SetDiffStatus(di, diffcode, DIFFCODE::SIDEFLAGS);
1096 * @brief Set compare status of diffitem
1097 * @note This does not update UI - ReloadItemStatus() does
1098 * @sa CDirDoc::ReloadItemStatus()
1100 void SetDiffCompare(DIFFITEM& di, unsigned diffcode)
1102 SetDiffStatus(di, diffcode, DIFFCODE::COMPAREFLAGS);
1106 * @brief Set status for diffitem
1107 * @param diffcode New code
1108 * @param mask Defines allowed set of flags to change
1109 * @param idx Item's index to list in UI
1111 void SetDiffStatus(DIFFITEM& di, unsigned diffcode, unsigned mask)
1113 // TODO: Why is the update broken into these pieces ?
1114 // Someone could figure out these pieces and probably simplify this.
1116 // Update DIFFITEM code (comparison result)
1117 assert( ((~mask) & diffcode) == 0 ); // make sure they only set flags in their mask
1118 di.diffcode.diffcode &= (~mask); // remove current data
1119 di.diffcode.diffcode |= diffcode; // add new data
1121 // update DIFFITEM time (and other disk info), and tell views
1124 void SetDiffCounts(DIFFITEM& di, unsigned diffs, unsigned ignored)
1126 di.nidiffs = ignored; // see StoreDiffResult() in DirScan.cpp
1131 * @brief Set item's view-flag.
1132 * @param [in] key Item fow which flag is set.
1133 * @param [in] flag Flag value to set.
1134 * @param [in] mask Mask for possible flag values.
1136 void SetItemViewFlag(DIFFITEM& di, unsigned flag, unsigned mask)
1138 unsigned curFlags = di.customFlags;
1139 curFlags &= ~mask; // Zero bits masked
1141 di.customFlags = curFlags;
1145 * @brief Set all item's view-flag.
1146 * @param [in] flag Flag value to set.
1147 * @param [in] mask Mask for possible flag values.
1149 void SetItemViewFlag(CDiffContext& ctxt, unsigned flag, unsigned mask)
1151 DIFFITEM *pos = ctxt.GetFirstDiffPosition();
1153 while (pos != nullptr)
1155 unsigned curFlags = ctxt.GetCustomFlags1(pos);
1156 curFlags &= ~mask; // Zero bits masked
1158 ctxt.SetCustomFlags1(pos, curFlags);
1159 ctxt.GetNextDiffPosition(pos);
1164 * @brief Mark selected items as needing for rescan.
1165 * @return Count of items to rescan.
1167 void MarkForRescan(DIFFITEM &di)
1169 SetDiffStatus(di, 0, DIFFCODE::TEXTFLAGS | DIFFCODE::SIDEFLAGS | DIFFCODE::COMPAREFLAGS);
1170 SetDiffStatus(di, DIFFCODE::NEEDSCAN, DIFFCODE::SCANFLAGS);
1174 * @brief Return string such as "15 of 30 Files Affected" or "30 Files Affected"
1176 String FormatFilesAffectedString(int nFilesAffected, int nFilesTotal)
1178 if (nFilesAffected == nFilesTotal)
1179 return strutils::format_string1(_("(%1 Files Affected)"), NumToStr(nFilesTotal));
1181 return strutils::format_string2(_("(%1 of %2 Files Affected)"), NumToStr(nFilesAffected), NumToStr(nFilesTotal));
1184 String FormatMenuItemString(const String& fmt1, const String& fmt2, int count, int total)
1187 return strutils::format_string1(fmt1, NumToStr(total));
1189 return strutils::format_string2(fmt2, NumToStr(count), NumToStr(total));
1192 String FormatMenuItemString(SIDE_TYPE src, SIDE_TYPE dst, int count, int total)
1195 if (src == SIDE_LEFT && dst == SIDE_RIGHT)
1197 fmt1 = _("Left to Right (%1)");
1198 fmt2 = _("Left to Right (%1 of %2)");
1200 else if (src == SIDE_LEFT && dst == SIDE_MIDDLE)
1202 fmt1 = _("Left to Middle (%1)");
1203 fmt2 = _("Left to Middle (%1 of %2)");
1205 else if (src == SIDE_MIDDLE && dst == SIDE_LEFT)
1207 fmt1 = _("Middle to Left (%1)");
1208 fmt2 = _("Middle to Left (%1 of %2)");
1210 else if (src == SIDE_MIDDLE && dst == SIDE_RIGHT)
1212 fmt1 = _("Middle to Right (%1)");
1213 fmt2 = _("Middle to Right (%1 of %2)");
1215 else if (src == SIDE_RIGHT && dst == SIDE_LEFT)
1217 fmt1 = _("Right to Left (%1)");
1218 fmt2 = _("Right to Left (%1 of %2)");
1220 else if (src == SIDE_RIGHT && dst == SIDE_MIDDLE)
1222 fmt1 = _("Right to Middle (%1)");
1223 fmt2 = _("Right to Middle (%1 of %2)");
1225 return FormatMenuItemString(fmt1, fmt2, count, total);
1228 String FormatMenuItemString(SIDE_TYPE src, int count, int total)
1231 if (src == SIDE_LEFT)
1233 fmt1 = _("Left (%1)");
1234 fmt2 = _("Left (%1 of %2)");
1236 else if (src == SIDE_MIDDLE)
1238 fmt1 = _("Middle (%1)");
1239 fmt2 = _("Middle (%1 of %2)");
1241 else if (src == SIDE_RIGHT)
1243 fmt1 = _("Right (%1)");
1244 fmt2 = _("Right (%1 of %2)");
1246 return FormatMenuItemString(fmt1, fmt2, count, total);
1249 String FormatMenuItemStringAll(int nDirs, int count, int total)
1252 return FormatMenuItemString(_("Both (%1)"), _("Both (%1 of %2)"), count, total);
1254 return FormatMenuItemString(_("All (%1)"), _("All (%1 of %2)"), count, total);
1257 String FormatMenuItemStringTo(SIDE_TYPE src, int count, int total)
1260 if (src == SIDE_LEFT)
1262 fmt1 = _("Left to... (%1)");
1263 fmt2 = _("Left to... (%1 of %2)");
1265 else if (src == SIDE_MIDDLE)
1267 fmt1 = _("Middle to... (%1)");
1268 fmt2 = _("Middle to... (%1 of %2)");
1270 else if (src == SIDE_RIGHT)
1272 fmt1 = _("Right to... (%1)");
1273 fmt2 = _("Right to... (%1 of %2)");
1275 return FormatMenuItemString(fmt1, fmt2, count, total);
1278 String FormatMenuItemStringAllTo(int nDirs, int count, int total)
1281 return FormatMenuItemString(_("Both to... (%1)"), _("Both to... (%1 of %2)"), count, total);
1283 return FormatMenuItemString(_("All to... (%1)"), _("All to... (%1 of %2)"), count, total);
1286 String FormatMenuItemStringDifferencesTo(int count, int total)
1288 return FormatMenuItemString(_("Differences to... (%1)"), _("Differences to... (%1 of %2)"), count, total);
1292 * @brief Rename a file without moving it to different directory.
1294 * @param szOldFileName [in] Full path of file to rename.
1295 * @param szNewFileName [in] New file name (without the path).
1297 * @return true if file was renamed successfully.
1299 bool RenameOnSameDir(const String& szOldFileName, const String& szNewFileName)
1301 bool bSuccess = false;
1303 if (paths::DOES_NOT_EXIST != paths::DoesPathExist(szOldFileName))
1305 String sFullName = paths::ConcatPath(paths::GetPathOnly(szOldFileName), szNewFileName);
1307 // No need to rename if new file already exist.
1308 if ((sFullName != szOldFileName) ||
1309 (paths::DOES_NOT_EXIST == paths::DoesPathExist(sFullName)))
1311 ShellFileOperations fileOp;
1312 fileOp.SetOperation(FO_RENAME, 0);
1313 fileOp.AddSourceAndDestination(szOldFileName, sFullName);
1314 bSuccess = fileOp.Run();
1326 * @brief Convert number to string.
1327 * Converts number to string, with commas between digits in
1328 * locale-appropriate manner.
1330 String NumToStr(int n)
1332 return locality::NumToLocaleStr(n);
1335 void ExpandSubdirs(CDiffContext& ctxt, DIFFITEM& dip)
1337 dip.customFlags |= ViewCustomFlags::EXPANDED;
1338 DIFFITEM *diffpos = ctxt.GetFirstChildDiffPosition(&dip);
1339 while (diffpos != nullptr)
1341 DIFFITEM &di = ctxt.GetNextDiffRefPosition(diffpos);
1342 if (!di.IsAncestor(&dip))
1344 if (di.HasChildren())
1345 di.customFlags |= ViewCustomFlags::EXPANDED;
1349 void ExpandAllSubdirs(CDiffContext& ctxt)
1351 DIFFITEM *diffpos = ctxt.GetFirstDiffPosition();
1352 while (diffpos != nullptr)
1354 DIFFITEM &di = ctxt.GetNextDiffRefPosition(diffpos);
1355 di.customFlags |= ViewCustomFlags::EXPANDED;
1359 void CollapseAllSubdirs(CDiffContext& ctxt)
1361 DIFFITEM *diffpos = ctxt.GetFirstDiffPosition();
1362 while (diffpos != nullptr)
1364 DIFFITEM &di = ctxt.GetNextDiffRefPosition(diffpos);
1365 di.customFlags &= ~ViewCustomFlags::EXPANDED;
1369 DirViewTreeState *SaveTreeState(const CDiffContext& ctxt)
1371 DirViewTreeState *pTreeState = new DirViewTreeState();
1372 DIFFITEM *diffpos = ctxt.GetFirstDiffPosition();
1373 while (diffpos != nullptr)
1375 const DIFFITEM &di = ctxt.GetNextDiffPosition(diffpos);
1376 if (di.HasChildren())
1378 String relpath = paths::ConcatPath(di.diffFileInfo[0].path, di.diffFileInfo[0].filename);
1379 pTreeState->insert(std::pair<String, bool>(relpath, !!(di.customFlags & ViewCustomFlags::EXPANDED)));
1385 void RestoreTreeState(CDiffContext& ctxt, DirViewTreeState *pTreeState)
1387 DIFFITEM *diffpos = ctxt.GetFirstDiffPosition();
1388 while (diffpos != nullptr)
1390 DIFFITEM &di = ctxt.GetNextDiffRefPosition(diffpos);
1391 if (di.HasChildren())
1393 String relpath = paths::ConcatPath(di.diffFileInfo[0].path, di.diffFileInfo[0].filename);
1394 std::map<String, bool>::iterator p = pTreeState->find(relpath);
1395 if (p != pTreeState->end())
1397 di.customFlags &= ~ViewCustomFlags::EXPANDED;
1398 di.customFlags |= (p->second ? ViewCustomFlags::EXPANDED : 0);
1405 * @brief Tell if user may use ".." and move to parents directory.
1406 * This function checks if current compare's parent folders should
1407 * be allowed to open. I.e. if current compare folders are:
1408 * - C:\Work\Project1 and
1409 * - C:\Work\Project2
1410 * we check if C:\Work and C:\Work should be allowed to opened.
1411 * For regular folders we allow opening if both folders exist.
1412 * @param [out] leftParent Left parent folder to open.
1413 * @param [out] rightParent Right parent folder to open.
1414 * @return Info if opening parent folders should be enabled:
1415 * - No : upward RESTRICTED
1416 * - ParentIsRegularPath : upward ENABLED
1417 * - ParentIsTempPath : upward ENABLED
1419 AllowUpwardDirectory::ReturnCode
1420 CheckAllowUpwardDirectory(const CDiffContext& ctxt, const CTempPathContext *pTempPathContext, PathContext &pathsParent)
1422 std::vector<String> path(ctxt.GetCompareDirs());
1423 for (int i = 0; i < static_cast<int>(path.size()); ++i)
1424 path[i] = ctxt.GetNormalizedPath(i);
1426 // If we have temp context it means we are comparing archives
1427 if (pTempPathContext != nullptr)
1429 std::vector<String> name(path.size());
1430 for (int i = 0; i < static_cast<int>(path.size()); ++i)
1431 name[i] = paths::FindFileName(path[i]);
1433 String::size_type cchLeftRoot = pTempPathContext->m_strRoot[0].length();
1434 if (path[0].length() <= cchLeftRoot)
1436 pathsParent.SetSize(ctxt.GetCompareDirs());
1437 if (pTempPathContext->m_pParent)
1439 for (int i = 0; i < static_cast<int>(path.size()); ++i)
1440 pathsParent[i] = pTempPathContext->m_pParent->m_strRoot[i];
1441 if (paths::GetPairComparability(pathsParent) != paths::IS_EXISTING_DIR)
1442 return AllowUpwardDirectory::Never;
1443 return AllowUpwardDirectory::ParentIsTempPath;
1445 for (int i = 0; i < static_cast<int>(path.size()); ++i)
1446 pathsParent[i] = pTempPathContext->m_strDisplayRoot[i];
1447 if (pathsParent.GetSize() < 3)
1449 if (!ctxt.m_piFilterGlobal->includeFile(pathsParent[0], pathsParent[1]))
1450 return AllowUpwardDirectory::Never;
1454 if (!ctxt.m_piFilterGlobal->includeFile(pathsParent[0], pathsParent[1], pathsParent[2]))
1455 return AllowUpwardDirectory::Never;
1457 if (path.size() == 2 &&
1458 strutils::compare_nocase(name[0], _T("ORIGINAL")) == 0 &&
1459 strutils::compare_nocase(name[1], _T("ALTERED")) == 0)
1461 for (int i = 0; i < static_cast<int>(path.size()); ++i)
1462 pathsParent[i] = paths::GetParentPath(pathsParent[i]);
1463 for (int i = 0; i < static_cast<int>(path.size()); ++i)
1464 name[i] = paths::FindFileName(pathsParent[i]);
1465 if (strutils::compare_nocase(name[0], name[1]) == 0)
1467 if (paths::GetPairComparability(pathsParent) != paths::IS_EXISTING_DIR)
1468 return AllowUpwardDirectory::Never;
1469 return AllowUpwardDirectory::ParentIsTempPath;
1472 return AllowUpwardDirectory::No;
1476 // If regular parent folders exist, allow opening them
1477 pathsParent.SetSize(ctxt.GetCompareDirs());
1478 for (int i = 0; i < static_cast<int>(path.size()); ++i)
1479 pathsParent[i] = paths::GetParentPath(path[i]);
1480 if (paths::GetPairComparability(pathsParent) != paths::IS_EXISTING_DIR)
1481 return AllowUpwardDirectory::Never;
1482 return AllowUpwardDirectory::ParentIsRegularPath;