~ubuntu-branches/ubuntu/wily/granite/wily

« back to all changes in this revision

Viewing changes to lib/Widgets/ContractorMenu.vala

  • Committer: Package Import Robot
  • Author(s): Sergey "Shnatsel" Davidoff
  • Date: 2014-05-22 20:22:06 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20140522202206-vqpcceou14qlxa31
Tags: 0.3.0~bzr734-1
* New upstream bzr snapshot (Closes: #722522).
* New maintainer (Closes: #748123).
* debian/control:
  - Update package versions in Build-Depends.
  - Update package descriptions.
  - Drop gir1.2-glib-2.0, libgee-dev, dh-exec from Build-Depends.
  - Update Standards Version to 3.9.5:
    + Drop obsolete DM-Upload-Allowed flag.
  - Set myself as maintainer.
  - Rename libgranite0 to libgranite2.
  - Rename libgranite0-dbg to libgranite2-dbg.
  - Rename gir1.2-granite-0.1 to gir1.2-granite-1.0.
  - Add libgranite-common binary package.
  - Remove Multi-Arch field for libgranite-dev.
  - Update Depends and Recommends for libgranite2.
  - Update Depends for libgranite-dev.
* debian/copyright:
  - Update licensing information.
  - Update copyright years.
  - Add new copyright holders.
* Update symbols files.
* Update debian/*.install files.
* Drop debian/README.source.
* Fix debian/watch.
* debian/rules:
  - Build package with --parallel.
  - Use xz for compression.
  - Add --fail-missing to dh_install.
  - Set check level for dpkg-gensymbols to 1.
  - Set dbg-package for dh_strip to libgranite2-dbg.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***
 
2
    Copyright (C) 2012-2013 Andrea Basso <andrea@elementaryos.org>
 
3
 
 
4
    This program or library is free software; you can redistribute it
 
5
    and/or modify it under the terms of the GNU Lesser General Public
 
6
    License as published by the Free Software Foundation; either
 
7
    version 3 of the License, or (at your option) any later version.
 
8
 
 
9
    This library is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
12
    Lesser General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Lesser General
 
15
    Public License along with this library; if not, write to the
 
16
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
    Boston, MA 02110-1301 USA.
 
18
***/
 
19
 
 
20
/**
 
21
 * This class provides a simple menu for managing Contractor.
 
22
 * It uses a long-obsolete and unused revision of Contractor API and will not
 
23
 * work with stable releases of Contractor.
 
24
 */
 
25
[Deprecated (since = "0.2")]
 
26
public class Granite.Widgets.ContractorMenu : Gtk.Menu {
 
27
    /**
 
28
     * The Hashtable of available contracts
 
29
     */
 
30
    HashTable<string,string>[] contracts;
 
31
    /**
 
32
     * The Hashtable of executables
 
33
     */
 
34
    Gee.HashMap <string,string> execs;
 
35
    public delegate void ContractCallback ();
 
36
    private string filepath;
 
37
    private string filemime;
 
38
    
 
39
    /**
 
40
     * Passes when contract is clicked
 
41
     */
 
42
    public signal void contract_activated (string contract_name);
 
43
    
 
44
    /**
 
45
     * Makes new Contractor Meu
 
46
     *
 
47
     * @param filename the filename of the file
 
48
     * @param mime the mime-type of the file
 
49
     */
 
50
    public ContractorMenu (string filename, string mime) {
 
51
        filepath = filename;
 
52
        filemime = mime;
 
53
        load_items (filename, mime);
 
54
    }
 
55
    
 
56
    /**
 
57
     * Adds new item to Contractor Menu
 
58
     *
 
59
     * @param name name of menu item
 
60
     * @param icon_name the desired icon for menu item
 
61
     * @param position desired position of menu item
 
62
     * @param method method to be called when menu item is clicked
 
63
     * @param use_stock tells whether to use stock for menu item
 
64
     */
 
65
    public void add_item (string name, string icon_name, int position, ContractCallback method, bool use_stock = true) {
 
66
        var item = new Gtk.ImageMenuItem ();
 
67
        item.set_always_show_image (true);
 
68
        item.set_use_stock (use_stock);
 
69
        var image = new Gtk.Image.from_icon_name (icon_name, Gtk.IconSize.MENU);
 
70
        item.set_label (name);
 
71
        item.set_image (image);
 
72
        item.activate.connect (()=> {
 
73
            contract_activated (name);
 
74
            method();
 
75
        });
 
76
        insert(item, position);
 
77
        item.show ();
 
78
    }
 
79
    
 
80
    /**
 
81
     * Deletes a group of menu items
 
82
     *
 
83
     * @param names of menu items to delete
 
84
     */
 
85
    public void name_blacklist (string[] names) {
 
86
        this.foreach ((item)=> {
 
87
            if (((Gtk.MenuItem)item).get_label () in names)
 
88
                remove (item);
 
89
        });
 
90
    }
 
91
    
 
92
    private void load_items (string filename, string mime) {
 
93
        contracts = Granite.Services.Contractor.get_contract (filename, mime);
 
94
        execs = new Gee.HashMap<string,string> ();
 
95
        
 
96
        for (int i=0;i<contracts.length;i++) {
 
97
            execs[contracts[i].lookup ("Name")] = contracts[i].lookup ("Exec");
 
98
            
 
99
            var item = new Gtk.ImageMenuItem ();
 
100
            item.set_always_show_image (true);
 
101
            var image = new Gtk.Image.from_icon_name (contracts[i].lookup ("IconName"), Gtk.IconSize.MENU);
 
102
            item.set_label (contracts[i].lookup ("Name"));
 
103
            item.set_image (image);
 
104
            item.activate.connect ( ()=> {
 
105
                try {
 
106
                        Process.spawn_command_line_async (execs.get(item.get_label ()));
 
107
                    } catch (Error e) {
 
108
                        error (e.message);
 
109
                    }
 
110
            });
 
111
            append (item);
 
112
            item.show_all ();
 
113
        }
 
114
    }
 
115
    
 
116
    /**
 
117
     * Updates Contractor menu items
 
118
     *
 
119
     * @param filename the filename of the file
 
120
     * @param mime the mime-type of the file
 
121
     */
 
122
    public void update (string? filename, string? mime) {
 
123
        this.foreach ((w) => {remove (w);});
 
124
        
 
125
        string fn = "";
 
126
        string mm = "";
 
127
        
 
128
        if (filename != null) {
 
129
            fn = filename;
 
130
            filepath = filename;
 
131
        } else {
 
132
            fn = filepath;
 
133
        }
 
134
        
 
135
        if (mime != null) {
 
136
            mm = mime;
 
137
            filemime = mime;
 
138
        } else {
 
139
            mm = filemime;
 
140
        }
 
141
    
 
142
        load_items (fn, mm);
 
143
    }
 
144
}