File indexing completed on 2023-10-03 07:53:59

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2008 Javier Goday <jgoday @ gmail.com>
0004    First Url regular expression taken from urlview tool by Michael Elkins <me@cs.hmc.edu>.
0005    Regular expression improved by FiNex.
0006    Improvements to regular expression and slotReadFile by Frantisek Ziacik
0007 
0008    This program is free software; you can redistribute it and/or
0009    modify it under the terms of the GNU General Public
0010    License as published by the Free Software Foundation; either
0011    version 2 of the License, or (at your option) any later version.
0012 */
0013 #include "linkimporter.h"
0014 
0015 #include <QDebug>
0016 #include <QDir>
0017 #include <QFile>
0018 #include <QIODevice>
0019 #include <QMap>
0020 #include <QRegExp>
0021 #include <QTextStream>
0022 
0023 #include <KIO/CopyJob>
0024 #include <KLocalizedString>
0025 
0026 // static QString REGULAR_EXPRESSION = "(((https?|ftp|gopher)://|(mailto|file|news):)[^’ <>\"]+|(www|web|w3).[-a-z0-9.]+)[^’ .,;<>\":]";
0027 //  static QString REGULAR_EXPRESSION = "((http|https|ftp|ftps)+([\\:\\w\\d:#@%/;$()~_?\\+-=\\\\.&])*)";
0028 static QString REGULAR_EXPRESSION =
0029     "(\\w+[:]//"
0030     ")?(((([\\w-]+[.]){1,}(ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|"
0031     "cl|cm|cn|co|com|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|"
0032     "gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|int|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|"
0033     "mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|"
0034     "ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|sv|st|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|"
0035     "wf|ws|ye|yt|yu|za|zm|zw|aero|biz|coop|info|museum|name|pro|travel))|([0-9]+[.][0-9]+[.][0-9]+[.][0-9]+)))([:][0-9]*)?([?/][\\w~#\\-;%?@&=/.+]*)?(?!\\w)";
0036 
0037 LinkImporter::LinkImporter(const QUrl &url, QObject *parent)
0038     : QThread(parent)
0039     , m_url(url)
0040     , m_transfers()
0041     , m_tempFile()
0042 {
0043 }
0044 
0045 LinkImporter::LinkImporter(QObject *parent)
0046     : QThread(parent)
0047     , m_url()
0048     , m_transfers()
0049     , m_tempFile()
0050 {
0051 }
0052 
0053 LinkImporter::~LinkImporter()
0054 {
0055 }
0056 
0057 void LinkImporter::checkClipboard(const QString &clipboardContent)
0058 {
0059     QRegExp rx(REGULAR_EXPRESSION);
0060 
0061     int regexPos = 0;
0062 
0063     while ((regexPos = rx.indexIn(clipboardContent, regexPos)) > -1) {
0064         QString link = rx.capturedTexts()[0];
0065 
0066         addTransfer(link);
0067 
0068         regexPos += rx.matchedLength();
0069     }
0070 }
0071 
0072 void LinkImporter::run()
0073 {
0074     if (!m_url.isLocalFile() && !m_tempFile.isEmpty()) {
0075         slotReadFile(QUrl(m_tempFile));
0076     } else {
0077         slotReadFile(m_url);
0078     }
0079 
0080     quit();
0081 }
0082 
0083 void LinkImporter::copyRemoteFile()
0084 {
0085     m_tempFile = QString("%1/%2.tmp").arg(QDir::tempPath()).arg("importer_aux");
0086 
0087     QUrl aux(m_tempFile);
0088     KIO::CopyJob *job = KIO::copy(m_url, aux, KIO::HideProgressInfo);
0089 
0090     if (!job->exec()) {
0091         Q_EMIT error(ki18n("Error trying to get %1").subs(m_url.url()));
0092     }
0093 }
0094 
0095 void LinkImporter::slotReadFile(const QUrl &url)
0096 {
0097     QRegExp rx(REGULAR_EXPRESSION);
0098     QFile file(url.toLocalFile());
0099 
0100     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
0101         return;
0102 
0103     QTextStream in(&file);
0104     quint64 size = file.size();
0105     quint64 position = 0;
0106 
0107     while (!in.atEnd()) {
0108         QString line = in.readLine();
0109         int regexPos = 0;
0110         quint64 lastPosition = position;
0111 
0112         while ((regexPos = rx.indexIn(line, regexPos)) > -1) {
0113             QString link = rx.capturedTexts()[0];
0114 
0115             addTransfer(link);
0116 
0117             regexPos += rx.matchedLength();
0118             position = lastPosition + regexPos;
0119 
0120             Q_EMIT progress(position * 100 / size);
0121         }
0122 
0123         position += line.size();
0124 
0125         Q_EMIT progress(position * 100 / size);
0126     }
0127 
0128     if (!m_url.isLocalFile()) {
0129         file.remove();
0130     }
0131 }
0132 
0133 void LinkImporter::addTransfer(QString &link)
0134 {
0135     QUrl auxUrl;
0136 
0137     if (link.contains("://")) {
0138         auxUrl = QUrl(link);
0139     } else {
0140         auxUrl = QUrl(QString("http://") + link);
0141     }
0142 
0143     if (!link.isEmpty() && auxUrl.isValid() && m_transfers.indexOf(link) < 0 && !auxUrl.scheme().isEmpty() && !auxUrl.host().isEmpty()) {
0144         m_transfers << link;
0145     }
0146 }
0147 
0148 #include "moc_linkimporter.cpp"