forked from zyantific/IDASkins
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCore.cpp
200 lines (171 loc) · 6.26 KB
/
Core.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
/**
* The MIT License (MIT)
*
* Copyright (c) 2014 athre0z
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "Core.hpp"
#include "Config.hpp"
#include "ThemeSelector.hpp"
#include "Settings.hpp"
#include "IdaFontConfig.hpp"
#include <QDir>
#include <QApplication>
#include <QMessageBox>
#ifdef BUILD_IDA
#include <idp.hpp>
#include <diskio.hpp>
#include <kernwin.hpp>
#include <loader.hpp>
#endif // BUILD_IDA
// ========================================================================= //
// [Core] //
// ========================================================================= //
Core::Core()
: m_lastUiActionWasFontChange(false)
{
// If first start with plugin, ask for theme.
Settings settings;
QVariant firstStartVar = settings.value(Settings::kFirstStart, true);
bool firstStart = true;
if (firstStartVar.canConvert<bool>())
firstStart = firstStartVar.toBool();
else
settings.remove(Settings::kFirstStart);
if (firstStartVar.toBool())
{
auto pressedButton = QMessageBox::information(qApp->activeWindow(),
PLUGIN_NAME": First start",
PLUGIN_NAME" detected that this is you first IDA startup with this plugin "
"installed. Do you wish to select a theme now?",
QMessageBox::Yes | QMessageBox::No);
if (pressedButton == QMessageBox::Yes)
openThemeSelectionDialog();
settings.setValue(Settings::kFirstStart, false);
}
applyStylesheetFromSettings();
#ifdef BUILD_IDA
hook_to_notification_point(HT_UI, &uiHook, this);
#endif // BUILD_IDA
}
Core::~Core()
{
#ifdef BUILD_IDA
unhook_from_notification_point(HT_UI, &uiHook, this);
#endif BUILD_IDA
}
void Core::runPlugin()
{
openThemeSelectionDialog();
applyStylesheetFromSettings();
}
bool Core::applyStylesheet(QDir &themeDir)
{
QString themeDirPath = themeDir.absolutePath();
QFile stylesheet(themeDirPath + "/stylesheet.qss");
if (!stylesheet.open(QFile::ReadOnly))
{
msg("[" PLUGIN_NAME "] Unable to load stylesheet file.\n");
return false;
}
QString data = stylesheet.readAll();
preprocessStylesheet(data, themeDirPath);
qApp->setStyleSheet(data);
request_refresh(IWID_ALL);
msg("[" PLUGIN_NAME "] Skin file successfully applied!\n");
return true;
}
bool Core::applyStylesheetFromSettings()
{
QDir activeThemeDir;
if (Utils::getCurrentThemeDir(activeThemeDir))
return applyStylesheet(activeThemeDir);
return false;
}
void Core::preprocessStylesheet(QString &qss, const QString &themeDirPath)
{
qss.replace("<IDADIR>", idadir(nullptr));
qss.replace("<SKINDIR>", themeDirPath);
auto applyFontReplacements = [&](const QString &keyword, IdaFontConfig::FontType type)
{
IdaFontConfig settings(type);
QString prefix = "<" + keyword + "_FONT_";
qss.replace(prefix + "FAMILY>", settings.family());
qss.replace(prefix + "STYLE>", settings.italic() ? " italic" : "");
qss.replace(prefix + "WEIGHT>", settings.bold() ? " bold" : "");
qss.replace(prefix + "SIZE>", QString::number(settings.size()) + "pt");
};
applyFontReplacements("DISASSEMBLY", IdaFontConfig::FONT_DISASSEMBLY);
applyFontReplacements("HEXVIEW", IdaFontConfig::FONT_HEXVIEW);
applyFontReplacements("DEBUG_REGISTERS", IdaFontConfig::FONT_DEBUG_REGISTERS);
applyFontReplacements("TEXT_INPUT", IdaFontConfig::FONT_TEXT_INPUT);
applyFontReplacements("OUTPUT_WINDOW", IdaFontConfig::FONT_OUTPUT_WINDOW);
}
int Core::uiHook(void *userData, int notificationCode, va_list va)
{
#ifdef BUILD_IDA
auto thiz = static_cast<Core*>(userData);
Q_ASSERT(thiz);
switch (notificationCode)
{
case ui_preprocess:
{
const char *action = va_arg(va, const char*);
if (::qstrcmp(action, "SetFont") == 0)
thiz->m_lastUiActionWasFontChange = true;
} break;
case ui_postprocess:
{
if (thiz->m_lastUiActionWasFontChange)
{
QMessageBox::warning(qApp->activeWindow(), "IDASkins",
"Please note that altering the font settings when IDASkins is loaded "
"may cause strange effects on font rendering. It is recommended to "
"restart IDA after making font-related changes in the settings to avoid "
"instability.");
thiz->m_lastUiActionWasFontChange = false;
}
} break;
}
#else
Q_UNUSED(userData);
Q_UNUSED(notificationCode);
Q_UNUSED(va);
#endif // BUILD_IDA
return 0;
}
void Core::openThemeSelectionDialog()
{
ThemeSelector selector(qApp->activeWindow());
connect(&selector, SIGNAL(accepted()), SLOT(onThemeSelectionAccepted()));
selector.exec();
}
void Core::onThemeSelectionAccepted()
{
auto selector = dynamic_cast<ThemeSelector*>(sender());
Q_ASSERT(selector);
// New theme selected? Save to settings.
if (selector->selectedThemeDir())
{
Settings().setValue(Settings::kSelectedThemeDir,
selector->selectedThemeDir()->dirName());
}
}
// ========================================================================= //