1/*
2 *      Copyright 2015 Julien Lavergne <gilir@ubuntu.com>
3 *
4 *      This program is free software; you can redistribute it and/or modify
5 *      it under the terms of the GNU General Public License as published by
6 *      the Free Software Foundation; either version 2 of the License, or
7 *      (at your option) any later version.
8 *
9 *      This program 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
12 *      GNU General Public License for more details.
13 *
14 *      You should have received a copy of the GNU General Public License
15 *      along with this program; if not, write to the Free Software
16 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 *      MA 02110-1301, USA.
18 */
19#if USE_GTK
20using Gtk;
21#if USE_ADVANCED_NOTIFICATIONS
22using AppIndicator;
23using Notify;
24#endif
25#endif
26
27namespace Lxsession
28{
29#if USE_GTK
30    public class MenuItemObject : Gtk.MenuItem
31#else
32    public class MenuItemObject : MenuItemGenericObject
33#endif
34    {
35        public MenuItemObject ()
36        {
37
38        }
39    }
40
41    public class MenuItemGenericObject
42    {
43        public MenuItemGenericObject ()
44        {
45
46        }
47    }
48
49
50#if USE_GTK
51    public class MenuObject : Gtk.Menu
52#else
53    public class MenuObject : MenuGenericObject
54#endif
55    {
56        public MenuObject ()
57        {
58
59        }
60    }
61
62    public class MenuGenericObject : GLib.Object
63    {
64        public delegate void ActionCallback ();
65
66        public void add_item (string text, owned ActionCallback callback)
67        {
68            warning("Not implemented");
69        }
70    }
71
72    public class IconObject : GLib.Object
73    {
74        public string name;
75        public string icon_name;
76        public string notification_text;
77
78        public MenuObject menu;
79#if USE_GTK
80#if USE_ADVANCED_NOTIFICATIONS
81        public Indicator indicator;
82        public Notify.Notification notification;
83#endif
84#endif
85
86        public delegate void ActionCallback ();
87
88        public IconObject(string name_param, string? icon_name_param, string? notification_param, MenuObject? menu_param)
89        {
90            this.name = name_param;
91
92            if (icon_name_param != null)
93            {
94                this.icon_name = icon_name_param;
95            }
96            else
97            {
98                this.icon_name = "dialog-warning";
99            }
100
101            if (notification_param != null)
102            {
103                this.notification_text = notification_param;
104            }
105
106            this.menu = menu_param;
107#if USE_GTK
108#if USE_ADVANCED_NOTIFICATIONS
109            this.indicator = new Indicator(this.name, this.icon_name, IndicatorCategory.APPLICATION_STATUS);
110            this.notification = new Notify.Notification ("LXsession", this.notification_text, this.icon_name);
111            this.notification.set_timeout(6000);
112#endif
113#endif
114        }
115
116#if USE_GTK
117#if USE_ADVANCED_NOTIFICATIONS
118        public void init()
119        {
120            if (this.indicator == null)
121            {
122                this.indicator = new Indicator(this.name, this.icon_name, IndicatorCategory.APPLICATION_STATUS);
123            }
124
125            this.indicator.set_status(IndicatorStatus.ACTIVE);
126
127            if (this.menu != null)
128            {
129                this.indicator.set_menu(this.menu);
130            }
131        }
132
133        public void activate()
134        {
135            message("Try activate");
136            if (this.indicator != null)
137            {
138                message("Activate");
139                this.indicator.set_status(IndicatorStatus.ACTIVE);
140                try
141                {
142                    this.notification.show ();
143                }
144                catch (GLib.Error e)
145                {
146                    message ("Error: %s\n", e.message);
147                }
148                message("Activate done");
149            }
150        }
151
152        public void inactivate()
153        {
154            message("Try inactivate");
155            if (this.indicator != null)
156            {
157                message("Inactivate");
158                this.indicator.set_status(IndicatorStatus.PASSIVE);
159                message("Inactivate done");
160            }
161        }
162
163        public void set_icon(string param_icon_name)
164        {
165            this.icon_name = param_icon_name;
166            message("Set new icon");
167            this.indicator.icon_name = param_icon_name;
168        }
169
170        public void set_menu(MenuObject param_menu)
171        {
172            this.menu = param_menu;
173            this.indicator.set_menu(param_menu);
174        }
175
176        public void add_action (string action, string label, owned ActionCallback callback)
177        {
178            if (this.notification != null)
179            {
180                this.notification.add_action (action, label, (n, a) =>
181                {
182                    callback ();
183                });
184            }
185        }
186
187        public void set_notification_body(string text)
188        {
189            if (this.notification != null)
190            {
191                this.notification_text = text;
192                this.notification.body = text;
193            }
194        }
195
196        public void clear_actions ()
197        {
198            if (this.notification != null)
199            {
200                this.notification.clear_actions() ;
201            }
202        }
203#else
204        public void init()
205        {
206
207        }
208
209        public void activate()
210        {
211
212        }
213
214        public void inactivate()
215        {
216
217        }
218
219        public void set_icon(string param_icon_name)
220        {
221
222        }
223
224        public void set_menu(MenuObject param_menu)
225        {
226
227        }
228
229        public void add_action (string action, string label, owned ActionCallback callback)
230        {
231
232        }
233
234        public void set_notification_body(string text)
235        {
236
237        }
238
239        public void clear_actions ()
240        {
241
242        }
243#endif
244    }
245}
246