~adamreichold/qpdfview/trunk

« back to all changes in this revision

Viewing changes to sources/main.cpp

  • Committer: Adam Reichold
  • Date: 2012-07-01 17:37:17 UTC
  • Revision ID: adamreichold@myopera.com-20120701173717-rjw063ohuse3wcri
manual merge of future branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
Copyright 2012 Adam Reichold
4
 
 
5
 
This file is part of qpdfview.
6
 
 
7
 
qpdfview is free software: you can redistribute it and/or modify
8
 
it under the terms of the GNU General Public License as published by
9
 
the Free Software Foundation, either version 2 of the License, or
10
 
(at your option) any later version.
11
 
 
12
 
qpdfview is distributed in the hope that it will be useful,
13
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
GNU General Public License for more details.
16
 
 
17
 
You should have received a copy of the GNU General Public License
18
 
along with qpdfview.  If not, see <http://www.gnu.org/licenses/>.
19
 
 
20
 
*/
21
 
 
22
 
#include "mainwindow.h"
23
 
 
24
 
struct Link
25
 
{
26
 
    QString filePath;
27
 
    int page;
28
 
    qreal top;
29
 
 
30
 
    Link() : filePath(), page(1), top(0.0) {}
31
 
    Link(const QString& filePath, int page = 1, qreal top = 0.0) : filePath(filePath), page(page), top(top) {}
32
 
 
33
 
};
34
 
 
35
 
int main(int argc, char** argv)
36
 
{
37
 
    QApplication a(argc, argv);
38
 
    QApplication::setOrganizationDomain("local.qpdfview");
39
 
    QApplication::setOrganizationName("qpdfview");
40
 
    QApplication::setApplicationName("qpdfview");
41
 
    QApplication::setApplicationVersion("0.3.1");
42
 
 
43
 
#ifdef DATA_INSTALL_PATH
44
 
 
45
 
    QApplication::setWindowIcon(QIcon(QString("%1/qpdfview.svg").arg(DATA_INSTALL_PATH)));
46
 
 
47
 
    QTranslator t;
48
 
    if(t.load(QString("%1/qpdfview_").arg(DATA_INSTALL_PATH) + QLocale::system().name()))
49
 
    {
50
 
        a.installTranslator(&t);
51
 
    }
52
 
 
53
 
#else
54
 
 
55
 
    QApplication::setWindowIcon(QIcon(":/icons/qpdfview.svg"));
56
 
 
57
 
    QTranslator t;
58
 
    if(t.load(QString(":/translations/qpdfview_") + QLocale::system().name()))
59
 
    {
60
 
        a.installTranslator(&t);
61
 
    }
62
 
 
63
 
#endif
64
 
 
65
 
    // command-line arguments
66
 
 
67
 
    QStringList arguments = QApplication::arguments();
68
 
    QList< Link > links;
69
 
 
70
 
    if(!arguments.isEmpty())
71
 
    {
72
 
        arguments.removeFirst();
73
 
    }
74
 
 
75
 
    foreach(QString argument, arguments)
76
 
    {
77
 
        if(!argument.startsWith("--"))
78
 
        {
79
 
            QStringList fields = argument.split('#');
80
 
            Link link;
81
 
 
82
 
            link.filePath = QFileInfo(fields.at(0)).absoluteFilePath();
83
 
 
84
 
            if(fields.count() > 1)
85
 
            {
86
 
                link.page = fields.at(1).toInt();
87
 
                link.page = qMax(link.page, 1);
88
 
            }
89
 
            if(fields.count() > 2)
90
 
            {
91
 
                link.top = fields.at(2).toFloat();
92
 
                link.top = qMax(link.top, static_cast< qreal >(0.0));
93
 
                link.top = qMin(link.top, static_cast< qreal >(1.0));
94
 
            }
95
 
 
96
 
            links.append(link);
97
 
        }
98
 
    }
99
 
 
100
 
#ifdef WITH_DBUS
101
 
 
102
 
    MainWindow* mainWindow = 0;
103
 
 
104
 
    if(arguments.contains("--unique"))
105
 
    {
106
 
        QDBusInterface interface("local.qpdfview", "/MainWindow", "local.qpdfview.MainWindow", QDBusConnection::sessionBus());
107
 
 
108
 
        if(interface.isValid())
109
 
        {
110
 
            foreach(Link link, links)
111
 
            {
112
 
                interface.call("refresh", link.filePath, link.page, link.top);
113
 
            }
114
 
 
115
 
            return 0;
116
 
        }
117
 
        else
118
 
        {
119
 
            mainWindow = new MainWindow();
120
 
 
121
 
            new MainWindowAdaptor(mainWindow);
122
 
 
123
 
            if(!QDBusConnection::sessionBus().registerService("local.qpdfview"))
124
 
            {
125
 
                qDebug() << QDBusConnection::sessionBus().lastError().message();
126
 
 
127
 
                delete mainWindow;
128
 
                return 1;
129
 
            }
130
 
 
131
 
            if(!QDBusConnection::sessionBus().registerObject("/MainWindow", mainWindow))
132
 
            {
133
 
                qDebug() << QDBusConnection::sessionBus().lastError().message();
134
 
 
135
 
                delete mainWindow;
136
 
                return 1;
137
 
            }
138
 
        }
139
 
    }
140
 
    else
141
 
    {
142
 
        mainWindow = new MainWindow();
143
 
    }
144
 
 
145
 
#else
146
 
 
147
 
    MainWindow* mainWindow = new MainWindow();
148
 
 
149
 
#endif // WITH_DBUS
150
 
 
151
 
    foreach(Link link, links)
152
 
    {
153
 
        mainWindow->openInNewTab(link.filePath, link.page, link.top);
154
 
    }
155
 
 
156
 
    mainWindow->show();
157
 
    mainWindow->setAttribute(Qt::WA_DeleteOnClose);
158
 
 
159
 
    return a.exec();
160
 
}