|
| 1 | +# This Python caja extension only consider files/folders with a mixed |
| 2 | +# upper/lower case name. For those, the following is featured: |
| 3 | +# - an emblem on the icon, |
| 4 | +# - contextual menu entry. |
| 5 | +# - a list view "Mixed" column, |
| 6 | +# - a property page, |
| 7 | +# - A top area widget. |
| 8 | + |
| 9 | +import os |
| 10 | + |
| 11 | +try: |
| 12 | + # Python 3. |
| 13 | + from urllib.parse import unquote, urlparse |
| 14 | +except: |
| 15 | + # Python 2. |
| 16 | + from urllib import unquote |
| 17 | + from urlparse import urlparse |
| 18 | + |
| 19 | +from gi.repository import Caja, GObject, Gtk |
| 20 | + |
| 21 | + |
| 22 | +class Mixed(GObject.GObject, |
| 23 | + Caja.InfoProvider, |
| 24 | + Caja.ColumnProvider, |
| 25 | + Caja.MenuProvider, |
| 26 | + Caja.PropertyPageProvider, |
| 27 | + Caja.LocationWidgetProvider): |
| 28 | + |
| 29 | + emblem = 'favorite-symbolic.symbolic' # Use one of the stock emblems. |
| 30 | + |
| 31 | + # Private methods. |
| 32 | + |
| 33 | + def _basename(self, uri): |
| 34 | + try: |
| 35 | + uri = uri.get_uri() # In case a CajaFile is given. |
| 36 | + except: |
| 37 | + pass |
| 38 | + (scheme, netloc, path, parameters, query, fragment) = urlparse(uri) |
| 39 | + return os.path.basename(unquote(path)) |
| 40 | + |
| 41 | + def _file_has_mixed_name(self, cajafile): |
| 42 | + name = self._basename(cajafile) |
| 43 | + if name.upper() != name and name.lower() != name: |
| 44 | + return 'mixed' |
| 45 | + return '' |
| 46 | + |
| 47 | + # Caja.InfoProvider implementation. |
| 48 | + |
| 49 | + def update_file_info(self, cajafile): |
| 50 | + mixed = self._file_has_mixed_name(cajafile) |
| 51 | + cajafile.add_string_attribute('mixed', mixed) |
| 52 | + if mixed: |
| 53 | + cajafile.add_emblem(self.emblem) |
| 54 | + |
| 55 | + # Caja.ColumnProvider implementation. |
| 56 | + |
| 57 | + def get_columns(self): |
| 58 | + return [ |
| 59 | + Caja.Column( |
| 60 | + name = 'Mixed::mixed_column', |
| 61 | + attribute = 'mixed', |
| 62 | + label = 'Mixed', |
| 63 | + description = 'Column added by the mixed extension' |
| 64 | + ) |
| 65 | + ] |
| 66 | + |
| 67 | + # Caja.MenuProvider implementation. |
| 68 | + |
| 69 | + def get_file_items(self, window, cajafiles): |
| 70 | + menuitems = [] |
| 71 | + if len(cajafiles) == 1: |
| 72 | + for cajafile in cajafiles: |
| 73 | + mixed = cajafile.get_string_attribute('mixed') |
| 74 | + if mixed: |
| 75 | + filename = self._basename(cajafile) |
| 76 | + menuitem = Caja.MenuItem( |
| 77 | + name = 'Mixed::FileMenu', |
| 78 | + label = 'Mixed: %s has a mixed case name' % filename, |
| 79 | + tip = '', |
| 80 | + icon = '' |
| 81 | + ) |
| 82 | + menuitems.append(menuitem) |
| 83 | + |
| 84 | + return menuitems |
| 85 | + |
| 86 | + def get_background_items(self, window, folder): |
| 87 | + mixed = self._file_has_mixed_name(folder) |
| 88 | + if not mixed: |
| 89 | + return [] |
| 90 | + return [ |
| 91 | + Caja.MenuItem( |
| 92 | + name = 'Mixed::BackgroundMenu', |
| 93 | + label = 'Mixed: you are browsing a directory with a mixed case name', |
| 94 | + tip = '', |
| 95 | + icon = '' |
| 96 | + ) |
| 97 | + ] |
| 98 | + |
| 99 | + # Caja.PropertyPageProvider implementation. |
| 100 | + |
| 101 | + def get_property_pages(self, cajafiles): |
| 102 | + pages = [] |
| 103 | + if len(cajafiles) == 1: |
| 104 | + for cajafile in cajafiles: |
| 105 | + if self._file_has_mixed_name(cajafile): |
| 106 | + page_label = Gtk.Label('Mixed') |
| 107 | + page_label.show() |
| 108 | + hbox = Gtk.HBox(homogeneous = False, spacing = 4) |
| 109 | + hbox.show() |
| 110 | + name_label = Gtk.Label(self._basename(cajafile)) |
| 111 | + name_label.show() |
| 112 | + comment_label = Gtk.Label('has a mixed-case name') |
| 113 | + comment_label.show() |
| 114 | + hbox.pack_start(name_label, False, False, 0) |
| 115 | + hbox.pack_start(comment_label, False, False, 0) |
| 116 | + pages.append( |
| 117 | + Caja.PropertyPage( |
| 118 | + name = 'Mixed::PropertyPage', |
| 119 | + label = page_label, |
| 120 | + page = hbox |
| 121 | + ) |
| 122 | + ) |
| 123 | + |
| 124 | + return pages |
| 125 | + |
| 126 | + # Caja.LocationWidgetProvider implementation. |
| 127 | + |
| 128 | + def get_widget(self, uri, window): |
| 129 | + filename = self._basename(uri) |
| 130 | + if not self._file_has_mixed_name(filename): |
| 131 | + return None |
| 132 | + label = Gtk.Label('In mixed-case directory %s' % filename) |
| 133 | + label.show() |
| 134 | + return label |
0 commit comments