13 #include <QApplication>
15 #include <QWebHistory>
21 #include <QDomDocument>
22 #include <QNetworkCookieJar>
48 {
"audio/mpeg3",
"mp3",
false },
49 {
"audio/x-mpeg-3",
"mp3",
false },
50 {
"audio/mpeg",
"mp2",
false },
51 {
"audio/x-mpeg",
"mp2",
false },
52 {
"audio/ogg",
"ogg",
false },
53 {
"audio/ogg",
"oga",
false },
54 {
"audio/flac",
"flac",
false },
55 {
"audio/x-ms-wma",
"wma",
false },
56 {
"audio/wav",
"wav",
false },
57 {
"audio/x-wav",
"wav",
false },
58 {
"audio/ac3",
"ac3",
false },
59 {
"audio/x-ac3",
"ac3",
false },
60 {
"audio/x-oma",
"oma",
false },
61 {
"audio/x-realaudio",
"ra",
false },
62 {
"audio/dts",
"dts",
false },
63 {
"audio/x-dts",
"dts",
false },
64 {
"audio/aac",
"aac",
false },
65 {
"audio/x-aac",
"aac",
false },
66 {
"audio/m4a",
"m4a",
false },
67 {
"audio/x-m4a",
"m4a",
false },
68 {
"video/mpeg",
"mpg",
true },
69 {
"video/mpeg",
"mpeg",
true },
70 {
"video/x-ms-wmv",
"wmv",
true },
71 {
"video/x-ms-wmv",
"avi",
true },
72 {
"application/x-troff-msvideo",
"avi",
true },
73 {
"video/avi",
"avi",
true },
74 {
"video/msvideo",
"avi",
true },
75 {
"video/x-msvideo",
"avi",
true }
79 sizeof(SupportedMimeTypes[0]);
88 reply->ignoreSslErrors();
99 networkManager = NULL;
109 LOG(VB_GENERAL, LOG_DEBUG,
"Copying DLManager's Cookie Jar");
125 m_frame(NULL), m_gotAnswer(
false)
137 QWebPage *page = view->page();
141 connect(
m_frame, SIGNAL(javaScriptWindowObjectCleared()),
this,
147 m_frame->addToJavaScriptWindowObject(QString(
"MusicPlayer"),
this);
170 MythEvent me(QString(
"MUSIC_COMMAND %1 SET_VOLUME %2")
179 MythEvent me(QString(
"MUSIC_COMMAND %1 GET_VOLUME")
188 qApp->processEvents();
200 MythEvent me(QString(
"MUSIC_COMMAND %1 PLAY_FILE '%2'")
207 MythEvent me(QString(
"MUSIC_COMMAND %1 PLAY_TRACK %2")
214 MythEvent me(QString(
"MUSIC_COMMAND %1 PLAY_URL %2")
223 MythEvent me(QString(
"MUSIC_COMMAND %1 GET_METADATA")
232 qApp->processEvents();
239 return QString(
"unknown");
247 QString message = me->
Message();
249 if (!message.startsWith(
"MUSIC_CONTROL"))
252 QStringList tokens = message.simplified().split(
" ");
254 if ((tokens.size() >= 4) && (tokens[1] ==
"ANSWER")
259 for (
int i = 4; i < tokens.size(); i++)
260 m_answer += QString(
" ") + tokens[i];
275 LOG(VB_GENERAL, LOG_DEBUG,
"Refreshing DLManager's Cookie Jar");
282 if (extension == QWebPage::ErrorPageExtension)
289 ExtensionReturn *output)
291 if (extension == QWebPage::ErrorPageExtension)
293 ErrorPageExtensionOption *erroroption;
294 erroroption = (ErrorPageExtensionOption *) option;
295 ErrorPageExtensionReturn *erroroutput;
296 erroroutput = (ErrorPageExtensionReturn *) output;
298 if (!option || !output)
301 QString
filename =
"htmls/notfound.html";
306 QFile file(QLatin1String(qPrintable(filename)));
307 bool isOpened = file.open(QIODevice::ReadOnly);
312 QString title = tr(
"Error loading page: %1").arg(erroroption->url.toString());
313 QString
html = QString(QLatin1String(file.readAll()))
315 .arg(erroroption->errorString)
316 .arg(erroroption->url.toString());
319 imageBuffer.open(QBuffer::ReadWrite);
320 QIcon icon = qApp->style()->standardIcon(QStyle::SP_MessageBoxWarning,
322 QPixmap pixmap = icon.pixmap(QSize(32, 32));
324 if (pixmap.save(&imageBuffer,
"PNG"))
326 html.replace(QLatin1String(
"IMAGE_BINARY_DATA_HERE"),
327 QString(QLatin1String(imageBuffer.buffer().toBase64())));
330 erroroutput->content = html.toUtf8();
357 connect(page(), SIGNAL(unsupportedContent(QNetworkReply *)),
360 connect(page(), SIGNAL(downloadRequested(
const QNetworkRequest &)),
363 page()->setForwardUnsupportedContent(
true);
383 function activeElement()\
386 type = document.activeElement.type;\
395 .toString().toLower();
396 bool editHasFocus = (type ==
"text" || type ==
"textarea" ||
411 bool handled =
false;
415 for (
int i = 0; i < actions.size() && !handled; i++)
417 QString
action = actions[i];
420 if (action ==
"NEXTLINK")
422 QKeyEvent tabKey(event->type(), Qt::Key_Tab,
423 event->modifiers(), QString(),
424 event->isAutoRepeat(),
event->count());
429 else if (action ==
"PREVIOUSLINK")
431 QKeyEvent shiftTabKey(event->type(), Qt::Key_Tab,
432 event->modifiers() | Qt::ShiftModifier,
434 event->isAutoRepeat(),
event->count());
435 *
event = shiftTabKey;
439 else if (action ==
"FOLLOWLINK")
441 QKeyEvent returnKey(event->type(), Qt::Key_Return,
442 event->modifiers(), QString(),
443 event->isAutoRepeat(),
event->count());
458 if (reply->error() == QNetworkReply::NoError)
462 QVariant header = reply->header(QNetworkRequest::ContentTypeHeader);
464 if (header != QVariant())
465 LOG(VB_GENERAL, LOG_ERR,
466 QString(
"MythWebView::handleUnsupportedContent - %1")
467 .arg(header.toString()));
489 QFileInfo fi(request.url().path());
490 QString basename(fi.completeBaseName());
491 QString extension = fi.suffix().toLower();
495 QString saveBaseName = basename;
500 saveBaseName = savefi.completeBaseName();
504 if (saveBaseName.isEmpty())
505 saveBaseName =
"unnamed_download";
508 if (extension.isEmpty())
511 if (!extension.isEmpty())
512 extension =
'.' + extension;
514 QString saveFilename = QString(
"%1%2%3")
526 saveFilename = QString(
"%1%2-%3%4")
529 .arg(QString::number(i++))
544 QString msg = tr(
"Enter filename to save file");
561 if (saveFilename.isEmpty())
564 openBusyPopup(QObject::tr(
"Downloading file. Please wait..."));
577 QString msg(tr(
"Downloading..."));
579 if (!message.isEmpty())
607 QString resultid = dce->
GetId();
610 if (resultid ==
"filenamedialog")
612 else if (resultid ==
"downloadmenu")
614 if (resulttext == tr(
"Play the file"))
617 QString basename(fi.baseName());
618 QString extension = fi.suffix();
623 MythEvent me(QString(
"MUSIC_COMMAND %1 PLAY_URL %2")
632 LOG(VB_GENERAL, LOG_ERR,
633 QString(
"MythWebView: Asked to play a file with "
634 "extension '%1' but don't know how")
637 else if (resulttext == tr(
"Download the file"))
641 else if (resulttext == tr(
"Download and play the file"))
651 QStringList tokens = me->
Message().split(
" ", QString::SkipEmptyParts);
653 if (tokens.isEmpty())
656 if (tokens[0] ==
"DOWNLOAD_FILE")
660 if (tokens[1] ==
"UPDATE")
664 else if (tokens[1] ==
"FINISHED")
666 int fileSize = args[2].toInt();
667 int errorCode = args[4].toInt();
672 if ((errorCode != 0) || (fileSize == 0))
677 MythEvent me(QString(
"BROWSER_DOWNLOAD_FINISHED"), args);
687 QString basename(fi.baseName());
688 QString extension = fi.suffix();
691 QString label = tr(
"What do you want to do with this file?");
709 menu->
AddButton(tr(
"Download and play the file"));
711 menu->
AddButton(tr(
"Download the file"));
721 if (!mimetype.isEmpty() && mimetype == SupportedMimeTypes[
x].
mimeType)
722 return SupportedMimeTypes[
x].extension;
734 if (!mimetype.isEmpty() &&
735 mimetype == SupportedMimeTypes[
x].
mimeType)
738 if (!extension.isEmpty() &&
739 extension.toLower() == SupportedMimeTypes[
x].
extension)
753 if (!mimetype.isEmpty() &&
754 mimetype == SupportedMimeTypes[
x].
mimeType)
757 if (!extension.isEmpty() &&
758 extension.toLower() == SupportedMimeTypes[
x].
extension)
772 QVariant header =
m_downloadReply->header(QNetworkRequest::ContentTypeHeader);
774 if (header != QVariant())
775 mimeType = header.toString();
783 return (QWebView *)
this;
830 m_parentScreen(NULL),
831 m_browser(NULL), m_browserArea(
MythRect()),
832 m_actualBrowserArea(
MythRect()), m_image(NULL),
834 m_initialized(
false), m_lastUpdateTime(QTime()),
835 m_updateInterval(0), m_zoom(1.0),
836 m_bgColor(
"White"), m_widgetUrl(QUrl()), m_userCssFile(
""),
837 m_defaultSaveDir(
GetConfDir() +
"/MythBrowser/"),
838 m_defaultSaveFilename(
""),
839 m_inputToggled(
false), m_lastMouseAction(
""),
840 m_mouseKeyCount(0), m_lastMouseActionTime(),
841 m_horizontalScrollbar(NULL), m_verticalScrollbar(NULL)
843 SetCanTakeFocus(
true);
844 m_scrollAnimation.setDuration(0);
877 m_browser->setPalette(qApp->style()->standardPalette());
881 m_browser->page()->setLinkDelegationPolicy(QWebPage::DontDelegateLinks);
888 QWebFrame* frame =
m_browser->page()->currentFrame();
889 frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
898 QWebFrame* frame =
m_browser->page()->currentFrame();
899 frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
917 QString filename =
"htmls/mythbrowser.css";
939 connect(
m_browser->page(), SIGNAL(linkHovered(
const QString &,
943 connect(
m_browser, SIGNAL(linkClicked(
const QUrl &)),
948 QObject *parentObject = parent();
957 parentObject = parentObject->parent();
961 LOG(VB_GENERAL, LOG_ERR,
962 "MythUIWebBrowser: failed to find our parent screen");
981 path +=
"/MythBrowser";
987 QWebSettings::setIconDatabasePath(path);
991 LOG(VB_GENERAL, LOG_INFO,
"MythUIWebBrowser: enabling plugins");
992 QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled,
997 LOG(VB_GENERAL, LOG_INFO,
"MythUIWebBrowser: disabling plugins");
998 QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled,
1077 LOG(VB_GENERAL, LOG_INFO,
1078 "MythUIWebBrowser: Loading css from - " + url.toString());
1080 m_browser->page()->settings()->setUserStyleSheetUrl(url);
1094 color.setAlpha(255);
1095 QPalette palette =
m_browser->page()->palette();
1096 palette.setBrush(QPalette::Window, QBrush(color));
1097 palette.setBrush(QPalette::Base, QBrush(color));
1180 if (!saveDir.isEmpty())
1188 if (!filename.isEmpty())
1213 return m_browser->history()->canGoForward();
1226 return m_browser->history()->canGoBack();
1259 return QWebSettings::iconForUrl(
m_browser->url());
1299 QWebFrame *frame =
m_browser->page()->currentFrame();
1300 return frame->evaluateJavaScript(scriptSource);
1308 QPoint startPos =
m_browser->page()->currentFrame()->scrollPosition();
1309 QPoint endPos = startPos + QPoint(dx, dy);
1327 m_browser->page()->currentFrame()->setScrollPosition(endPos);
1419 QPoint position =
m_browser->page()->currentFrame()->scrollPosition();
1423 m_browser->page()->currentFrame()->contentsSize().height() -
1433 m_browser->page()->currentFrame()->contentsSize().width() -
1450 QPainter painter(m_image);
1454 m_image->SetChanged();
1466 m_browser->page()->currentFrame()->scrollPosition())
1471 m_browser->page()->currentFrame()->setScrollPosition(scrollPosition);
1489 int alphaMod, QRect clipRegion)
1495 area.translate(xoffset, yoffset);
1505 QStringList actions;
1506 bool handled =
false;
1509 for (
int i = 0; i < actions.size() && !handled; i++)
1511 QString
action = actions[i];
1514 if (action ==
"TOGGLEINPUT")
1534 QWebFrame *frame =
m_browser->page()->currentFrame();
1537 int pos = frame->scrollPosition().y();
1546 else if (action ==
"DOWN")
1548 int pos = frame->scrollPosition().y();
1551 if (pos != maximum.height())
1558 else if (action ==
"LEFT")
1560 int pos = frame->scrollPosition().x();
1569 else if (action ==
"RIGHT")
1571 int pos = frame->scrollPosition().x();
1574 if (pos != maximum.width())
1581 else if (action ==
"PAGEUP")
1585 else if (action ==
"PAGEDOWN")
1589 else if (action ==
"ZOOMIN")
1593 else if (action ==
"ZOOMOUT")
1597 else if (action ==
"MOUSEUP" || action ==
"MOUSEDOWN" ||
1598 action ==
"MOUSELEFT" || action ==
"MOUSERIGHT" ||
1599 action ==
"MOUSELEFTBUTTON")
1603 else if (action ==
"PAGELEFT")
1607 else if (action ==
"PAGERIGHT")
1611 else if (action ==
"NEXTLINK")
1615 else if (action ==
"PREVIOUSLINK")
1619 else if (action ==
"FOLLOWLINK")
1623 else if (action ==
"HISTORYBACK")
1627 else if (action ==
"HISTORYFORWARD")
1659 if (action ==
"MOUSEUP")
1661 QPoint curPos = QCursor::pos();
1662 QCursor::setPos(curPos.x(), curPos.y() - step);
1664 else if (action ==
"MOUSELEFT")
1666 QPoint curPos = QCursor::pos();
1667 QCursor::setPos(curPos.x() - step, curPos.y());
1669 else if (action ==
"MOUSERIGHT")
1671 QPoint curPos = QCursor::pos();
1672 QCursor::setPos(curPos.x() + step, curPos.y());
1674 else if (action ==
"MOUSEDOWN")
1676 QPoint curPos = QCursor::pos();
1677 QCursor::setPos(curPos.x(), curPos.y() + step);
1679 else if (action ==
"MOUSELEFTBUTTON")
1681 QPoint curPos = QCursor::pos();
1682 QWidget *widget = QApplication::widgetAt(curPos);
1686 curPos = widget->mapFromGlobal(curPos);
1688 QMouseEvent *me =
new QMouseEvent(QEvent::MouseButtonPress, curPos,
1689 Qt::LeftButton, Qt::LeftButton,
1691 QCoreApplication::postEvent(widget, me);
1693 me =
new QMouseEvent(QEvent::MouseButtonRelease, curPos,
1694 Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
1695 QCoreApplication::postEvent(widget, me);
1719 const QString &filename, QDomElement &element,
bool showWarnings)
1721 if (element.tagName() ==
"zoom")
1726 else if (element.tagName() ==
"url")
1730 else if (element.tagName() ==
"userstylesheet")
1734 else if (element.tagName() ==
"updateinterval")
1739 else if (element.tagName() ==
"background")
1741 m_bgColor = QColor(element.attribute(
"color",
"#ffffff"));
1742 int alpha = element.attribute(
"alpha",
"255").toInt();
1745 else if (element.tagName() ==
"browserarea")
1749 else if (element.tagName() ==
"scrollduration")
1754 else if (element.tagName() ==
"acceptsfocus")
1775 LOG(VB_GENERAL, LOG_ERR,
"ERROR, bad parsing");