eagleoneraptor

Place formatter patch

Oct 3rd, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 10.96 KB | None | 0 0
  1. diff --git a/src/gnome-maps.js.gresource.xml b/src/gnome-maps.js.gresource.xml
  2. index 5e8ef6d..9dd7f60 100644
  3. --- a/src/gnome-maps.js.gresource.xml
  4. +++ b/src/gnome-maps.js.gresource.xml
  5. @@ -19,6 +19,7 @@
  6.      <file>notificationManager.js</file>
  7.      <file>path.js</file>
  8.      <file>placeEntry.js</file>
  9. +    <file>placeFormatter.js</file>
  10.      <file>placeStore.js</file>
  11.      <file>route.js</file>
  12.      <file>routeQuery.js</file>
  13. diff --git a/src/placeFormatter.js b/src/placeFormatter.js
  14. new file mode 100644
  15. index 0000000..05322f5
  16. --- /dev/null
  17. +++ b/src/placeFormatter.js
  18. @@ -0,0 +1,135 @@
  19. +/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
  20. +/* vim: set et ts=4 sw=4: */
  21. +/*
  22. + * Copyright (c) 2014 Damián Nohales
  23. + *
  24. + * GNOME Maps is free software; you can redistribute it and/or modify
  25. + * it under the terms of the GNU General Public License as published by the
  26. + * Free Software Foundation; either version 2 of the License, or (at your
  27. + * option) any later version.
  28. + *
  29. + * GNOME Maps is distributed in the hope that it will be useful, but
  30. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  31. + * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  32. + * for more details.
  33. + *
  34. + * You should have received a copy of the GNU General Public License along
  35. + * with GNOME Maps; if not, write to the Free Software Foundation,
  36. + * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  37. + *
  38. + * Author: Damián Nohales <[email protected]>
  39. + */
  40. +
  41. +const Geocode = imports.gi.GeocodeGlib;
  42. +
  43. +const Lang = imports.lang;
  44. +
  45. +const PlaceFormatter = new Lang.Class({
  46. +    Name: "PlaceFormatter",
  47. +
  48. +    _init: function(place) {
  49. +        this.place = place;
  50. +    },
  51. +
  52. +    set place(place) {
  53. +        this._place = place;
  54. +        this._update();
  55. +    },
  56. +
  57. +    get place() {
  58. +        return this._place;
  59. +    },
  60. +
  61. +    get titleProperty() {
  62. +        return this._titleProperty;
  63. +    },
  64. +
  65. +    get title() {
  66. +        return this.getProperty(this.titleProperty);
  67. +    },
  68. +
  69. +    get infoPropertyGroups() {
  70. +        return this._infoPropertyGroups;
  71. +    },
  72. +
  73. +    getProperty: function(prop) {
  74. +        return this.place[prop];
  75. +    },
  76. +
  77. +    _update: function() {
  78. +        let title;
  79. +        let info = [];
  80. +        let place = this.place;
  81. +
  82. +        if (this._isBrokenPlace(place)) {
  83. +            // Fallback for places coming from PlaceStore
  84. +            title = 'name';
  85. +        } else {
  86. +            switch (place.place_type) {
  87. +            case Geocode.PlaceType.COUNTRY:
  88. +                title = 'country';
  89. +                this._addInfoGroup(info, place, ['country_code']);
  90. +                break;
  91. +
  92. +            case Geocode.PlaceType.STATE:
  93. +                title = 'state';
  94. +                this._addInfoGroup(info, place, ['country']);
  95. +                break;
  96. +
  97. +            case Geocode.PlaceType.COUNTY:
  98. +                title = 'county';
  99. +                this._addInfoGroup(info, place, ['state', 'country']);
  100. +                break;
  101. +
  102. +            case Geocode.PlaceType.TOWN:
  103. +            case Geocode.PlaceType.LOCAL_ADMINISTRATIVE_AREA:
  104. +                title = place.town ? 'town' : 'administrative_area';
  105. +                if (title === 'town')
  106. +                    this._addInfoGroup(info, place, ['administrative_area']);
  107. +                this._addInfoGroup(info, place, ['postal_code']);
  108. +                this._addInfoGroup(info, place, ['county']);
  109. +                this._addInfoGroup(info, place, ['state', 'country']);
  110. +                break;
  111. +
  112. +            case Geocode.PlaceType.STREET:
  113. +            case Geocode.PlaceType.MOTORWAY:
  114. +            case Geocode.PlaceType.BUS_STOP:
  115. +                title = place.street_address ? 'street_address' : 'street';
  116. +                this._addInfoGroup(info, place, ['area', 'town', 'administrative_area']);
  117. +                this._addInfoGroup(info, place, ['postal_code']);
  118. +                this._addInfoGroup(info, place, ['county']);
  119. +                this._addInfoGroup(info, place, ['state', 'country']);
  120. +                break;
  121. +
  122. +            //TODO: add specific UIs for the rest of the place types
  123. +            default:
  124. +                title = 'name';
  125. +                break;
  126. +            }
  127. +        }
  128. +
  129. +        // Ensure that we have a valid title
  130. +        if (!this.getProperty(title))
  131. +            title = 'name';
  132. +
  133. +        this._titleProperty = title;
  134. +        this._infoPropertyGroups = info;
  135. +    },
  136. +
  137. +    _isBrokenPlace: function(place) {
  138. +        // Broken places are GeocodePlace objects coming from PlaceStore,
  139. +        // which doesn't save most of the place properties.
  140. +        // See: https://bugzilla.gnome.org/show_bug.cgi?id=726625
  141. +        return !place.country && !place.state && !place.county && !place.town &&
  142. +               !place.street && !place.street_address;
  143. +    },
  144. +
  145. +    _addInfoGroup: function(info, place, properties) {
  146. +        properties = properties.filter((function(prop) {
  147. +            return this.getProperty(prop) ? true : false;
  148. +        }).bind(this));
  149. +
  150. +        if (properties.length > 0)
  151. +            info.push(properties);
  152. +    }
  153. +});
  154. \ No newline at end of file
  155. diff --git a/src/searchPopup.js b/src/searchPopup.js
  156. index f54d58c..7a4459a 100644
  157. --- a/src/searchPopup.js
  158. +++ b/src/searchPopup.js
  159. @@ -22,9 +22,13 @@ const Gtk = imports.gi.Gtk;
  160.  const GLib = imports.gi.GLib;
  161.  const GdkPixbuf = imports.gi.GdkPixbuf;
  162.  const GObject = imports.gi.GObject;
  163. +const Pango = imports.gi.Pango;
  164.  
  165.  const Lang = imports.lang;
  166. +
  167. +const PlaceFormatter = imports.placeFormatter;
  168.  const Utils = imports.utils;
  169. +const _ = imports.gettext.gettext;
  170.  
  171.  const Columns = {
  172.      ICON:         0,
  173. @@ -81,7 +85,9 @@ const SearchPopup = new Lang.Class({
  174.          column.add_attribute(cell, 'pixbuf', Columns.ICON);
  175.  
  176.          cell = new Gtk.CellRendererText({ xpad: 8,
  177. -                                          ypad: 8 });
  178. +                                          ypad: 8,
  179. +                                          wrap_mode: Pango.WrapMode.WORD,
  180. +                                          wrap_width: 500 });
  181.          column.pack_start(cell, true);
  182.          column.add_attribute(cell, 'markup', Columns.DESCRIPTION);
  183.  
  184. @@ -145,10 +151,9 @@ const SearchPopup = new Lang.Class({
  185.                  return;
  186.  
  187.              let iter = model.append();
  188. -            let location = place.get_location();
  189.              let icon = place.icon;
  190.  
  191. -            let description = GLib.markup_escape_text(location.description, -1);
  192. +            let description = this._getDescription(place);
  193.              description = this._boldMatch(description, searchString);
  194.  
  195.              model.set(iter,
  196. @@ -165,6 +170,30 @@ const SearchPopup = new Lang.Class({
  197.          }).bind(this));
  198.      },
  199.  
  200. +    _getDescription: function(place) {
  201. +        let formatter = new PlaceFormatter.PlaceFormatter(place);
  202. +        let title = formatter.title;
  203. +        let info = formatter.infoPropertyGroups.map(function(group) {
  204. +            group = group.map(function(prop) {
  205. +                switch (prop) {
  206. +                case 'country_code':
  207. +                    return _("Country code: %s").format(formatter.getProperty(prop));
  208. +
  209. +                default:
  210. +                    return formatter.getProperty(prop);
  211. +                }
  212. +            });
  213. +
  214. +            return group.join(', ');
  215. +        }).join(', ');
  216. +
  217. +        if (info)
  218. +            return '%s\n<small>%s</small>'.format(GLib.markup_escape_text(title, -1),
  219. +                                                  GLib.markup_escape_text(info, -1));
  220. +        else
  221. +            return GLib.markup_escape_text(title, -1);
  222. +    },
  223. +
  224.      _boldMatch: function(description, searchString) {
  225.          searchString = searchString.toLowerCase();
  226.  
  227. diff --git a/src/searchResultBubble.js b/src/searchResultBubble.js
  228. index fb40535..a6e7286 100644
  229. --- a/src/searchResultBubble.js
  230. +++ b/src/searchResultBubble.js
  231. @@ -26,6 +26,7 @@ const Gtk = imports.gi.Gtk;
  232.  const Lang = imports.lang;
  233.  
  234.  const MapBubble = imports.mapBubble;
  235. +const PlaceFormatter = imports.placeFormatter;
  236.  const Utils = imports.utils;
  237.  const _ = imports.gettext.gettext;
  238.  
  239. @@ -46,41 +47,29 @@ const SearchResultBubble = new Lang.Class({
  240.              ui.image.pixbuf = pixbuf;
  241.          });
  242.  
  243. -        let title = null;
  244. -        let content = [];
  245. +        let formatter = new PlaceFormatter.PlaceFormatter(place);
  246. +        let info = [];
  247.  
  248. -        if (this._isBrokenPlace(place)) {
  249. -            // Fallback for places coming from PlaceStore
  250. -            title = place.name;
  251. -        } else {
  252. -            switch (place.place_type) {
  253. -            case Geocode.PlaceType.COUNTRY:
  254. -                title = place.country;
  255. -                if (place.country_code)
  256. -                    content.push(_("Country code: %s").format(place.country_code));
  257. -                break;
  258. +        ui.labelTitle.label = formatter.title;
  259. +        info = formatter.infoPropertyGroups.map(function(group) {
  260. +            group = group.map(function(prop) {
  261. +                switch (prop) {
  262. +                case 'postal_code':
  263. +                    return _("Postal code: %s").format(formatter.getProperty(prop));
  264.  
  265. -            case Geocode.PlaceType.TOWN:
  266. -                title = place.town;
  267. -                if (place.postal_code)
  268. -                    content.push(_("Postal code: %s").format(place.postal_code));
  269. -                if (place.state)
  270. -                    content.push(place.state + ', ' + place.country);
  271. -                else
  272. -                    content.push(place.country);
  273. -                break;
  274. +                case 'country_code':
  275. +                    return _("Country code: %s").format(formatter.getProperty(prop));
  276.  
  277. -            //TODO: add specific UIs for the rest of the place types
  278. -            default:
  279. -                title = place.name;
  280. -                break;
  281. -            }
  282. -        }
  283. +                default:
  284. +                    return formatter.getProperty(prop);
  285. +                }
  286. +            });
  287.  
  288. -        ui.labelTitle.label = title;
  289. +            return group.join(', ');
  290. +        });
  291.  
  292. -        content.forEach(function(c) {
  293. -            let label = new Gtk.Label({ label: c,
  294. +        info.forEach(function(i) {
  295. +            let label = new Gtk.Label({ label: i,
  296.                                          visible: true,
  297.                                          halign: Gtk.Align.START });
  298.              ui.boxRight.pack_start(label, false, true, 0);
  299. @@ -88,12 +77,4 @@ const SearchResultBubble = new Lang.Class({
  300.  
  301.          this.add(ui.grid);
  302.      },
  303. -
  304. -    _isBrokenPlace: function(place) {
  305. -        // Broken places are GeocodePlace objects coming from PlaceStore,
  306. -        // which doesn't save most of the place properties.
  307. -        // See: https://bugzilla.gnome.org/show_bug.cgi?id=726625
  308. -        return !place.country && !place.state && !place.county && !place.town &&
  309. -               !place.street && !place.street_address;
  310. -    }
  311.  });
Advertisement
Add Comment
Please, Sign In to add comment