2011-09-23 22:06:21 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
2011-09-23 22:06:21 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-09-18 15:35:44 +02:00
|
|
|
#include "globalfunctions.h"
|
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QByteArray>
|
|
|
|
#include <QPixmap>
|
2012-05-27 14:05:28 +02:00
|
|
|
#include <QPainter>
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QBuffer>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QDesktopWidget>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QIcon>
|
|
|
|
|
2011-09-18 15:35:44 +02:00
|
|
|
QByteArray qz_pixmapToByteArray(const QPixmap &pix)
|
|
|
|
{
|
|
|
|
QByteArray bytes;
|
|
|
|
QBuffer buffer(&bytes);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (pix.save(&buffer, "PNG")) {
|
2011-09-18 15:35:44 +02:00
|
|
|
return buffer.buffer().toBase64();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-18 15:35:44 +02:00
|
|
|
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
2011-11-05 11:51:46 +01:00
|
|
|
QPixmap qz_pixmapFromByteArray(const QByteArray &data)
|
|
|
|
{
|
|
|
|
QPixmap image;
|
|
|
|
QByteArray bArray = QByteArray::fromBase64(data);
|
|
|
|
image.loadFromData(bArray);
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2012-07-08 21:17:10 +02:00
|
|
|
QString qz_readAllFileContents(const QString &filename)
|
2011-09-18 15:35:44 +02:00
|
|
|
{
|
|
|
|
QFile file(filename);
|
2012-07-08 00:15:03 +02:00
|
|
|
if (file.open(QFile::ReadOnly)) {
|
2012-07-08 21:17:10 +02:00
|
|
|
QString a = QString::fromUtf8(file.readAll());
|
2012-07-08 00:15:03 +02:00
|
|
|
file.close();
|
|
|
|
return a;
|
|
|
|
}
|
2011-09-18 15:35:44 +02:00
|
|
|
|
2012-07-08 00:15:03 +02:00
|
|
|
return QByteArray();
|
2011-09-18 15:35:44 +02:00
|
|
|
}
|
2011-09-21 14:20:49 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void qz_centerWidgetOnScreen(QWidget* w)
|
2011-09-21 14:20:49 +02:00
|
|
|
{
|
|
|
|
const QRect screen = QApplication::desktop()->screenGeometry();
|
|
|
|
const QRect &size = w->geometry();
|
2011-11-06 17:01:23 +01:00
|
|
|
w->move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2);
|
2011-09-21 14:20:49 +02:00
|
|
|
}
|
2011-10-07 15:37:49 +02:00
|
|
|
|
2011-10-26 19:23:50 +02:00
|
|
|
// Very, very, very simplified QDialog::adjustPosition from qdialog.cpp
|
|
|
|
void qz_centerWidgetToParent(QWidget* w, QWidget* parent)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!parent || !w) {
|
2011-10-26 19:23:50 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-26 19:23:50 +02:00
|
|
|
|
|
|
|
QPoint p;
|
|
|
|
parent = parent->window();
|
2011-11-06 17:01:23 +01:00
|
|
|
QPoint pp = parent->mapToGlobal(QPoint(0, 0));
|
|
|
|
p = QPoint(pp.x() + parent->width() / 2, pp.y() + parent->height() / 2);
|
|
|
|
p = QPoint(p.x() - w->width() / 2, p.y() - w->height() / 2 - 20);
|
2011-10-26 19:23:50 +02:00
|
|
|
|
|
|
|
w->move(p);
|
|
|
|
}
|
|
|
|
|
2012-01-26 18:23:35 +01:00
|
|
|
bool qz_removeFile(const QString &fullFileName)
|
|
|
|
{
|
|
|
|
QFile f(fullFileName);
|
|
|
|
if (f.exists()) {
|
|
|
|
return f.remove();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void qz_removeDir(const QString &d)
|
|
|
|
{
|
|
|
|
QDir dir(d);
|
|
|
|
if (dir.exists()) {
|
|
|
|
const QFileInfoList list = dir.entryInfoList();
|
|
|
|
QFileInfo fi;
|
|
|
|
for (int l = 0; l < list.size(); l++) {
|
|
|
|
fi = list.at(l);
|
2012-09-05 19:57:36 +02:00
|
|
|
if (fi.isDir() && fi.fileName() != QLatin1String(".") && fi.fileName() != QLatin1String("..")) {
|
2012-01-26 18:23:35 +01:00
|
|
|
qz_removeDir(fi.absoluteFilePath());
|
|
|
|
}
|
|
|
|
else if (fi.isFile()) {
|
|
|
|
qz_removeFile(fi.absoluteFilePath());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
dir.rmdir(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-09 14:51:25 +02:00
|
|
|
QString qz_samePartOfStrings(const QString &one, const QString &other)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
int maxSize = qMin(one.size(), other.size());
|
|
|
|
while (one.at(i) == other.at(i)) {
|
|
|
|
i++;
|
2011-11-06 17:01:23 +01:00
|
|
|
if (i == maxSize) {
|
2011-10-09 14:51:25 +02:00
|
|
|
break;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-09 14:51:25 +02:00
|
|
|
}
|
|
|
|
return one.left(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl qz_makeRelativeUrl(const QUrl &baseUrl, const QUrl &rUrl)
|
|
|
|
{
|
|
|
|
QString baseUrlPath = baseUrl.path();
|
|
|
|
QString rUrlPath = rUrl.path();
|
|
|
|
|
|
|
|
QString samePart = qz_samePartOfStrings(baseUrlPath, rUrlPath);
|
|
|
|
|
|
|
|
QUrl returnUrl;
|
|
|
|
if (samePart.isEmpty()) {
|
|
|
|
returnUrl = rUrl;
|
|
|
|
}
|
2012-09-04 12:42:45 +02:00
|
|
|
else if (samePart == QLatin1String("/")) {
|
2011-10-09 14:51:25 +02:00
|
|
|
returnUrl = QUrl(rUrl.path());
|
|
|
|
}
|
|
|
|
else {
|
2012-09-04 12:42:45 +02:00
|
|
|
samePart = samePart.left(samePart.lastIndexOf(QLatin1Char('/')) + 1);
|
|
|
|
int slashCount = samePart.count(QLatin1Char('/')) + 1;
|
|
|
|
if (samePart.startsWith(QLatin1Char('/'))) {
|
2011-10-09 14:51:25 +02:00
|
|
|
slashCount--;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-09-04 12:42:45 +02:00
|
|
|
if (samePart.endsWith(QLatin1Char('/'))) {
|
2011-10-09 14:51:25 +02:00
|
|
|
slashCount--;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-09 14:51:25 +02:00
|
|
|
|
|
|
|
rUrlPath.remove(samePart);
|
2011-10-17 09:57:07 +02:00
|
|
|
rUrlPath.prepend(QString("..""/").repeated(slashCount));
|
2011-10-09 14:51:25 +02:00
|
|
|
returnUrl = QUrl(rUrlPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnUrl;
|
|
|
|
}
|
|
|
|
|
2012-02-21 21:28:19 +01:00
|
|
|
QString qz_urlEncodeQueryString(const QUrl &url)
|
|
|
|
{
|
|
|
|
QString returnString = url.toString(QUrl::RemoveQuery | QUrl::RemoveFragment);
|
2012-02-22 19:12:22 +01:00
|
|
|
|
|
|
|
if (url.hasQuery()) {
|
2012-09-04 12:42:45 +02:00
|
|
|
returnString += QLatin1Char('?') + url.encodedQuery();
|
2012-02-22 19:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (url.hasFragment()) {
|
2012-09-04 12:42:45 +02:00
|
|
|
returnString += QLatin1Char('#') + url.encodedFragment();
|
2012-02-22 19:12:22 +01:00
|
|
|
}
|
2012-02-21 21:28:19 +01:00
|
|
|
|
2012-09-04 12:42:45 +02:00
|
|
|
returnString.replace(QLatin1Char(' '), QLatin1String("%20"));
|
2012-03-15 19:35:37 +01:00
|
|
|
|
2012-02-21 21:28:19 +01:00
|
|
|
return returnString;
|
|
|
|
}
|
|
|
|
|
2012-07-08 00:15:03 +02:00
|
|
|
QString qz_ensureUniqueFilename(const QString &name, const QString &appendFormat)
|
2011-10-12 22:28:41 +02:00
|
|
|
{
|
2012-07-08 00:15:03 +02:00
|
|
|
if (!QFile::exists(name)) {
|
|
|
|
return name;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-12 22:28:41 +02:00
|
|
|
|
2012-07-08 00:15:03 +02:00
|
|
|
QString tmpFileName = name;
|
2011-10-12 22:28:41 +02:00
|
|
|
int i = 1;
|
|
|
|
while (QFile::exists(tmpFileName)) {
|
2012-07-08 00:15:03 +02:00
|
|
|
tmpFileName = name;
|
2012-09-04 12:42:45 +02:00
|
|
|
int index = tmpFileName.lastIndexOf(QLatin1Char('.'));
|
2011-10-12 22:28:41 +02:00
|
|
|
|
2012-07-08 00:15:03 +02:00
|
|
|
QString appendString = appendFormat.arg(i);
|
2011-10-12 22:28:41 +02:00
|
|
|
if (index == -1) {
|
2012-07-08 00:15:03 +02:00
|
|
|
tmpFileName.append(appendString);
|
2011-11-05 11:51:46 +01:00
|
|
|
}
|
|
|
|
else {
|
2012-07-08 00:15:03 +02:00
|
|
|
tmpFileName = tmpFileName.left(index) + appendString + tmpFileName.mid(index);
|
2011-10-12 22:28:41 +02:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return tmpFileName;
|
|
|
|
}
|
|
|
|
|
2011-11-05 11:51:46 +01:00
|
|
|
QString qz_getFileNameFromUrl(const QUrl &url)
|
|
|
|
{
|
|
|
|
QString fileName = url.toString(QUrl::RemoveFragment | QUrl::RemoveQuery | QUrl::RemoveScheme | QUrl::RemovePort);
|
2012-09-04 12:42:45 +02:00
|
|
|
if (fileName.indexOf(QLatin1Char('/')) != -1) {
|
|
|
|
int pos = fileName.lastIndexOf(QLatin1Char('/'));
|
2011-11-05 11:51:46 +01:00
|
|
|
fileName = fileName.mid(pos);
|
2012-09-04 12:42:45 +02:00
|
|
|
fileName.remove(QLatin1Char('/'));
|
2011-11-05 11:51:46 +01:00
|
|
|
}
|
2012-03-07 12:19:54 +01:00
|
|
|
|
2012-03-23 13:58:31 +01:00
|
|
|
fileName = qz_filterCharsFromFilename(fileName);
|
|
|
|
|
|
|
|
if (fileName.isEmpty()) {
|
2012-09-04 12:42:45 +02:00
|
|
|
fileName = qz_filterCharsFromFilename(url.host().replace(QLatin1Char('.'), QLatin1Char('-')));
|
2012-03-23 13:58:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return fileName;
|
2011-11-05 11:51:46 +01:00
|
|
|
}
|
|
|
|
|
2011-11-28 19:17:48 +01:00
|
|
|
QString qz_filterCharsFromFilename(const QString &name)
|
|
|
|
{
|
|
|
|
QString value = name;
|
2012-06-24 23:46:32 +02:00
|
|
|
|
2012-09-04 12:42:45 +02:00
|
|
|
value.replace(QLatin1Char('/'), QLatin1Char('-'));
|
|
|
|
value.remove(QLatin1Char('\\'));
|
|
|
|
value.remove(QLatin1Char(':'));
|
|
|
|
value.remove(QLatin1Char('*'));
|
|
|
|
value.remove(QLatin1Char('?'));
|
|
|
|
value.remove(QLatin1Char('"'));
|
|
|
|
value.remove(QLatin1Char('<'));
|
|
|
|
value.remove(QLatin1Char('>'));
|
|
|
|
value.remove(QLatin1Char('|'));
|
2011-11-28 19:17:48 +01:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-05-13 17:01:35 +02:00
|
|
|
QString qz_alignTextToWidth(const QString &string, const QString &text, const QFontMetrics &metrics, int width)
|
|
|
|
{
|
|
|
|
int pos = 0;
|
|
|
|
QString returnString;
|
|
|
|
|
|
|
|
while (pos <= string.size()) {
|
|
|
|
QString part = string.mid(pos);
|
|
|
|
QString elidedLine = metrics.elidedText(part, Qt::ElideRight, width);
|
|
|
|
|
|
|
|
if (elidedLine.isEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elidedLine.size() != part.size()) {
|
2012-07-01 18:11:43 +02:00
|
|
|
elidedLine = elidedLine.left(elidedLine.size() - 3);
|
2012-05-13 17:01:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!returnString.isEmpty()) {
|
|
|
|
returnString += text;
|
|
|
|
}
|
|
|
|
|
|
|
|
returnString += elidedLine;
|
|
|
|
pos += elidedLine.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnString;
|
|
|
|
}
|
|
|
|
|
2012-08-23 15:40:10 +02:00
|
|
|
QString qz_fileSizeToString(qint64 size)
|
|
|
|
{
|
|
|
|
if (size < 0) {
|
|
|
|
return QObject::tr("Unknown size");
|
|
|
|
}
|
|
|
|
|
|
|
|
double _size = (double)size;
|
|
|
|
_size /= 1024; //kB
|
|
|
|
if (_size < 1000) {
|
|
|
|
return QString::number(_size > 1 ? _size : 1, 'f', 0) + " KB";
|
|
|
|
}
|
|
|
|
|
|
|
|
_size /= 1024; //MB
|
|
|
|
if (_size < 1000) {
|
|
|
|
return QString::number(_size, 'f', 1) + " MB";
|
|
|
|
}
|
|
|
|
|
|
|
|
_size /= 1024; //GB
|
|
|
|
return QString::number(_size, 'f', 2) + " GB";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-27 14:05:28 +02:00
|
|
|
QPixmap qz_createPixmapForSite(const QIcon &icon, const QString &title, const QString &url)
|
|
|
|
{
|
|
|
|
const QFontMetrics fontMetrics = QApplication::fontMetrics();
|
|
|
|
const int padding = 4;
|
|
|
|
const int maxWidth = fontMetrics.width(title.length() > url.length() ? title : url) + 3 * padding + 16;
|
|
|
|
|
|
|
|
const int width = qMin(maxWidth, 150);
|
|
|
|
const int height = fontMetrics.height() * 2 + fontMetrics.leading() + 2 * padding;
|
|
|
|
|
|
|
|
QPixmap pixmap(width, height);
|
|
|
|
QPainter painter(&pixmap);
|
|
|
|
|
|
|
|
// Draw background
|
|
|
|
QPen pen(Qt::black);
|
|
|
|
pen.setWidth(1);
|
|
|
|
painter.setPen(pen);
|
|
|
|
|
|
|
|
painter.fillRect(QRect(0, 0, width, height), Qt::white);
|
|
|
|
painter.drawRect(0, 0, width - 1, height - 1);
|
|
|
|
|
|
|
|
// Draw icon
|
|
|
|
QRect iconRect(0, 0, 16 + 2 * padding, height);
|
|
|
|
icon.paint(&painter, iconRect);
|
|
|
|
|
|
|
|
// Draw title
|
|
|
|
QRect titleRect(iconRect.width(), padding, width - padding - iconRect.width(), fontMetrics.height());
|
|
|
|
painter.drawText(titleRect, fontMetrics.elidedText(title, Qt::ElideRight, titleRect.width()));
|
|
|
|
|
|
|
|
// Draw url
|
|
|
|
QRect urlRect(titleRect.x(), titleRect.bottom() + fontMetrics.leading(), titleRect.width(), titleRect.height());
|
|
|
|
painter.setPen(QApplication::palette().color(QPalette::Link));
|
|
|
|
painter.drawText(urlRect, fontMetrics.elidedText(url, Qt::ElideRight, urlRect.width()));
|
|
|
|
|
|
|
|
return pixmap;
|
|
|
|
}
|
|
|
|
|
2012-08-19 19:47:28 +02:00
|
|
|
QString QT_QUPZILLA_EXPORT qz_applyDirectionToPage(QString &pageContents)
|
|
|
|
{
|
2012-09-04 12:42:45 +02:00
|
|
|
QString direction = QLatin1String("ltr");
|
|
|
|
QString right_str = QLatin1String("right");
|
|
|
|
QString left_str = QLatin1String("left");
|
2012-08-19 19:47:28 +02:00
|
|
|
|
|
|
|
if (QApplication::isRightToLeft()) {
|
2012-09-04 12:42:45 +02:00
|
|
|
direction = QLatin1String("rtl");
|
|
|
|
right_str = QLatin1String("left");
|
|
|
|
left_str = QLatin1String("right");
|
2012-08-19 19:47:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-04 12:42:45 +02:00
|
|
|
pageContents.replace(QLatin1String("%DIRECTION%"), direction);
|
|
|
|
pageContents.replace(QLatin1String("%RIGHT_STR%"), right_str);
|
|
|
|
pageContents.replace(QLatin1String("%LEFT_STR%"), left_str);
|
2012-08-19 19:47:28 +02:00
|
|
|
|
|
|
|
return pageContents;
|
|
|
|
}
|
|
|
|
|
2011-10-07 15:37:49 +02:00
|
|
|
QString qz_buildSystem()
|
|
|
|
{
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
return "Linux";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_BSD4
|
|
|
|
return "BSD 4.4";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_BSDI
|
|
|
|
return "BSD/OS";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_FREEBSD
|
|
|
|
return "FreeBSD";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_HPUX
|
|
|
|
return "HP-UX";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_HURD
|
|
|
|
return "GNU Hurd";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_LYNX
|
|
|
|
return "LynxOS";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
return "MAC OS";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_NETBSD
|
|
|
|
return "NetBSD";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_OS2
|
|
|
|
return "OS/2";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_OPENBSD
|
|
|
|
return "OpenBSD";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_OSF
|
|
|
|
return "HP Tru64 UNIX";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_SOLARIS
|
|
|
|
return "Sun Solaris";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_UNIXWARE
|
|
|
|
return "UnixWare 7 / Open UNIX 8";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
return "Windows";
|
|
|
|
#endif
|
2012-02-23 19:18:08 +01:00
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
return "Unix";
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_HAIKU
|
|
|
|
return "Haiku";
|
|
|
|
#endif
|
2011-10-07 15:37:49 +02:00
|
|
|
}
|