finance.vala 5.22 KB
Newer Older
1
/*
2
 * This file is part of GNOME LaTeX.
3
4
5
 *
 * Copyright © 2015 - Sébastien Wilmet
 *
6
 * GNOME LaTeX is free software: you can redistribute it and/or modify
7
8
9
10
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
11
 * GNOME LaTeX is distributed in the hope that it will be useful,
12
13
14
15
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
17
 * along with GNOME LaTeX.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
21
22
23
24
25
26
27
28
 */

using Gtk;

namespace Finance
{
    public void show_dialog (Window parent_window, bool startup)
    {
        if (startup && ! should_show_dialog_on_startup ())
            return;

29
30
        string title;
        if (startup)
31
            title = "Welcome in GNOME LaTeX!";
32
        else
33
            title = "GNOME LaTeX Finance";
34
35

        Dialog dialog = new Dialog.with_buttons (title,
36
37
38
            parent_window,
            DialogFlags.DESTROY_WITH_PARENT,
            "_Close", ResponseType.CLOSE,
39
            "GNOME LaTeX _Fundraising", ResponseType.ACCEPT,
40
41
42
43
44
45
46
47
48
49
            null);

        dialog.set_resizable (false);
        dialog.set_default_response (ResponseType.ACCEPT);

        unowned Box content_area = dialog.get_content_area ();
        content_area.set_spacing (6);
        content_area.margin = 12;
        content_area.margin_top = 6;

50
51
        Image image = new Image.from_icon_name ("gnome-latex", IconSize.DIALOG);
        image.set_pixel_size (128);
52
53
54
55
56
57
        content_area.add (image);

        Label label = new Label (null);
        label.set_markup ("<big>Did you know...</big>");
        content_area.add (label);

58
        label = new Label ("...that you can support GNOME LaTeX financially?");
59
60
61
        content_area.add (label);

        label = new Label (null);
62
        label.set_markup ("GNOME LaTeX is a Free/<i>Libre</i> LaTeX editor and comes" +
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
            " free of charge. But if you appreciate this software, you are encouraged" +
            " to make a donation to help its future development.");
        label.max_width_chars = 60;
        label.set_line_wrap (true);
        label.xalign = 0;
        content_area.add (label);

        if (startup)
        {
            label = new Label ("You can see again this information at any time by going to the Help menu.");
            label.max_width_chars = 60;
            label.set_line_wrap (true);
            label.xalign = 0;
            content_area.add (label);
        }

        label = new Label ("Thanks!");
        label.xalign = 0;
        content_area.add (label);

        CheckButton remind_later_checkbutton =
            new CheckButton.with_mnemonic ("_Remind me later (in one month)");

        remind_later_checkbutton.set_active (false);
        remind_later_checkbutton.margin_top = 12;
        remind_later_checkbutton.margin_bottom = 6;

        content_area.add (remind_later_checkbutton);
        content_area.show_all ();

        while (true)
        {
            int response = dialog.run ();

            if (response == ResponseType.ACCEPT)
            {
99
                open_donate_page (dialog);
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
158
                continue;
            }

            GLib.Settings settings =
                new GLib.Settings ("org.gnome.latexila.state.dialogs.finance");

            settings.set_boolean ("remind-later", remind_later_checkbutton.get_active ());
            break;
        }

        dialog.destroy ();
        save_date ();
    }

    private bool should_show_dialog_on_startup ()
    {
        GLib.Settings settings =
            new GLib.Settings ("org.gnome.latexila.state.dialogs.finance");

        string date = settings.get_string ("last-shown-date");

        if (date == "")
            return true;

        if (settings.get_boolean ("remind-later"))
        {
            string[] ymd = date.split ("-");
            if (ymd.length != 3)
                return false;

            int year = int.parse (ymd[0]);
            int month = int.parse (ymd[1]);
            int day = int.parse (ymd[2]);

            DateTime last_time = new DateTime.utc (year, month, day, 0, 0, 0);
            DateTime cur_time = new DateTime.now_utc ();

            // Remind one month later.
            DateTime time_limit = last_time.add_months (1);

            if (time_limit.compare (cur_time) <= 0)
                return true;
        }

        return false;
    }

    private void save_date ()
    {
        GLib.Settings settings =
            new GLib.Settings ("org.gnome.latexila.state.dialogs.finance");

        DateTime time = new DateTime.now_utc ();
        string date = "%d-%d-%d".printf (time.get_year (), time.get_month (),
            time.get_day_of_month ());

        settings.set_string ("last-shown-date", date);
    }

159
    private void open_donate_page (Widget widget)
160
161
162
    {
        try
        {
163
            string uri = "https://liberapay.com/LaTeXila/";
164
            Latexila.utils_show_uri (widget, uri, Gdk.CURRENT_TIME);
165
166
167
168
169
170
171
        }
        catch (Error e)
        {
            warning ("Impossible to open the donate page: %s", e.message);
        }
    }
}