summaryrefslogtreecommitdiff
path: root/libreofficekit/qa/gtktiledviewer/gtv-application.cxx
blob: 88106ae6b5835df8750cfd616dff194dfc47c84c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <gtk/gtk.h>

#include <gtv-application.hxx>
#include <gtv-application-window.hxx>

#include <string>

struct GtvApplicationPrivate
{
    GtvRenderingArgs* m_pRenderingArgs;
};

G_DEFINE_TYPE_WITH_PRIVATE(GtvApplication, gtv_application, GTK_TYPE_APPLICATION);

static GtvApplicationPrivate*
getPrivate(GtvApplication* app)
{
    return static_cast<GtvApplicationPrivate*>(gtv_application_get_instance_private(app));
}

static void
gtv_application_open(GApplication* app, GFile** file, gint /*nFiles*/, const gchar* /*hint*/)
{
    // TODO: add some option to create a new view for existing document
    // For now, this just opens a new document
    GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtv_application_window_new(GTK_APPLICATION(app)));
    gtk_window_present(GTK_WINDOW(window));

    GtvApplicationPrivate* priv = getPrivate(GTV_APPLICATION(app));
    gtv_application_window_load_document(window, priv->m_pRenderingArgs, std::string(g_file_get_path(file[0])));
}

static void
gtv_application_init(GtvApplication* app)
{
    static const GOptionEntry commandLineOptions[] =
    {
        { "version", 0, 0, G_OPTION_ARG_NONE, nullptr, "Show LOkit version", nullptr },
        { "lo-path", 0, 0, G_OPTION_ARG_STRING, nullptr, "LO path", nullptr },
        { "user-profile", 0, 0, G_OPTION_ARG_STRING, nullptr, "User profile to use", nullptr },
        { "enable-tiled-annotations", 0, 0, G_OPTION_ARG_NONE, nullptr, "Whether tiled annotations should be enabled", nullptr },
        { "background-color", 0, 0, G_OPTION_ARG_STRING, nullptr, "Background color", nullptr },
        { "hide-page-shadow", 0, 0, G_OPTION_ARG_NONE, nullptr, "Hide page shadow", nullptr },
        { "hide-whitespace", 0, 0, G_OPTION_ARG_NONE, nullptr, "Hide whitespace", nullptr }
    };

    g_application_add_main_option_entries(G_APPLICATION(app), commandLineOptions);

    GtvApplicationPrivate* priv = getPrivate(GTV_APPLICATION(app));
    priv->m_pRenderingArgs = new GtvRenderingArgs();
}

static void
gtv_application_dispose (GObject* object)
{
    GtvApplicationPrivate* priv = getPrivate(GTV_APPLICATION(object));

    delete priv->m_pRenderingArgs;
    priv->m_pRenderingArgs = nullptr;

    G_OBJECT_CLASS (gtv_application_parent_class)->dispose (object);
}

static gint
gtv_application_handle_local_options(GApplication* app, GVariantDict* options)
{
    GtvApplicationPrivate* priv = getPrivate(GTV_APPLICATION(app));
    // This is mandatory
    if (g_variant_dict_contains(options, "lo-path"))
    {
        gchar* loPath = nullptr;
        g_variant_dict_lookup(options, "lo-path", "s", &loPath);
        if (loPath)
        {
            priv->m_pRenderingArgs->m_aLoPath = std::string(loPath);
            g_free(loPath);
        }
    }
    else
    {
        g_print("--lo-path= is mandatory. Please provide the path to LO installation.\n");
        return 1; // Cannot afford to continue in absence of this param
    }

    if (g_variant_dict_contains(options, "version"))
    {
        if (!priv->m_pRenderingArgs->m_aLoPath.empty())
        {
            // FIXME: Crashes for some reason
            GtkWidget* pDocView = lok_doc_view_new(priv->m_pRenderingArgs->m_aLoPath.c_str(), nullptr, nullptr);
            const gchar* versionInfo = lok_doc_view_get_version_info(LOK_DOC_VIEW(pDocView));
            if (versionInfo)
                g_print("LOKit version: %s", versionInfo);
        }

        return 1; // exit anyway
    }

    // Optional args
    if (g_variant_dict_contains(options, "user-profile"))
    {
        gchar* userProfile = nullptr;
        g_variant_dict_lookup(options, "user-profile", "s", &userProfile);
        if (userProfile)
        {
            priv->m_pRenderingArgs->m_aUserProfile = std::string("vnd.sun.star.pathname:") + std::string(userProfile);
            g_free(userProfile);
        }
    }

    if (g_variant_dict_contains(options, "background-color"))
    {
        gchar* backgroundColor = nullptr;
        g_variant_dict_lookup(options, "background-color", "s", &backgroundColor);
        if (backgroundColor)
        {
            priv->m_pRenderingArgs->m_aBackgroundColor = std::string(backgroundColor);
            g_free(backgroundColor);
        }
    }

    if (g_variant_dict_contains(options, "enable-tiled-annotations"))
        priv->m_pRenderingArgs->m_bEnableTiledAnnotations = true;
    if (g_variant_dict_contains(options, "hide-page-shadow"))
        priv->m_pRenderingArgs->m_bHidePageShadow = true;
    if (g_variant_dict_contains(options, "hide-whitespace"))
        priv->m_pRenderingArgs->m_bHideWhiteSpace = true;

    return -1;
}

static void
gtv_application_class_init(GtvApplicationClass* klass)
{
    G_APPLICATION_CLASS(klass)->open = gtv_application_open;
    G_APPLICATION_CLASS(klass)->handle_local_options = gtv_application_handle_local_options;
    G_OBJECT_CLASS(klass)->dispose = gtv_application_dispose;
}

GtvApplication* gtv_application_new()
{
    return GTV_APPLICATION(g_object_new(GTV_TYPE_APPLICATION,
                                        "application-id", "org.libreoffice.gtktiledviewer",
                                        "flags", G_APPLICATION_HANDLES_OPEN,
                                        nullptr));
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */