Guest User

Untitled

a guest
Jul 3rd, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 7.81 KB | None | 0 0
  1. //  Stephen Smally © 2012
  2. //      This program is free software; you can redistribute it and/or modify
  3. //      it under the terms of the GNU General Public License as published by
  4. //      the Free Software Foundation; either version 2 of the License, or
  5. //      (at your option) any later version.
  6. //      
  7. //      This program is distributed in the hope that it will be useful,
  8. //      but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. //      GNU General Public License for more details.
  11. //      
  12. //      You should have received a copy of the GNU General Public License
  13. //      along with this program; if not, write to the Free Software
  14. //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  15. //      MA 02110-1301, USA.
  16. //      
  17.  
  18.  
  19. using Pk;
  20.  
  21. namespace Lsc.Backend {
  22.     public class AppsManager : Object {
  23.         // Signals
  24.         public signal void app_added (Package app);
  25.         public signal void category_added (string category, string group);
  26.         public signal void loading_started (string text);
  27.         public signal void loading_finished ();
  28.         public signal void details_received (Package pkg, Details details);
  29.        
  30.         // PackageKit stuffs
  31.         private Client client;
  32.         private Control control;
  33.         private Cancellable transaction;
  34.        
  35.         // Vars
  36.         public string[] icon_table;
  37.        
  38.         public static void get_pkgs_cb (Progress progress, ProgressType type) {
  39.             if (type == ProgressType.PACKAGE_ID && progress.package != null) {
  40.                 stdout.printf("%s, %s\n", progress.package.get_name(), progress.package.get_arch());
  41.                 app_added(progress.package);
  42.                 while (Gtk.events_pending())spe
  43.                     Gtk.main_iteration();
  44.             }
  45.         }
  46.        
  47.         public void ready () {
  48.             loading_finished();
  49.         }
  50.        
  51.         public void get_pkgs (string filters) {
  52.             Bitfield filter;
  53.             filter = filter_bitfield_from_string(filters);
  54.             loading_started("Loading packages");
  55.             client.get_packages_async(filter, null, (ProgressCallback) get_pkgs_cb, () => {ready();});
  56.         }
  57.        
  58.         public void search_for_packages (string filters, string name) {
  59.             Bitfield filter;
  60.             filter = filter_bitfield_from_string(filters);
  61.             loading_started("Loading packages");
  62.             client.search_names_async(filter, {name}, null, (pkg) => {
  63.                 if (pkg.package != null) {
  64.                     app_added(pkg.package);
  65.                 }
  66.                 while (Gtk.events_pending())
  67.                     Gtk.main_iteration();
  68.             }, () => {
  69.                 loading_finished();
  70.             });
  71.         }
  72.        
  73.         public void get_details (string pkg_id) {
  74.             client.get_details_async({pkg_id},
  75.             null,
  76.             () => {},
  77.             (obj, res) => {
  78.                 Results contains = client.generic_finish(res);
  79.                 details_received(get_package_from_id(pkg_id), contains.get_details_array()[0]);
  80.             });
  81.         }
  82.        
  83.         public Package get_package_from_id (string pkg_id) {
  84.             Results res = client.resolve(filter_bitfield_from_string(""),
  85.             {pkg_id},
  86.             null,
  87.             null);
  88.             return res.get_package_array()[0];
  89.         }
  90.        
  91.         private void fill_icon_table () {
  92.             icon_table = {
  93.                 string.joinv("|", {group_enum_to_string(Group.UNKNOWN),"help-browser"}),    /* fall though value */
  94.                 string.joinv("|", {group_enum_to_string(Group.ACCESSIBILITY), "preferences-desktop-accessibility"}),
  95.                 string.joinv("|", {group_enum_to_string(Group.ACCESSORIES), "applications-accessories"}),
  96.                 string.joinv("|", {group_enum_to_string(Group.ADMIN_TOOLS), "system-lock-screen"}),
  97.                 string.joinv("|", {group_enum_to_string(Group.COLLECTIONS), "pk-collection-installed"}),
  98.                 string.joinv("|", {group_enum_to_string(Group.COMMUNICATION), "folder-remote"}),
  99.                 string.joinv("|", {group_enum_to_string(Group.DESKTOP_GNOME), "pk-desktop-gnome"}),
  100.                 string.joinv("|", {group_enum_to_string(Group.DESKTOP_KDE), "pk-desktop-kde"}),
  101.                 string.joinv("|", {group_enum_to_string(Group.DESKTOP_OTHER), "user-desktop"}),
  102.                 string.joinv("|", {group_enum_to_string(Group.DESKTOP_XFCE), "pk-desktop-xfce"}),
  103.                 string.joinv("|", {group_enum_to_string(Group.DOCUMENTATION), "x-office-address-book"}),
  104.                 string.joinv("|", {group_enum_to_string(Group.EDUCATION), "utilities-system-monitor"}),
  105.                 string.joinv("|", {group_enum_to_string(Group.ELECTRONICS), "video-display"}),
  106.                 string.joinv("|", {group_enum_to_string(Group.FONTS), "preferences-desktop-font"}),
  107.                 string.joinv("|", {group_enum_to_string(Group.GAMES), "applications-games"}),
  108.                 string.joinv("|", {group_enum_to_string(Group.GRAPHICS), "applications-graphics"}),
  109.                 string.joinv("|", {group_enum_to_string(Group.INTERNET), "applications-internet"}),
  110.                 string.joinv("|", {group_enum_to_string(Group.LEGACY), "media-floppy"}),
  111.                 string.joinv("|", {group_enum_to_string(Group.LOCALIZATION), "preferences-desktop-locale"}),
  112.                 string.joinv("|", {group_enum_to_string(Group.MAPS), "applications-multimedia"}),
  113.                 string.joinv("|", {group_enum_to_string(Group.MULTIMEDIA), "applications-multimedia"}),
  114.                 string.joinv("|", {group_enum_to_string(Group.NETWORK), "network-wired"}),
  115.                 string.joinv("|", {group_enum_to_string(Group.OFFICE), "applications-office"}),
  116.                 string.joinv("|", {group_enum_to_string(Group.OTHER), "applications-other"}),
  117.                 string.joinv("|", {group_enum_to_string(Group.POWER_MANAGEMENT), "battery"}),
  118.                 string.joinv("|", {group_enum_to_string(Group.PROGRAMMING), "applications-development"}),
  119.                 string.joinv("|", {group_enum_to_string(Group.PUBLISHING), "accessories-dictionary"}),
  120.                 string.joinv("|", {group_enum_to_string(Group.REPOS), "system-file-manager"}),
  121.                 string.joinv("|", {group_enum_to_string(Group.SCIENCE), "application-certificate"}),
  122.                 string.joinv("|", {group_enum_to_string(Group.SECURITY), "network-wireless-encrypted"}),
  123.                 string.joinv("|", {group_enum_to_string(Group.SERVERS), "network-server"}),
  124.                 string.joinv("|", {group_enum_to_string(Group.SYSTEM), "applications-system"}),
  125.                 string.joinv("|", {group_enum_to_string(Group.VIRTUALIZATION), "computer"}),
  126.                 string.joinv("|", {group_enum_to_string(Group.VENDOR), "application-certificate"}),
  127.                 string.joinv("|", {group_enum_to_string(Group.NEWEST), "dialog-information"})
  128.             };
  129.         }
  130.        
  131.         private string group_to_icon_name(string group) {
  132.             foreach (string couple in icon_table) {
  133.                 if (couple.split("|")[0] == group) {
  134.                 return couple.split("|")[1];
  135.                 }
  136.             }
  137.            
  138.             return "";
  139.         }
  140.        
  141.         public void get_categories () {
  142.             for (int i = 0; i < Group.LAST; i++) {
  143.                 if (i != Group.COLLECTIONS && i != Group.NEWEST)
  144.                     category_added(group_to_icon_name(group_enum_to_string((Group)i)), group_enum_to_string((Group)i));
  145.             }
  146.         }
  147.        
  148.         public AppsManager () {
  149.             client = new Client();
  150.             control = new Control();
  151.             transaction = new Cancellable();
  152.             fill_icon_table();
  153.         }
  154.     }
  155. }
Add Comment
Please, Sign In to add comment