summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/licensor.cpp
blob: d6ed8ff9bd051149fbf48fff286221b3ae5201b5 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Authors:
 *   bulia byak <buliabyak@users.sf.net>
 *   Bryce W. Harrington <bryce@bryceharrington.org>
 *   Lauris Kaplinski <lauris@kaplinski.com>
 *   Jon Phillips <jon@rejon.org>
 *   Ralf Stephan <ralf@ark.in-berlin.de> (Gtkmm)
 *   Abhishek Sharma
 *
 * Copyright (C) 2000 - 2005 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "licensor.h"

#include <gtkmm/entry.h>
#include <gtkmm/radiobutton.h>

#include "rdf.h"
#include "inkscape.h"
#include "document-undo.h"

#include "ui/widget/entity-entry.h"
#include "ui/widget/registry.h"

namespace Inkscape {
namespace UI {
namespace Widget {

//===================================================

const struct rdf_license_t _proprietary_license = 
  {_("Proprietary"), "", nullptr};

const struct rdf_license_t _other_license = 
  {Q_("MetadataLicence|Other"), "", nullptr};

class LicenseItem : public Gtk::RadioButton {
public:
    LicenseItem (struct rdf_license_t const* license, EntityEntry* entity, Registry &wr, Gtk::RadioButtonGroup *group);
protected:
    void on_toggled() override;
    struct rdf_license_t const *_lic;
    EntityEntry                *_eep;
    Registry                   &_wr;
};

LicenseItem::LicenseItem (struct rdf_license_t const* license, EntityEntry* entity, Registry &wr, Gtk::RadioButtonGroup *group)
: Gtk::RadioButton(_(license->name)), _lic(license), _eep(entity), _wr(wr)
{
    if (group) {
        set_group (*group);
    }
}

/// \pre it is assumed that the license URI entry is a Gtk::Entry
void LicenseItem::on_toggled()
{
    if (_wr.isUpdating() || !_wr.desktop())
        return;

    _wr.setUpdating (true);
    SPDocument *doc = _wr.desktop()->getDocument();
    rdf_set_license (doc, _lic->details ? _lic : nullptr);
    if (doc->isSensitive()) {
        DocumentUndo::done(doc, _("Document license updated"), "");
    }
    _wr.setUpdating (false);
    static_cast<Gtk::Entry*>(_eep->_packable)->set_text (_lic->uri);
    _eep->on_changed();
}

//---------------------------------------------------

Licensor::Licensor()
: Gtk::Box(Gtk::ORIENTATION_VERTICAL, 4),
  _eentry (nullptr)
{
}

Licensor::~Licensor()
{
    if (_eentry) delete _eentry;
}

void Licensor::init (Registry& wr)
{
    /* add license-specific metadata entry areas */
    rdf_work_entity_t* entity = rdf_find_entity ( "license_uri" );
    _eentry = EntityEntry::create (entity, wr);

    LicenseItem *i;
    wr.setUpdating (true);
    i = Gtk::manage (new LicenseItem (&_proprietary_license, _eentry, wr, nullptr));
    Gtk::RadioButtonGroup group = i->get_group();
    add (*i);
    LicenseItem *pd = i;

    for (struct rdf_license_t * license = rdf_licenses;
             license && license->name;
             license++) {
        i = Gtk::manage (new LicenseItem (license, _eentry, wr, &group));
        add(*i);
    }
    // add Other at the end before the URI field for the confused ppl.
    LicenseItem *io = Gtk::manage (new LicenseItem (&_other_license, _eentry, wr, &group));
    add (*io);

    pd->set_active();
    wr.setUpdating (false);

    Gtk::Box *box = Gtk::manage (new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
    pack_start (*box, true, true, 0);

    box->pack_start (_eentry->_label, false, false, 5);
    box->pack_start (*_eentry->_packable, true, true, 0);

    show_all_children();
}

void Licensor::update (SPDocument *doc)
{
    /* identify the license info */
    struct rdf_license_t * license = rdf_get_license (doc);

    if (license) {
        int i;
        for (i=0; rdf_licenses[i].name; i++) 
            if (license == &rdf_licenses[i]) 
                break;
        static_cast<LicenseItem*>(get_children()[i+1])->set_active();
    }
    else {
        static_cast<LicenseItem*>(get_children()[0])->set_active();
    }
    
    /* update the URI */
    _eentry->update (doc);
}

} // namespace Dialog
} // namespace UI
} // namespace Inkscape

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :