Commit 632785c1 authored by Camilo Higuita's avatar Camilo Higuita

collect image preview for links locally #1

parent e545bf02
......@@ -78,7 +78,9 @@ HEADERS += \
src/utils/htmlparser.h
INCLUDEPATH += \
src/utils/
src/utils/ \
src/
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
......@@ -23,7 +23,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QVariantMap>
#include <QUuid>
#include <QDateTime>
#include "linker.h"
DBActions::DBActions(QObject *parent) : DB(parent)
{
......@@ -136,11 +136,13 @@ QVariantList DBActions::getNotes()
bool DBActions::insertLink(const QString &link, const QString &title, const QString &preview, const QString &color, const QString &tags)
{
auto image_path = OWL::saveImage(Linker::getUrl(preview), OWL::LinksPath+QUuid::createUuid().toString());
QVariantMap link_map =
{
{OWL::KEYMAP[OWL::KEY::LINK], link},
{OWL::KEYMAP[OWL::KEY::TITLE], title},
{OWL::KEYMAP[OWL::KEY::PREVIEW], preview},
{OWL::KEYMAP[OWL::KEY::PREVIEW], image_path},
{OWL::KEYMAP[OWL::KEY::COLOR], color},
{OWL::KEYMAP[OWL::KEY::ADD_DATE], QDateTime::currentDateTime()}
};
......
#include "linker.h"
#include <QEventLoop>
#include "owl.h"
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QEventLoop>
Linker::Linker(QObject *parent) : QObject(parent)
{
}
QByteArray Linker::getUrl(const QString &url)
{
QUrl mURL(url);
QNetworkAccessManager manager;
QNetworkRequest request (mURL);
QNetworkReply *reply = manager.get(request);
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), &loop,
SLOT(quit()));
loop.exec();
if(reply->error())
{
qDebug() << reply->error();
return QByteArray();
}
if(reply->bytesAvailable())
{
auto data = reply->readAll();
reply->deleteLater();
return data;
}
return QByteArray();
}
/* extract needs to extract from a url the title, the body and a preview image*/
void Linker::extract(const QString &url)
{
auto data = this->getUrl(url);
auto data = getUrl(url);
auto titles = query(data, HtmlTag::TITLE);
QStringList imgs ;
......@@ -40,37 +73,7 @@ void Linker::extract(const QString &url)
emit previewReady(link_data);
}
QByteArray Linker::getUrl(const QString &url)
{
QUrl mURL(url);
QNetworkAccessManager manager;
QNetworkRequest request (mURL);
QNetworkReply *reply = manager.get(request);
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), &loop,
SLOT(quit()));
loop.exec();
if(reply->error())
{
qDebug() << reply->error();
return QByteArray();
}
if(reply->bytesAvailable())
{
auto data = reply->readAll();
reply->deleteLater();
return data;
}
return QByteArray();
}
QStringList Linker::query(const QByteArray &array, const HtmlTag &tag, const QString &attribute)
{
......
......@@ -2,11 +2,10 @@
#define LINKER_H
#include <QObject>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include "qgumbodocument.h"
#include "qgumbonode.h"
#include <QVariantMap>
typedef QVariantMap LINK;
......@@ -15,11 +14,11 @@ class Linker : public QObject
Q_OBJECT
public:
explicit Linker(QObject *parent = nullptr);
static QByteArray getUrl(const QString &url);
Q_INVOKABLE void extract(const QString &url);
private:
QByteArray getUrl(const QString &url);
QStringList query(const QByteArray &array, const HtmlTag &tag, const QString &attribute = QString());
signals:
......
......@@ -13,143 +13,165 @@
#include <QJsonDocument>
#include <QJsonObject>
namespace OWL
{
Q_NAMESPACE
enum class W : uint8_t
{
TITLE,
BODY,
IMAGE,
VIDEO,
LINK,
TAG,
AUTHOR,
DATE,
NOTE,
TAGS,
ADD_DATE,
COLOR
};
static const QMap<W, QString> SLANG =
{
{W::TITLE, "title"},
{W::BODY, "body"},
{W::IMAGE, "image"},
{W::VIDEO, "video"},
{W::LINK, "link"},
{W::TAG, "tag"},
{W::AUTHOR, "author"},
{W::DATE, "date"},
{W::NOTE, "note"},
{W::TAGS, "tags"},
{W::ADD_DATE, "addDate"},
{W::COLOR, "color"}
};
enum class TABLE : uint8_t
{
NOTES,
NOTES_TAGS,
TAGS,
BOOKS,
PAGES,
BOOKS_PAGES,
LINKS,
LINKS_TAGS,
PAGES_TAGS,
NONE
};
static const QMap<TABLE,QString> TABLEMAP =
{
{TABLE::NOTES,"notes"},
{TABLE::NOTES_TAGS,"notes_tags"},
{TABLE::TAGS,"tags"},
{TABLE::BOOKS,"books"},
{TABLE::PAGES,"pages"},
{TABLE::BOOKS_PAGES,"books_pages"},
{TABLE::LINKS,"links"},
{TABLE::LINKS_TAGS,"links_tags"},
{TABLE::PAGES_TAGS,"pages_tags"},
{TABLE::LINKS_TAGS,"links_tags"}
};
enum class KEY :uint8_t
{
URL,
UPDATED,
ID,
TITLE,
BODY,
FAV,
COLOR,
ADD_DATE,
TAG,
PREVIEW,
IMAGE,
LINK,
NONE
};
typedef QMap<OWL::KEY, QString> DB;
typedef QList<DB> DB_LIST;
static const DB KEYMAP =
{
{KEY::ID, "id"},
{KEY::BODY, "body"},
{KEY::UPDATED, "updated"},
{KEY::TITLE, "title"},
{KEY::URL, "url"},
{KEY::FAV, "fav"},
{KEY::COLOR, "color"},
{KEY::ADD_DATE, "addDate"},
{KEY::TAG, "tag"},
{KEY::PREVIEW, "preview"},
{KEY::IMAGE, "image"},
{KEY::LINK, "link"}
};
const QString CollectionDBPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/buho/";
const QString NotesPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/buho/notes/";
const QString BooksPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/buho/books/";
const QString LinksPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/buho/links/";
const QString App = "Buho";
const QString version = "1.0";
const QString DBName = "collection.db";
inline bool fileExists(const QString &url)
{
QFileInfo path(url);
if (path.exists()) return true;
else return false;
}
inline void saveJson(QJsonDocument document, QString fileName)
{
QFile jsonFile(fileName);
jsonFile.open(QFile::WriteOnly);
jsonFile.write(document.toJson());
}
inline QVariantMap openJson(const QString &url)
namespace OWL
{
QString val;
QFile file;
file.setFileName(url);
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();
QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
QJsonObject obj = d.object();
return obj.toVariantMap();
}
Q_NAMESPACE
enum class W : uint8_t
{
TITLE,
BODY,
IMAGE,
VIDEO,
LINK,
TAG,
AUTHOR,
DATE,
NOTE,
TAGS,
ADD_DATE,
COLOR
};
static const QMap<W, QString> SLANG =
{
{W::TITLE, "title"},
{W::BODY, "body"},
{W::IMAGE, "image"},
{W::VIDEO, "video"},
{W::LINK, "link"},
{W::TAG, "tag"},
{W::AUTHOR, "author"},
{W::DATE, "date"},
{W::NOTE, "note"},
{W::TAGS, "tags"},
{W::ADD_DATE, "addDate"},
{W::COLOR, "color"}
};
enum class TABLE : uint8_t
{
NOTES,
NOTES_TAGS,
TAGS,
BOOKS,
PAGES,
BOOKS_PAGES,
LINKS,
LINKS_TAGS,
PAGES_TAGS,
NONE
};
static const QMap<TABLE,QString> TABLEMAP =
{
{TABLE::NOTES,"notes"},
{TABLE::NOTES_TAGS,"notes_tags"},
{TABLE::TAGS,"tags"},
{TABLE::BOOKS,"books"},
{TABLE::PAGES,"pages"},
{TABLE::BOOKS_PAGES,"books_pages"},
{TABLE::LINKS,"links"},
{TABLE::LINKS_TAGS,"links_tags"},
{TABLE::PAGES_TAGS,"pages_tags"},
{TABLE::LINKS_TAGS,"links_tags"}
};
enum class KEY :uint8_t
{
URL,
UPDATED,
ID,
TITLE,
BODY,
FAV,
COLOR,
ADD_DATE,
TAG,
PREVIEW,
IMAGE,
LINK,
NONE
};
typedef QMap<OWL::KEY, QString> DB;
typedef QList<DB> DB_LIST;
static const DB KEYMAP =
{
{KEY::ID, "id"},
{KEY::BODY, "body"},
{KEY::UPDATED, "updated"},
{KEY::TITLE, "title"},
{KEY::URL, "url"},
{KEY::FAV, "fav"},
{KEY::COLOR, "color"},
{KEY::ADD_DATE, "addDate"},
{KEY::TAG, "tag"},
{KEY::PREVIEW, "preview"},
{KEY::IMAGE, "image"},
{KEY::LINK, "link"}
};
const QString CollectionDBPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/buho/";
const QString NotesPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/buho/notes/";
const QString BooksPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/buho/books/";
const QString LinksPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/buho/links/";
const QString App = "Buho";
const QString version = "1.0";
const QString DBName = "collection.db";
inline bool fileExists(const QString &url)
{
QFileInfo path(url);
if (path.exists()) return true;
else return false;
}
inline void saveJson(QJsonDocument document, QString fileName)
{
QFile jsonFile(fileName);
jsonFile.open(QFile::WriteOnly);
jsonFile.write(document.toJson());
}
inline QVariantMap openJson(const QString &url)
{
QString val;
QFile file;
file.setFileName(url);
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();
QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
QJsonObject obj = d.object();
return obj.toVariantMap();
}
inline QString saveImage(QByteArray array, const QString &path)
{
if(!array.isNull()&&!array.isEmpty())
{
QImage img;
img.loadFromData(array);
QString name = path;
name.replace("/", "-");
name.replace("&", "-");
QString format = "JPEG";
if (img.save(path+".jpg", format.toLatin1(), 100))
return path;
else qDebug() << "couldn't save artwork";
}else qDebug()<<"array is empty";
return QString();
}
}
......
......@@ -122,7 +122,7 @@ ItemDelegate
sourceSize.height: height
sourceSize.width: width
fillMode: Image.PreserveAspectCrop
source: model.preview || ""
source: "file://"+encodeURIComponent( model.preview ) || ""
layer.enabled: img.visible
layer.effect: OpacityMask
......
......@@ -147,6 +147,7 @@ Popup
font.bold: true
font.pointSize: fontSizes.large
color: fgColor
Layout.alignment: Qt.AlignCenter
background: Rectangle
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment