pix.cpp 3.98 KB
Newer Older
1
#include "pix.h"
Camilo Higuita's avatar
Camilo Higuita committed
2
#include "db/fileloader.h"
3 4
#include <QFileSystemWatcher>
#include <QTimer>
Camilo Higuita's avatar
Camilo Higuita committed
5 6 7 8 9 10 11
#include <QApplication>
#include <QDesktopWidget>
#include <QDirIterator>
#include <QtQml>
#include <QPalette>
#include <QWidget>
#include <QColor>
12

Camilo Higuita's avatar
Camilo Higuita committed
13 14 15 16 17
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
#include "kde/notify.h"
#include "kde/kde.h"
#endif

18 19
using namespace PIX;

Camilo Higuita's avatar
Camilo Higuita committed
20
Pix::Pix(QObject *parent) : DBActions(parent)
21 22 23
{
    qDebug() << "Getting settings info from: " << PIX::SettingPath;

Camilo Higuita's avatar
Camilo Higuita committed
24
    //    if(!PIX::fileExists(notifyDir+"/Pix.notifyrc"))
25 26
    //    {
    //        qDebug()<<"The Knotify file does not exists, going to create it";
Camilo Higuita's avatar
Camilo Higuita committed
27
    //        QFile knotify(":Data/data/Pix.notifyrc");
28

Camilo Higuita's avatar
Camilo Higuita committed
29
    //        if(knotify.copy(notifyDir+"/Pix.notifyrc"))
30 31 32
    //            qDebug()<<"the knotify file got copied";
    //    }

Camilo Higuita's avatar
Camilo Higuita committed
33

34 35 36
    this->fileLoader = new FileLoader;


37
    connect(this->fileLoader, &FileLoader::finished,[this](int size)
38
    {
39 40 41
            emit refreshViews({{PIX::TABLEMAP[TABLE::ALBUMS], true},
                               {PIX::TABLEMAP[TABLE::TAGS], true},
                               {PIX::TABLEMAP[TABLE::IMAGES], true}});
42 43 44 45 46 47 48 49 50
    });

}

Pix::~Pix()
{
    delete this->fileLoader;
}

51 52 53 54 55
void Pix::refreshCollection()
{
    this->populateDB({PIX::PicturesPath, PIX::DownloadsPath});
}

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
QVariantList Pix::getList(const QStringList &urls)
{
    QVariantList mapList;

    for(auto url : urls)
    {
        auto queryTxt = QString("SELECT * FROM %1 WHERE %2 = \"%3\"").arg(TABLEMAP[TABLE::IMAGES],
                KEYMAP[KEY::URL], url);

        mapList << this->get(queryTxt);
    }

    return mapList;
}


bool Pix::run(const QString &query)
{
Camilo Higuita's avatar
Camilo Higuita committed
74
    return this->execQuery(query);
75 76
}

77
void Pix::populateDB(const QStringList &paths)
78 79
{
    qDebug() << "Function Name: " << Q_FUNC_INFO
80 81
             << "new path for database action: " << paths;
    QStringList newPaths;
82

83
    for(auto path : paths)
84 85 86
    {
        if(path.startsWith("file://"))

87 88 89 90 91
            newPaths << path.replace("file://", "");
        else
            newPaths<<path;

        qDebug()<<"paths to scan"<<newPaths;
92 93 94
    }

    fileLoader->requestPath(newPaths);
95 96
}

Camilo Higuita's avatar
Camilo Higuita committed
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
QString Pix::pixColor()
{
    return "#03A9F4";
}


int Pix::screenGeometry(QString &side)
{
    side = side.toLower();
    auto geo = QApplication::desktop()->screenGeometry();

    if(side == "width")
        return geo.width();
    else if(side == "height")
        return geo.height();
    else return 0;
}

int Pix::cursorPos(QString &axis)
{
    axis = axis.toLower();
    auto pos = QCursor::pos();
    if(axis == "x")
        return pos.x();
    else if(axis == "y")
        return pos.y();
    else return 0;
}

QString Pix::homeDir()
{
    return PIX::PicturesPath;
}

QVariantList Pix::getDirs(const QString &pathUrl)
{
    auto path = pathUrl;
    if(path.startsWith("file://"))
        path.replace("file://", "");
    qDebug()<<"DIRECTRORY"<<path;
    QVariantList paths;

    if (QFileInfo(path).isDir())
    {
        QDirIterator it(path, QDir::Dirs, QDirIterator::NoIteratorFlags);
        while (it.hasNext())
        {
            auto url = it.next();
            auto name = QDir(url).dirName();
            qDebug()<<name<<url;
            QVariantMap map = { {"url", url }, {"name", name} };
            paths << map;
        }
    }

    return paths;
}

QVariantMap Pix::getParentDir(const QString &path)
{
    auto dir = QDir(path);
    dir.cdUp();
    auto dirPath = dir.absolutePath();

    if(dir.isReadable() && !dir.isRoot() && dir.exists())
        return {{"url", dirPath}, {"name", dir.dirName()}};
    else
        return {{"url", path}, {"name", QFileInfo(path).dir().dirName()}};

}

Camilo Higuita's avatar
Camilo Higuita committed
168 169 170 171 172 173 174 175 176
QVariantList Pix::openWith(const QString &url)
{
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
    return KDE::mimeApps(url);
#elif defined (Q_OS_ANDROID)
    return QVariantList();
#endif
}

177 178 179 180 181 182 183 184
void Pix::runApplication(const QString &exec, const QString &url)
{
    qDebug()<<"RUN:"<<exec<<url;
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
    return KDE::openWithApp(exec, url);
#endif
}

Camilo Higuita's avatar
Camilo Higuita committed
185

186