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
|
/* Copyright (c) MediaArea.net SARL. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license that can
* be found in the License.html file in the root of the source tree.
*/
#include "displaywindow.h"
#include "mainwindow.h"
#include "progressbar.h"
#include "WebView.h"
#include "WebPage.h"
#include <QDir>
#include <QFileDialog>
#include <QProgressBar>
#include <QRegularExpression>
#if QT_VERSION >= 0x050000
#include <QStandardPaths>
#else
#include <QDesktopServices>
#endif
#if defined(WEB_MACHINE_ENGINE)
#include <QWebChannel>
#endif
namespace MediaConch {
//***************************************************************************
// Constructor / Desructor
//***************************************************************************
DisplayWindow::DisplayWindow(MainWindow* m) : CommonWebWindow(m)
{
}
DisplayWindow::~DisplayWindow()
{
}
void DisplayWindow::display_display()
{
display_html();
}
//---------------------------------------------------------------------------
void DisplayWindow::create_web_view_finished()
{
fill_table();
}
void DisplayWindow::fill_table()
{
std::vector<QString>& displays = main_window->get_displays();
for (size_t i = 0; i < displays.size(); ++i)
{
QFileInfo file(displays[i]);
QString script;
QString fullPath = file.absoluteFilePath();
if (fullPath.startsWith(":/displays"))
script = "addSystemDisplay";
else
script = "addUserDisplay";
script += QString("('%2');")
.arg(file.completeBaseName());
((WebPage*)main_window->web_view->page())->use_javascript(script);
}
}
int DisplayWindow::add_new_file(const QString& name, const QString& filename)
{
#if QT_VERSION >= 0x050400
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
#elif QT_VERSION >= 0x050000
QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
#else
QString path = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
#endif
path += ("/displays");
QDir dir(path);
if (!dir.exists())
if (!dir.mkpath(dir.absolutePath()))
return -1;
QString display_name = name;
if (!display_name.length())
{
QFileInfo file_info(filename);
display_name = file_info.completeBaseName();
}
std::vector<QString>& displays = main_window->get_displays();
int pos = (int)displays.size();
for (int j = 0; 1; ++j)
{
QString str;
if (!j)
str = QString("%1/%2.xsl").arg(dir.absolutePath()).arg(display_name);
else
str = QString("%1/%2_%3.xsl").arg(dir.absolutePath()).arg(display_name).arg(j);
QFile info(str);
if (info.exists())
continue;
QFile::copy(filename, str);
displays.push_back(str);
break;
}
return pos;
}
int DisplayWindow::add_new_files(const QStringList& files)
{
#if QT_VERSION >= 0x050400
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
#elif QT_VERSION >= 0x050000
QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
#else
QString path = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
#endif
path += ("/displays");
QDir dir(path);
if (!dir.exists())
if (!dir.mkpath(dir.absolutePath()))
return -1;
std::vector<QString>& displays = main_window->get_displays();
int pos = (int)displays.size();
for (int i = 0; i < files.size(); ++i)
{
QFileInfo file_info(files[i]);
QString name = file_info.completeBaseName();
for (int j = 0; 1; ++j)
{
QString str;
if (!j)
str = QString("%1/%2.xsl").arg(dir.absolutePath()).arg(name);
else
str = QString("%1/%2_%3.xsl").arg(dir.absolutePath()).arg(name).arg(j);
QFile info(str);
if (info.exists())
continue;
QFile::copy(files[i], str);
displays.push_back(str);
QString script = QString("addUserDisplay('%2');").arg(file_info.completeBaseName());
((WebPage*)main_window->web_view->page())->use_javascript(script);
break;
}
if (i + 1 == files.size())
main_window->set_last_load_display_path(file_info.absolutePath().toUtf8().data());
}
return pos;
}
void DisplayWindow::export_file(const QString& name)
{
std::vector<QString>& displays = main_window->get_displays();
size_t i = 0;
for (; i < displays.size(); ++i)
{
QFileInfo f(displays[i]);
if (f.completeBaseName() == name)
break;
}
if (i == displays.size())
return;
QString suggested = QString().fromUtf8(main_window->select_correct_save_display_path().c_str());
QString file = QFileDialog::getSaveFileName(main_window, tr("Save display file"), suggested, tr("Display file (*.xsl)"));
if (!file.length())
return;
QFile f(displays[i]);
if (QFile::exists(file))
QFile::remove(file);
f.copy(file);
QDir info(QFileInfo(file).absoluteDir());
main_window->set_last_save_display_path(info.absolutePath().toUtf8().data());
}
void DisplayWindow::delete_file(const QString& name)
{
std::vector<QString>& displays = main_window->get_displays();
size_t i = 0;
for (; i < displays.size(); ++i)
{
if (displays[i].startsWith(":/displays"))
continue;
QFileInfo f(displays[i]);
if (f.completeBaseName() == name)
break;
}
if (i == displays.size())
return;
QFile file(displays[i]);
file.remove();
main_window->get_displays().erase(main_window->get_displays().begin() + i);
}
//---------------------------------------------------------------------------
void DisplayWindow::create_html(QString& html)
{
QString display;
create_html_display(display);
create_html_base(html, display);
}
//---------------------------------------------------------------------------
void DisplayWindow::create_html_display(QString& display)
{
QFile display_file(":/display.html");
display_file.open(QIODevice::ReadOnly | QIODevice::Text);
display = QString(display_file.readAll());
display_file.close();
}
//---------------------------------------------------------------------------
void DisplayWindow::create_html_base(QString& base, const QString& display)
{
set_webmachine_script_in_template(base);
change_qt_scripts_in_template(base);
change_checker_in_template(base, display);
remove_result_in_template(base);
}
//***************************************************************************
// HELPER
//***************************************************************************
//---------------------------------------------------------------------------
void DisplayWindow::change_qt_scripts_in_template(QString& html)
{
QRegularExpression reg("\\{\\{ QT_SCRIPTS \\}\\}", QRegularExpression::InvertedGreedinessOption);
QString script;
int pos = 0;
#if defined(WEB_MACHINE_KIT)
script = " <script type=\"text/javascript\" src=\"qrc:/display.js\"></script>\n";
#elif defined(WEB_MACHINE_ENGINE)
script = " <script type=\"text/javascript\" src=\"qrc:/qtwebchannel/qwebchannel.js\"></script>\n"
" <script type=\"text/javascript\" src=\"qrc:/webengine.js\"></script>\n"
" <script type=\"text/javascript\" src=\"qrc:/display.js\"></script>\n";
#endif
script += " <script type=\"text/javascript\" src=\"qrc:/utils/url.js\"></script>\n";
script += " <script type=\"text/javascript\" src=\"qrc:/menu.js\"></script>\n";
QRegularExpressionMatch match = reg.match(html, pos);
if ((pos = match.capturedStart()) != -1)
html.replace(pos, match.capturedLength(), script);
}
//---------------------------------------------------------------------------
void DisplayWindow::set_webmachine_script_in_template(QString& html)
{
QRegularExpression reg("\\{\\{[\\s]+webmachine[\\s]\\}\\}", QRegularExpression::InvertedGreedinessOption);
QString machine;
int pos = 0;
#if defined(WEB_MACHINE_KIT)
machine = "WEB_MACHINE_KIT";
#elif defined(WEB_MACHINE_ENGINE)
machine = "WEB_MACHINE_ENGINE";
#endif
QRegularExpressionMatch match = reg.match(html, pos);
if ((pos = match.capturedStart()) != -1)
html.replace(pos, match.capturedLength(), machine);
}
//---------------------------------------------------------------------------
void DisplayWindow::change_checker_in_template(QString& html, const QString& display)
{
QRegularExpression reg("\\{% block checker %\\}\\{% endblock %\\}", QRegularExpression::InvertedGreedinessOption);
html.replace(reg, display);
}
//---------------------------------------------------------------------------
void DisplayWindow::remove_result_in_template(QString& html)
{
QRegularExpression reg("\\{% block result %\\}\\{% endblock %\\}", QRegularExpression::InvertedGreedinessOption);
html.replace(reg, "");
}
}
|