urlinfo.h 3.89 KB
Newer Older
Kevin Funk's avatar
Kevin Funk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* This file is part of the KDE project
   Copyright (C) 2015 Milian Wolff <mail@milianw.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) version 3, or any
   later version accepted by the membership of KDE e.V. (or its
   successor approved by the membership of KDE e.V.), which shall
   act as a proxy defined in Section 6 of version 3 of the license.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef URLINFO_H
#define URLINFO_H

24
#include <util/texteditorhelpers.h>
Kevin Funk's avatar
Kevin Funk committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <KTextEditor/Cursor>

#include <QDataStream>
#include <QDir>
#include <QString>
#include <QUrl>

/**
 * Represents a file to be opened, consisting of its URL and the cursor to jump to.
 */
class UrlInfo
{
public:
    /**
     * Parses a file path argument and determines its line number and column and full path
     * @param path path passed on e.g. command line to parse into an URL
     */
42
    explicit UrlInfo(QString path = QString())
Kevin Funk's avatar
Kevin Funk committed
43 44 45
        : cursor(KTextEditor::Cursor::invalid())
    {
        /**
46
         * first try: just check if the path is an existing file
Kevin Funk's avatar
Kevin Funk committed
47
         */
48
        if (QFile::exists(path)) {
Kevin Funk's avatar
Kevin Funk committed
49 50
            /**
             * create absolute file path, we will e.g. pass this over dbus to other processes
51
             * and then we are done, no cursor can be detected here!
Kevin Funk's avatar
Kevin Funk committed
52 53
             */
            url = QUrl::fromLocalFile(QDir::current().absoluteFilePath(path));
54
            return;
Kevin Funk's avatar
Kevin Funk committed
55 56 57
        }

        /**
58 59
         * ok, the path as is, is no existing file, now, cut away :xx:yy stuff as cursor
         * this will make test:50 to test with line 50
Kevin Funk's avatar
Kevin Funk committed
60
         */
61 62 63
        int pathLength;
        cursor = KDevelop::KTextEditorHelpers::extractCursor(path, &pathLength);
        if (cursor.isValid()) {
Kevin Funk's avatar
Kevin Funk committed
64
            /**
65
             * cut away the line/column specification from the path
Kevin Funk's avatar
Kevin Funk committed
66
             */
67
            path.truncate(pathLength);
68
        }
Kevin Funk's avatar
Kevin Funk committed
69

70 71 72 73 74 75
        /**
         * construct url:
         *   - make relative paths absolute using the current working directory
         *   - prefer local file, if in doubt!
         */
        url = QUrl::fromUserInput(path, QDir::currentPath(), QUrl::AssumeLocalFile);
Kevin Funk's avatar
Kevin Funk committed
76

77 78 79 80 81
        /**
         * in some cases, this will fail, e.g. if you have line/column specs like test.c:10:1
         * => fallback: assume a local file and just convert it to an url
         */
        if (!url.isValid()) {
Kevin Funk's avatar
Kevin Funk committed
82
            /**
83
             * create absolute file path, we will e.g. pass this over dbus to other processes
Kevin Funk's avatar
Kevin Funk committed
84
             */
85
            url = QUrl::fromLocalFile(QDir::current().absoluteFilePath(path));
Kevin Funk's avatar
Kevin Funk committed
86 87 88
        }
    }

89 90 91 92 93
    bool isDirectory() const
    {
        return url.isLocalFile() && QFileInfo(url.toLocalFile()).isDir() && !cursor.isValid();
    }

Kevin Funk's avatar
Kevin Funk committed
94 95 96 97 98 99
    /**
     * url computed out of the passed path
     */
    QUrl url;

    /**
100
     * initial cursor position, if any found inside the path as line/column specification at the end
Kevin Funk's avatar
Kevin Funk committed
101 102 103 104
     */
    KTextEditor::Cursor cursor;
};

105 106
Q_DECLARE_TYPEINFO(UrlInfo, Q_MOVABLE_TYPE);

Kevin Funk's avatar
Kevin Funk committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
QDataStream& operator<<(QDataStream& stream, const UrlInfo& info)
{
    stream << info.url;
    stream << info.cursor.line();
    stream << info.cursor.column();
    return stream;
}

QDataStream& operator>>(QDataStream& stream, UrlInfo& info)
{
    stream >> info.url;
    int line, column;
    stream >> line;
    stream >> column;
    info.cursor.setLine(line);
    info.cursor.setColumn(column);
    return stream;
}

#endif // URLINFO_H