~pimvullers/pantheon-files/fix-983560

« back to all changes in this revision

Viewing changes to plugins/contractor/plugin.vala

  • Committer: xapantu
  • Date: 2012-06-01 21:34:30 UTC
  • Revision ID: xapantu@gmail.com-20120601213430-kvxei83fu396mfus
Back to the original extended-actions, add contractor plugin separately

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Authors:
 
3
 *      Lucas Baudin <xapantu@gmail.com>
 
4
 *      ammonkey <am.monkeyd@gmail.com>
 
5
 *      
 
6
 * Copyright (C) Lucas Baudin 2011 <xapantu@gmail.com>
 
7
 * 
 
8
 * Marlin is free software: you can redistribute it and/or modify it
 
9
 * under the terms of the GNU General Public License as published by the
 
10
 * Free Software Foundation, either version 3 of the License, or
 
11
 * (at your option) any later version.
 
12
 * 
 
13
 * Marlin is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
16
 * See the GNU General Public License for more details.
 
17
 * 
 
18
 * You should have received a copy of the GNU General Public License along
 
19
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
 
 
22
using Gtk;
 
23
using Gee;
 
24
 
 
25
[DBus (name = "org.elementary.contractor")]
 
26
public interface ExtendedActionsService : Object
 
27
{
 
28
    //public abstract GLib.HashTable<string,string>[] GetServicesByMime (string mime) throws IOError;
 
29
    public abstract GLib.HashTable<string,string>[] GetServicesByLocation (string strlocation, string? file_mime="")    throws IOError;
 
30
    public abstract GLib.HashTable<string,string>[] GetServicesByLocationsList (GLib.HashTable<string, string>[] locations)  throws IOError;
 
31
}
 
32
 
 
33
public class Marlin.Plugins.ExtendedActions : Marlin.Plugins.Base
 
34
{
 
35
    UIManager ui_manager;
 
36
    Gtk.Menu menu;
 
37
    GOF.File current_directory = null;
 
38
    unowned GLib.List<GOF.File> selection;
 
39
    GLib.HashTable<string,string>[] services = null;
 
40
    
 
41
    private ExtendedActionsService service_eactions;
 
42
    
 
43
    public ExtendedActions ()
 
44
    {
 
45
        try {
 
46
            service_eactions = Bus.get_proxy_sync (BusType.SESSION,
 
47
                                                   "org.elementary.contractor",
 
48
                                                   "/org/elementary/contractor");
 
49
        } catch (IOError e) {
 
50
            stderr.printf ("%s\n", e.message);
 
51
        }
 
52
    }
 
53
 
 
54
    private string get_app_display_name (GLib.HashTable<string,string> app__)
 
55
    {
 
56
        return app__.lookup ("Description");
 
57
    }
 
58
 
 
59
    private GLib.HashTable<string,string> add_location_entry (GOF.File file)
 
60
    {
 
61
        GLib.HashTable<string,string> entry;
 
62
                
 
63
        entry = new GLib.HashTable<string,string> (str_hash, str_equal);
 
64
        entry.insert ("uri", file.uri);
 
65
        var ftype = file.get_ftype ();
 
66
        if (ftype == "application/octet-stream" || ftype == null)
 
67
            entry.insert ("mimetype", "");
 
68
        else
 
69
            entry.insert ("mimetype", ftype);
 
70
 
 
71
        return entry;
 
72
    }
 
73
 
 
74
    private GLib.HashTable<string, string>[] build_hash_from_list_selection ()
 
75
    {
 
76
        GLib.HashTable<string,string>[] locations = null;
 
77
 
 
78
        foreach (GOF.File file in selection) {
 
79
            if (file != null)
 
80
                locations += add_location_entry (file);
 
81
            //message ("file %s", file.name);
 
82
        }
 
83
        if (selection == null && current_directory != null) {
 
84
            locations += add_location_entry (current_directory);
 
85
        }
 
86
 
 
87
        return locations;
 
88
    }
 
89
 
 
90
    public void action_activated ()
 
91
    {
 
92
        Gtk.MenuItem menuitem;
 
93
        GLib.HashTable<string,string> app__;
 
94
                
 
95
        menuitem = (Gtk.MenuItem) menu.get_active ();
 
96
        app__ = menuitem.get_data<GLib.HashTable<string,string>> ("app");
 
97
 
 
98
        if (app__ != null) {
 
99
            var cmd = app__.lookup ("Exec");
 
100
            //message ("test exec %s", cmd);
 
101
            try {
 
102
                GLib.Process.spawn_command_line_async (cmd);
 
103
            } catch (SpawnError e) {
 
104
                stderr.printf ("error spawn command line %s: %s", cmd, e.message);
 
105
            }
 
106
        }
 
107
    }
 
108
 
 
109
    public override void context_menu (Gtk.Widget? widget, GLib.List<GOF.File> files)
 
110
    {
 
111
        menu = widget as Gtk.Menu;
 
112
        
 
113
        selection = files;
 
114
        try {
 
115
            services = service_eactions.GetServicesByLocationsList (build_hash_from_list_selection ());
 
116
        
 
117
            uint i = 0;
 
118
            foreach(var app__ in services)
 
119
            {
 
120
                /* insert separator if we got at least 1 action */
 
121
                if (i == 0) {
 
122
                    var item = new Gtk.SeparatorMenuItem ();
 
123
                    menu.append (item);
 
124
                    item.show ();
 
125
                    plugins.menus.prepend (item);
 
126
                }
 
127
                var menuitem = new Gtk.MenuItem.with_label(get_app_display_name(app__));
 
128
                menu.append (menuitem);
 
129
                menuitem.set_data<GLib.HashTable<string,string>> ("app", app__);
 
130
                menuitem.show ();
 
131
                menuitem.activate.connect (action_activated);
 
132
                plugins.menus.prepend (menuitem);
 
133
                i++;
 
134
            }
 
135
        } catch (IOError e) {
 
136
            stderr.printf ("%s\n", e.message);
 
137
        }
 
138
    }
 
139
 
 
140
    public override void ui (Gtk.UIManager? widget)
 
141
    {
 
142
        ui_manager = widget;
 
143
        menu = (Gtk.Menu)ui_manager.get_widget("/selection");
 
144
    }
 
145
    
 
146
    public override void directory_loaded (void* user_data)
 
147
    {
 
148
        current_directory = ((Object[])user_data)[2] as GOF.File;
 
149
    }
 
150
}
 
151
 
 
152
public Marlin.Plugins.Base module_init ()
 
153
{
 
154
    return new Marlin.Plugins.ExtendedActions ();
 
155
}
 
156