LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / libreofficekit / qa / gtktiledviewer / gtv-application.cxx
blob4268d4e1962a3cf961b2821aaf97bd846721070b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <gtk/gtk.h>
12 #include "gtv-application.hxx"
13 #include "gtv-application-window.hxx"
15 #include <LibreOfficeKit/LibreOfficeKitGtk.h>
17 #include <string>
19 namespace {
21 struct GtvApplicationPrivate
23 GtvRenderingArgs* m_pRenderingArgs;
28 #if defined __clang__
29 #if __has_warning("-Wdeprecated-volatile")
30 #pragma clang diagnostic push
31 #pragma clang diagnostic ignored "-Wdeprecated-volatile"
32 #endif
33 #endif
34 G_DEFINE_TYPE_WITH_PRIVATE(GtvApplication, gtv_application, GTK_TYPE_APPLICATION);
35 #if defined __clang__
36 #if __has_warning("-Wdeprecated-volatile")
37 #pragma clang diagnostic pop
38 #endif
39 #endif
41 static GtvApplicationPrivate*
42 getPrivate(GtvApplication* app)
44 return static_cast<GtvApplicationPrivate*>(gtv_application_get_instance_private(app));
47 static void
48 gtv_application_activate(GApplication*)
50 // If this isn't provided, some GTK versions fail to run us at all.
53 static void
54 gtv_application_open(GApplication* app, GFile** file, gint nFiles, const gchar* /*hint*/)
56 for (gint i = 0; i < nFiles; i++)
58 // TODO: add some option to create a new view for existing document
59 // For now, this just opens a new document
60 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtv_application_window_new(GTK_APPLICATION(app)));
61 gtk_window_present(GTK_WINDOW(window));
63 GtvApplicationPrivate* priv = getPrivate(GTV_APPLICATION(app));
64 gtv_application_window_load_document(window, priv->m_pRenderingArgs, std::string(g_file_get_path(file[i])));
68 static void
69 gtv_application_init(GtvApplication* app)
71 static const GOptionEntry commandLineOptions[] =
73 { "version", 0, 0, G_OPTION_ARG_NONE, nullptr, "Show LOkit version", nullptr },
74 { "lo-path", 0, 0, G_OPTION_ARG_STRING, nullptr, "LO path", nullptr },
75 { "unipoll", 0, 0, G_OPTION_ARG_NONE, nullptr, "Enable unified polling loop", nullptr },
76 { "user-profile", 0, 0, G_OPTION_ARG_STRING, nullptr, "User profile to use", nullptr },
77 { "enable-tiled-annotations", 0, 0, G_OPTION_ARG_NONE, nullptr, "Whether tiled annotations should be enabled", nullptr },
78 { "background-color", 0, 0, G_OPTION_ARG_STRING, nullptr, "Background color", nullptr },
79 { "hide-page-shadow", 0, 0, G_OPTION_ARG_NONE, nullptr, "Hide page shadow", nullptr },
80 { "hide-whitespace", 0, 0, G_OPTION_ARG_NONE, nullptr, "Hide whitespace", nullptr },
81 { nullptr, 0, 0, G_OPTION_ARG_NONE, nullptr, nullptr, nullptr },
84 g_application_add_main_option_entries(G_APPLICATION(app), commandLineOptions);
86 GtvApplicationPrivate* priv = getPrivate(GTV_APPLICATION(app));
87 priv->m_pRenderingArgs = new GtvRenderingArgs();
90 static void
91 gtv_application_dispose (GObject* object)
93 GtvApplicationPrivate* priv = getPrivate(GTV_APPLICATION(object));
95 delete priv->m_pRenderingArgs;
96 priv->m_pRenderingArgs = nullptr;
98 G_OBJECT_CLASS (gtv_application_parent_class)->dispose (object);
101 static gint
102 gtv_application_handle_local_options(GApplication* app, GVariantDict* options)
104 GtvApplicationPrivate* priv = getPrivate(GTV_APPLICATION(app));
105 // This is mandatory
106 if (g_variant_dict_contains(options, "lo-path"))
108 gchar* loPath = nullptr;
109 g_variant_dict_lookup(options, "lo-path", "s", &loPath);
110 if (loPath)
112 priv->m_pRenderingArgs->m_aLoPath = std::string(loPath);
113 g_free(loPath);
116 else
118 g_print("--lo-path= is mandatory. Please provide the path to LO installation.\n");
119 return 1; // Cannot afford to continue in absence of this param
122 if (g_variant_dict_contains(options, "unipoll"))
123 priv->m_pRenderingArgs->m_bUnipoll = true;
125 if (g_variant_dict_contains(options, "version"))
127 if (!priv->m_pRenderingArgs->m_aLoPath.empty())
129 GtkWidget* pDocView = lok_doc_view_new(priv->m_pRenderingArgs->m_aLoPath.c_str(), nullptr, nullptr);
130 const gchar* versionInfo = lok_doc_view_get_version_info(LOK_DOC_VIEW(pDocView));
131 if (versionInfo)
132 g_print("LOKit version: %s", versionInfo);
135 return 1; // exit anyway
138 // Optional args
139 if (g_variant_dict_contains(options, "user-profile"))
141 gchar* userProfile = nullptr;
142 g_variant_dict_lookup(options, "user-profile", "s", &userProfile);
143 if (userProfile)
145 priv->m_pRenderingArgs->m_aUserProfile = std::string("vnd.sun.star.pathname:") + std::string(userProfile);
146 g_free(userProfile);
150 if (g_variant_dict_contains(options, "background-color"))
152 gchar* backgroundColor = nullptr;
153 g_variant_dict_lookup(options, "background-color", "s", &backgroundColor);
154 if (backgroundColor)
156 priv->m_pRenderingArgs->m_aBackgroundColor = std::string(backgroundColor);
157 g_free(backgroundColor);
161 if (g_variant_dict_contains(options, "enable-tiled-annotations"))
162 priv->m_pRenderingArgs->m_bEnableTiledAnnotations = true;
163 if (g_variant_dict_contains(options, "hide-page-shadow"))
164 priv->m_pRenderingArgs->m_bHidePageShadow = true;
165 if (g_variant_dict_contains(options, "hide-whitespace"))
166 priv->m_pRenderingArgs->m_bHideWhiteSpace = true;
168 return -1;
171 static void
172 gtv_application_class_init(GtvApplicationClass* klass)
174 G_APPLICATION_CLASS(klass)->activate = gtv_application_activate;
175 G_APPLICATION_CLASS(klass)->open = gtv_application_open;
176 G_APPLICATION_CLASS(klass)->handle_local_options = gtv_application_handle_local_options;
177 G_OBJECT_CLASS(klass)->dispose = gtv_application_dispose;
180 GtvApplication* gtv_application_new()
182 return GTV_APPLICATION(g_object_new(GTV_TYPE_APPLICATION,
183 "application-id", "org.libreoffice.gtktiledviewer",
184 "flags", G_APPLICATION_HANDLES_OPEN,
185 nullptr));
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */