~lubuntu-software-center-team/lubuntu-software-center/vala-port

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
204
205
206
207
208
209
210
211
212
213
214
215
216
//  Copyright © 2012 Stephen Smally
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 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 General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.
//      

using Gtk;
using Lsc.Backend;
using Lsc.Utils;
using Lsc.Widgets;

namespace Lsc {
    // Elements which should be accessible to every void or class
    public enum PageType {
        HOMEPAGE,
        APPSVIEW,
        APPSINFO;
        
        public string to_string () {
            switch (this) {
                case HOMEPAGE:
                    return "Home Page";
                case APPSVIEW:
                    return "Apps View";
                case APPSINFO:
                    return "Apps Info";
                default:
                    assert_not_reached();
            }
        }
        
        public PageType[] get_all () {
            return { HOMEPAGE, APPSVIEW, APPSINFO };
        }
    }
    
    // Apps Manager which handle the PackageKit connection
    public AppsManager apps_manager;
    
    public class Frontend : Window {
        // Widgets
        public Box main_box;
        public MainToolbar toolbar;
        public PagesView pages_view;
        public ProgressInfo progress_info;
        
        // Vars
        private int _width = 0;
        
        public Frontend () {
			IconTheme theme = IconTheme.get_default();
			theme.append_search_path ("/usr/share/app-install/icons/");
			
            apps_manager = new AppsManager();
            
            destroy.connect(Gtk.main_quit);
            size_allocate.connect(on_size_allocate);
            
            window_position = WindowPosition.CENTER;
            title = "Light Software Center";
            set_default_size(600, 400);
            has_resize_grip = true;
            main_box = new Box(Orientation.VERTICAL, 0);
            
            toolbar = new MainToolbar();
            main_box.pack_start(toolbar, false, false, 0);
            
            progress_info = new ProgressInfo();
            main_box.pack_start(progress_info, false, false, 0);
            
            pages_view = new PagesView();
            
            main_box.pack_start(pages_view, true, true, 0);
            
            connect_signals();
            apps_manager.get_categories();
            
            add(main_box);
            show_all();
            
            progress_info.set_visible(false);
            pages_view.apps_info.reviews_box.set_visible(false);
            
            set_focus(null);
        }
        
        public void on_info_message_response (ResponseId id, LscApp app) {
			switch (id) {
				case ResponseId.INFO:
				    load_details (app);
				    break;
				case ResponseId.INSTALL:
				    break;
			}
		}
        
        public void load_details (LscApp app) {
			apps_manager.get_details (app);
			pages_view.set_page(PageType.APPSINFO);
		}
        
        public void load_packages (LscCategory category) {
            pages_view.set_page(PageType.APPSVIEW);
            pages_view.apps_view.apps_tree.clear();
            apps_manager.get_pkgs(category.id);
            toolbar.label.label = category.name;
        }
        
        public void update_back_button (Notebook nb, Widget pg, uint page_n) {
            stdout.printf("Page->%s\n", ((PageType) page_n).to_string());
            switch ((PageType) page_n) {
                case PageType.HOMEPAGE:
                    toolbar.searchbar.set_sensitive(true);
                    toolbar.label.margin_left = 5;
                    toolbar.back.set_visible(false);
                    break;
                
                case PageType.APPSINFO:
                    toolbar.searchbar.set_sensitive(false);
                    toolbar.label.margin_left = 0;
                    toolbar.back.set_visible(true);
                    break;
                
                default:
                    toolbar.searchbar.set_sensitive(true);
                    toolbar.label.margin_left = 0;
                    toolbar.back.set_visible(true);
                    break;
            }
        }
        
        public void back_to_homepage () {
			pages_view.apps_view.apps_tree.clear();
			pages_view.set_page(PageType.HOMEPAGE);
		}
        
        public void connect_signals () {
			// When an app is added
            apps_manager.app_added.connect(pages_view.apps_view.apps_tree.append_app);
            // When something start loading
            apps_manager.loading_started.connect(progress_info.load_undefined);
            // When something stop loading
            apps_manager.loading_finished.connect(on_load_finished);
            // When a category is found in the database
            apps_manager.category_added.connect(pages_view.home_page.categories_view.add_category);
            // When a Details object is found from a package
            apps_manager.details_received.connect(pages_view.apps_info.set_details);
            // When either More Info or Install is pressed
            pages_view.apps_view.info_message.choosed.connect(on_info_message_response);
            // When a category is clicked
            pages_view.home_page.categories_view.category_choosed.connect(load_packages);
            // When the page is switched
            pages_view.switch_page.connect(update_back_button);
            pages_view.switch_page.connect(toolbar.update_label);
            // When back is clicked
            toolbar.back.clicked.connect(() => {
				back_to_homepage ();
			});
        }
        
        public void rework_categories_columns () {
            if (pages_view.get_current_page() != PageType.HOMEPAGE) {
                return;
            }
            if (_width != get_allocated_width() && get_allocated_width()/150 != pages_view.home_page.categories_view.columns) {
                stdout.printf("Window.width->%d rework columns\n", get_allocated_width());
                int size = get_allocated_width();
                if (size < 150 ) {
                    size = 150;
                }
                pages_view.home_page.categories_view.columns = size/150;
                
                _width = get_allocated_width();
            }
        }
        
        public void on_size_allocate (Allocation alloc) {
            rework_categories_columns();
        }
        
        public void on_load_finished (LoadingType load) {
            switch (load) {
                case LoadingType.CATEGORIES:
                    rework_categories_columns();
                    break;
                case LoadingType.PACKAGES:
                    progress_info.clear();
                    break;
                case LoadingType.DETAILS:
                    progress_info.clear();
                    break;
                default:
                    progress_info.clear();
                    break;
            }
        }
    }
}

int main (string[] args) {
    Gtk.init(ref args);
    new Lsc.Frontend();
    Gtk.main();
    return 0;
}