File: helper-desktop.vala

package info (click to toggle)
vala-panel-appmenu 0.7.3+dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,368 kB
  • sloc: ansic: 2,609; xml: 83; makefile: 58; sh: 7
file content (203 lines) | stat: -rw-r--r-- 9,318 bytes parent folder | download | duplicates (2)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * vala-panel-appmenu
 * Copyright (C) 2018 Konstantin Pugin <ria.freelander@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * 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 Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using GLib;
using Gtk;

namespace Appmenu
{
    internal class DesktopHelper: Helper
    {
        private GLib.Menu files_menu;
        private GLib.Menu documents_menu;
        private GLib.Menu pictures_menu;
        private GLib.Menu music_menu;
        private GLib.Menu videos_menu;
        private unowned MenuWidget widget;
        private const GLib.ActionEntry[] menu_entries =
        {
            {"launch-id", activate_menu_id, "s", null, null},
            {"launch-uri", activate_menu_uri, "s", null, null},
            {"launch-command", activate_menu_command, "s", null, null},
            {"launch-type", activate_menu_launch_type, "s", null, null},
            {"desktop-settings", activate_desktop, null, null, null},
            {"control-center", activate_control, null, null, null},
            {"populate-files", null, "b", "false", state_populate_files},
            {"populate-docs", null, "b", "false", state_populate_docs},
            {"populate-music", null, "b", "false", state_populate_music},
            {"populate-picts", null, "b", "false", state_populate_picts},
            {"populate-video", null, "b", "false", state_populate_video}
        };
        public DesktopHelper(MenuWidget w)
        {
    //            if(window != null)
    //                base(w,null,window);
            this.widget = w;
            var group = new SimpleActionGroup();
            group.add_action_entries(menu_entries,this);
            w.insert_action_group("menu",group);
            var builder = new Builder.from_resource("/org/vala-panel/appmenu/desktop-menus.ui");
            builder.set_translation_domain(Config.GETTEXT_PACKAGE);
            unowned GLib.Menu gmenu = builder.get_object("appmenu-desktop") as GLib.Menu;
            var menu = new GLib.Menu();
            string? name = GLib.dgettext(Config.GETTEXT_PACKAGE,"_Desktop");
            menu.append_submenu(name,gmenu);
            menu.freeze();
            widget.set_appmenu(menu);
            GLib.Menu append_menu = new GLib.Menu();
            widget.set_menubar(append_menu);
            files_menu = builder.get_object("files") as GLib.Menu;
            documents_menu = builder.get_object("docs") as GLib.Menu;
            music_menu = builder.get_object("music") as GLib.Menu;
            pictures_menu = builder.get_object("picts") as GLib.Menu;
            videos_menu = builder.get_object("video") as GLib.Menu;
            gmenu = builder.get_object("menubar") as GLib.Menu;
            append_menu.append_section(null,gmenu);
            append_menu.freeze();
        }
        internal void activate_menu_id(SimpleAction action, Variant? param)
        {
            Launcher.activate_menu_launch_id(action,param,this);
        }
        internal void activate_menu_uri(SimpleAction action, Variant? param)
        {
            Launcher.activate_menu_launch_uri(action,param,this);
        }
        internal void activate_menu_command(SimpleAction action, Variant? param)
        {
            Launcher.activate_menu_launch_command(action,param,this);
        }
        public void activate_menu_launch_type(SimpleAction action, Variant? param)
        {
            unowned string type = param.get_string();
            var info = GLib.AppInfo.get_default_for_type(type,false) as DesktopAppInfo;
            MenuMaker.launch(info,null,widget);
        }
        public void activate_desktop(SimpleAction action, Variant? param)
        {
            try{
                unowned string desktop = Environment.get_variable("XDG_CURRENT_DESKTOP");
                DesktopAppInfo? info = null;
                switch(desktop)
                {
                    case "MATE":
                        info = new DesktopAppInfo("mate-appearance-properties.desktop");
                        break;
                    case "XFCE":
                        info = new DesktopAppInfo("xfce-backdrop-settings.desktop");
                        break;
                    case "LXDE":
                        info = AppInfo.create_from_commandline("pcmanfm --desktop-pref",null,
                        AppInfoCreateFlags.SUPPORTS_STARTUP_NOTIFICATION) as DesktopAppInfo;
                        break;
                    default:
                        warning("Unknown desktop environment\n");
                        info = AppInfo.create_from_commandline("gnome-control-center backgrounds",null,
                        AppInfoCreateFlags.SUPPORTS_STARTUP_NOTIFICATION) as DesktopAppInfo;
                        break;
                }
                MenuMaker.launch(info,null,widget);
            } catch (GLib.Error e){stderr.printf("%s\n",e.message);}
        }
        public void activate_control(SimpleAction action, Variant? param)
        {
            try{
                unowned string desktop = Environment.get_variable("XDG_CURRENT_DESKTOP");
                DesktopAppInfo? info = null;
                switch(desktop)
                {
                    case "XFCE":
                        info = new DesktopAppInfo("xfce-settings-manager.desktop");
                        break;
                    case "MATE":
                        info = AppInfo.create_from_commandline("mate-control-center",null,
                        AppInfoCreateFlags.SUPPORTS_STARTUP_NOTIFICATION) as DesktopAppInfo;
                        break;
                    case "LXDE":
                        info = new DesktopAppInfo("lxappearance.desktop");
                        break;
                    default:
                        warning("Unknown desktop environment\n");
                        info = AppInfo.create_from_commandline("gnome-control-center",null,
                        AppInfoCreateFlags.SUPPORTS_STARTUP_NOTIFICATION) as DesktopAppInfo;
                        break;
                }
                MenuMaker.launch(info,null,widget);
            } catch (GLib.Error e){stderr.printf("%s\n",e.message);}
        }
        public void state_populate_files(SimpleAction action, Variant? param)
        {
            populate_menu(files_menu,UserDirectory.DOWNLOAD);
            action.set_state(new Variant.boolean(true));
        }
        public void state_populate_docs(SimpleAction action, Variant? param)
        {
            populate_menu(documents_menu,UserDirectory.DOCUMENTS);
            action.set_state(new Variant.boolean(true));
        }
        public void state_populate_music(SimpleAction action, Variant? param)
        {
            populate_menu(music_menu,UserDirectory.MUSIC);
            action.set_state(new Variant.boolean(true));
        }
        public void state_populate_picts(SimpleAction action, Variant? param)
        {
            populate_menu(pictures_menu,UserDirectory.PICTURES);
            action.set_state(new Variant.boolean(true));
        }
        public void state_populate_video(SimpleAction action, Variant? param)
        {
            populate_menu(videos_menu,UserDirectory.VIDEOS);
            action.set_state(new Variant.boolean(true));
        }
        private void populate_menu(GLib.Menu menu, GLib.UserDirectory udir)
        {
            var desktop_dir = Environment.get_user_special_dir(udir);
            menu.remove_all();
            try
            {
                var dir = Dir.open(desktop_dir);
                for(var name = dir.read_name();name != null; name = dir.read_name())
                {
                    var file = File.new_for_path(desktop_dir + "/" + name);
                    var info = file.query_info(FileAttribute.STANDARD_DISPLAY_NAME+","
                                               +FileAttribute.STANDARD_IS_HIDDEN,
                                               FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
                    if (!info.get_is_hidden())
                    {
                        var item = new GLib.MenuItem(info.get_display_name(),null);
                        item.set_action_and_target("menu.launch-uri", "s", file.get_uri() );
                        menu.append_item(item);
                    }
                }
            } catch (Error e) {
                stderr.printf("%s\n",e.message);
            }
            if (menu.get_n_items() == 0)
                menu.append(GLib.dgettext(Config.GETTEXT_PACKAGE,"No files"),"ls.this-should-not-be-reached");
        }
        ~DesktopHelper()
        {
            files_menu = null;
            documents_menu = null;
            pictures_menu = null;
            music_menu = null;
            videos_menu = null;
        }
    }
}