-
Notifications
You must be signed in to change notification settings - Fork 1
/
cheatshplugin.cpp
231 lines (201 loc) · 8.85 KB
/
cheatshplugin.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
#include "cheatshplugin.h"
#include "cheatshconstants.h"
#include "cheatfilter.h"
#include "querymanager.h"
#include "cheatoutputplane.h"
#include "optionspage.h"
#include "updatechecker.h"
#include "cheatsh_global.h"
#include <coreplugin/icore.h>
#include <coreplugin/icontext.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/editorwindow.h>
//#include <coreplugin/editormanager/editorarea.h>
#include <coreplugin/editormanager/ieditor.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditor.h>
#include <extensionsystem/pluginmanager.h>
#include <cppeditor/cppeditorconstants.h>
#include <coreplugin/locator/locatormanager.h>
#include <coreplugin/locator/locatorconstants.h>
#include <coreplugin/locator/locator.h>
#include <coreplugin/locator/commandlocator.h>
#include <utils/parameteraction.h>
#include <QAction>
#include <QMessageBox>
#include <QMetaObject>
#include <QMainWindow>
#include <QMenu>
#include <QListWidget>
#include <QString>
#include <QObject>
#include <QDebug>
#include <QSslSocket>
#include <QTextCursor>
#include <QTranslator>
#include <QDirIterator> //
using namespace Core;
namespace CheatSh {
namespace Internal {
CheatShPlugin::CheatShPlugin()
{
// QDirIterator it(":", QDirIterator::Subdirectories);
// while (it.hasNext()) {
// qDebug() << it.next();
// }
// Create your members
const QLocale locale(ICore::userInterfaceLanguage());
auto *translator = new QTranslator(this);
if (translator->load(locale, QLatin1String("cheatsh"), QLatin1String("_"), QLatin1String(":/i18n"))) {
// if (translator->load(locale, QLatin1String("cheatsh"), QLatin1String("_"))) {
QCoreApplication::installTranslator(translator);
// qDebug() << "translation loaded" << locale;
} else {
// qDebug() << "CheatShPlugin translation failed" << locale;
delete translator;
}
}
CheatShPlugin::~CheatShPlugin()
{
// Unregister objects from the plugin manager's object pool
// Delete members
delete action_cheat_sh_;
delete out_plane_;
delete options_page_;
delete cheat_sh_;
}
bool CheatShPlugin::initialize(const QStringList &arguments, QString *errorString)
{
// Register objects in the plugin manager's object pool
// Load settings
// Add actions to menus
// Connect to other plugins' signals
// In the initialize function, a plugin can be sure that the plugins it
// depends on have initialized their members.
Q_UNUSED(arguments)
Q_UNUSED(errorString)
settings_.load(Core::ICore::settings());
out_plane_ = new CheatOutputPlane(&settings_, this);
cheat_sh_ = new QueryManager(&settings_, this);
cheat_filter_ = std::make_unique<CheatFilter>(&settings_);
options_page_ = new OptionsPage(settings_, this);
connect(cheat_sh_, &QueryManager::found, out_plane_, &CheatOutputPlane::displayANSI);
connect(cheat_sh_, &QueryManager::errorOccurred, out_plane_, &CheatOutputPlane::displayHtml);
connect(cheat_sh_, &QueryManager::pasteReady, [this](const QString& paste_val){
paste_value_ = paste_val;
});
connect(cheat_sh_, &QueryManager::nextAvaliable, out_plane_, &CheatOutputPlane::setNextAvaliable);
connect(cheat_sh_, &QueryManager::prevAvaliable, out_plane_, &CheatOutputPlane::setPrevAvaliable);
connect(cheat_sh_, &QueryManager::indexChanged, out_plane_, &CheatOutputPlane::setIndex);
connect(out_plane_, &CheatOutputPlane::requestedNext, cheat_sh_, &QueryManager::requestNext);
connect(out_plane_, &CheatOutputPlane::requestedPrev, cheat_sh_, &QueryManager::requestPrev);
connect(cheat_filter_.get(), &CheatFilter::query, cheat_sh_, &QueryManager::search);
connect(options_page_, &OptionsPage::settingsChanged, this, &CheatShPlugin::changeSettings);
createMenus();
update_checker_ = new UpdateChecker(&settings_, this);
connect(update_checker_, &UpdateChecker::updateAvaliable, out_plane_, &CheatOutputPlane::displayHtml);
connect(Core::ICore::instance(), &Core::ICore::saveSettingsRequested,
this, [this](){ settings_.save(Core::ICore::settings()); });
return true;
}
void CheatShPlugin::extensionsInitialized()
{
// Retrieve objects from the plugin manager's object pool
// In the extensionsInitialized function, a plugin can be sure that all
// plugins that depend on it are completely initialized.
if(QSslSocket::supportsSsl()) {
update_checker_->checkUpdatesIfPossible();
} else {
QString ssl_error_string = tr("<b>Cheat.sh plugin can't use SSL.</b>"
"<br />HTTPS connections and update checks will be unavaliable."
"<br />Please <a href=\"%1\">install OpenSSL</a>")
.arg("https://slproweb.com/products/Win32OpenSSL.html");
QMetaObject::invokeMethod(out_plane_, "displayHtml",
Qt::QueuedConnection, Q_ARG(QString, ssl_error_string));
}
}
ExtensionSystem::IPlugin::ShutdownFlag CheatShPlugin::aboutToShutdown()
{
// Save settings
// Disconnect from signals that are not needed during shutdown
// Hide UI (if you add UI that is not in the main window directly)
cheat_filter_->disconnect();
action_cheat_sh_->disconnect();
action_paste_->disconnect();
options_page_->disconnect();
cheat_sh_->disconnect();
update_checker_->disconnect();
out_plane_->hide();
return SynchronousShutdown;
}
void CheatShPlugin::changeSettings(const Settings& settings)
{
settings.save(Core::ICore::settings());
settings_ = settings;
}
void CheatShPlugin::createMenus()
{
// create actions:
Core::Context textContext( TextEditor::Constants::C_TEXTEDITOR );
action_cheat_sh_ = new QAction( tr( "Search cheat.sh" ), this );
Core::Command* cheatShCommand = Core::ActionManager::registerAction(action_cheat_sh_, Constants::SEARCH_ACTION_ID, textContext);
cheatShCommand->setAttribute(Core::Command::CA_UpdateText); //Зачем?
cheatShCommand->setDefaultKeySequence(QKeySequence(tr("Alt+Shift+C")));
action_paste_ = new QAction( tr("Paste result"), this );
Core::Command* pasteCommand = Core::ActionManager::registerAction(action_paste_, Constants::PASTE_ACTION_ID, textContext);
pasteCommand->setAttribute(Core::Command::CA_UpdateText); //Зачем?
pasteCommand->setDefaultKeySequence(QKeySequence(tr("Alt+Shift+V")));
// connect actions
connect(action_cheat_sh_, &QAction::triggered, [this](){ // search action
auto* editorWidget =
qobject_cast<TextEditor::TextEditorWidget*> (
Core::EditorManager::currentEditor()->widget()
);
QString selected_text = editorWidget->selectedText();
if(selected_text.isEmpty()) {
QTextCursor text_cursor = editorWidget->textCursor();
text_cursor.select(QTextCursor::WordUnderCursor);
selected_text = text_cursor.selectedText();
}
// qDebug("selection: '%s'", qPrintable(selected_text));
cheat_sh_->search(selected_text);
});
connect(action_paste_, &QAction::triggered, [this](){ // paste action
if(!paste_value_.isEmpty()) {
auto* editorWidget =
qobject_cast<TextEditor::TextEditorWidget*> (
Core::EditorManager::currentEditor()->widget()
);
editorWidget->insertPlainText(paste_value_);
}
});
// add actions to tools menu
Core::ActionContainer* menu = Core::ActionManager::createMenu( Constants::MENU_ID );
menu->menu()->setTitle( tr( "CheatSh" ) );
menu->addAction(cheatShCommand);
menu->addAction(pasteCommand);
Core::ActionManager::actionContainer( Core::Constants::M_TOOLS )->addMenu( menu );
// add actions to context menu
Core::ActionContainer* editorcontextMenu =
Core::ActionManager::createMenu( TextEditor::Constants::M_STANDARDCONTEXTMENU );
Core::ActionContainer* contextMenu =
//Core::ActionManager::createMenu( TextEditor::Constants::M_STANDARDCONTEXTMENU );
Core::ActionManager::createMenu( Constants::CONTEXT_MENU_ID );
contextMenu->menu()->setTitle( tr( "CheatSh" ) );
contextMenu->addSeparator();
contextMenu->addAction(cheatShCommand);
contextMenu->addAction(pasteCommand);
contextMenu->addSeparator();
//editorcontextMenu->addMenu(contextMenu); //
Utils::Id group_id(Constants::CONTEXT_MENU_ID);
editorcontextMenu->appendGroup(group_id);
editorcontextMenu->addSeparator(group_id);
editorcontextMenu->addAction(cheatShCommand, group_id);
editorcontextMenu->addAction(pasteCommand, group_id);
}
} // namespace Internal
} // namespace CheatSh