1 /*
2     This file is part of the Okteta Kasten module, made within the KDE community.
3 
4     SPDX-FileCopyrightText: 2006-2009 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8 
9 #include "replacecontroller.hpp"
10 
11 // controller
12 #include "replacedialog.hpp"
13 #include "replaceprompt.hpp"
14 #include "replacetool.hpp"
15 // KF
16 #include <KXmlGuiWindow>
17 #include <KLocalizedString>
18 #include <KActionCollection>
19 #include <KStandardAction>
20 #include <KMessageBox>
21 // Qt
22 #include <QAction>
23 
24 namespace Kasten {
25 
26 // TODO: for docked widgets signal widgets if embedded or floating, if horizontal/vertical
ReplaceController(KXMLGUIClient * guiClient,QWidget * parentWidget)27 ReplaceController::ReplaceController(KXMLGUIClient* guiClient, QWidget* parentWidget)
28     : mParentWidget(parentWidget)
29 {
30     mReplaceAction = KStandardAction::replace(this, &ReplaceController::replace, this);
31 
32     guiClient->actionCollection()->addAction(mReplaceAction->objectName(), mReplaceAction);
33 
34     mTool = new ReplaceTool();
35     mTool->setUserQueryAgent(this);
36 
37     connect(mTool, &ReplaceTool::isApplyableChanged,
38             mReplaceAction, &QAction::setEnabled);
39 
40     connect(mTool, &ReplaceTool::finished, this, &ReplaceController::onFinished);
41 
42     mReplaceAction->setEnabled(false);
43 }
44 
~ReplaceController()45 ReplaceController::~ReplaceController()
46 {
47     delete mReplaceDialog;
48     delete mReplacePrompt;
49     delete mTool;
50 }
51 
setTargetModel(AbstractModel * model)52 void ReplaceController::setTargetModel(AbstractModel* model)
53 {
54     mTool->setTargetModel(model);
55 }
56 
replace()57 void ReplaceController::replace()
58 {
59     // ensure dialog
60     if (!mReplaceDialog) {
61         mReplaceDialog = new ReplaceDialog(mTool, mParentWidget);
62     }
63 
64     mReplaceDialog->show();
65 }
66 
onFinished(bool previousFound,int noOfReplacements)67 void ReplaceController::onFinished(bool previousFound, int noOfReplacements)
68 {
69     if (mReplacePrompt) {
70         mReplacePrompt->hide();
71     }
72 
73     const QString messageBoxTitle = i18nc("@title:window", "Replace");
74     const QString replacementReport = (noOfReplacements == 0) ?
75                                       i18nc("@info", "No replacements made.") :
76                                       i18ncp("@info", "1 replacement made.", "%1 replacements made.", noOfReplacements);
77 
78     if (!previousFound) {
79         KMessageBox::sorry(mParentWidget, i18nc("@info", "Replace pattern not found in byte array."), messageBoxTitle);
80     } else {
81         KMessageBox::information(mParentWidget, replacementReport, messageBoxTitle);
82     }
83 }
84 
queryContinue(FindDirection direction,int noOfReplacements)85 void ReplaceController::queryContinue(FindDirection direction, int noOfReplacements)
86 {
87     const QString messageBoxTitle = i18nc("@title:window", "Replace");
88     const QString replacementReport = (noOfReplacements == 0) ?
89                                       i18nc("@info", "No replacements made.") :
90                                       i18ncp("@info", "1 replacement made.", "%1 replacements made.", noOfReplacements);
91     const QString question = (direction == FindForward) ?
92                              xi18nc("@info", "End of byte array reached.<nl/>Continue from the beginning?") :
93                              xi18nc("@info", "Beginning of byte array reached.<nl/>Continue from the end?");
94 
95     const QString message = replacementReport + QLatin1String("<br /><br />") + question;
96     const int answer = KMessageBox::questionYesNo(mParentWidget, message, messageBoxTitle,
97                                                   KStandardGuiItem::cont(), KStandardGuiItem::cancel());
98 
99     const bool result = (answer != KMessageBox::No);
100 
101     emit queryContinueFinished(result);
102 }
103 
queryReplaceCurrent()104 void ReplaceController::queryReplaceCurrent()
105 {
106     if (!mReplacePrompt) {
107         mReplacePrompt = new ReplacePrompt(mParentWidget);
108         connect(mReplacePrompt, &ReplacePrompt::finished,
109                 this, &ReplaceController::onPromptReply);
110     }
111     mReplacePrompt->show();
112 }
113 
onPromptReply(ReplaceBehaviour behaviour)114 void ReplaceController::onPromptReply(ReplaceBehaviour behaviour)
115 {
116     if (behaviour == ReplaceAll || behaviour == CancelReplacing) {
117         mReplacePrompt->hide();
118     }
119 
120     emit queryReplaceCurrentFinished(behaviour);
121 }
122 
123 }
124