From 74b2ff309837a422d4e33dc2d178310fbfc58901 Mon Sep 17 00:00:00 2001
From: Aleksey Lim <alsroot@member.fsf.org>
Date: Fri, 18 Sep 2009 11:03:10 +0000
Subject: browse should use a better name when uploading an entry from the journal #901
---
filepicker.py | 54 +++++++++++++++++++++++++++++++++++++++---------------
1 files changed, 39 insertions(+), 15 deletions(-)
diff --git a/filepicker.py b/filepicker.py
index 4a39fdd..9af28aa 100644
a
|
b
|
import logging |
18 | 18 | import os |
19 | 19 | import tempfile |
20 | 20 | import shutil |
| 21 | import re |
| 22 | from gettext import gettext as _ |
21 | 23 | |
22 | 24 | import gtk |
23 | 25 | import hulahop |
… |
… |
from xpcom import components |
27 | 29 | from xpcom.components import interfaces |
28 | 30 | from xpcom.server.factory import Factory |
29 | 31 | |
| 32 | from sugar import mime |
30 | 33 | from sugar.graphics.objectchooser import ObjectChooser |
| 34 | from sugar.activity.activity import get_activity_root |
31 | 35 | |
32 | | _temp_files_to_clean = [] |
| 36 | _temp_dirs_to_clean = [] |
33 | 37 | |
34 | 38 | def cleanup_temp_files(): |
35 | | for temp_file in _temp_files_to_clean: |
36 | | if os.path.exists(temp_file): |
37 | | os.remove(temp_file) |
38 | | _temp_files_to_clean.remove(temp_file) |
| 39 | while _temp_dirs_to_clean: |
| 40 | temp_dir = _temp_dirs_to_clean.pop() |
| 41 | if os.path.isdir(temp_dir): |
| 42 | shutil.rmtree(temp_dir, ignore_errors=True) |
39 | 43 | else: |
40 | 44 | logging.debug('filepicker.cleanup_temp_files: no file %r' |
41 | | % temp_file) |
| 45 | % temp_dir) |
42 | 46 | |
43 | 47 | class FilePicker: |
44 | 48 | _com_interfaces_ = interfaces.nsIFilePicker |
… |
… |
class FilePicker: |
67 | 71 | |
68 | 72 | def show(self): |
69 | 73 | chooser = ObjectChooser(parent=self._parent) |
| 74 | jobject = None |
70 | 75 | try: |
71 | 76 | result = chooser.run() |
72 | 77 | if result == gtk.RESPONSE_ACCEPT: |
73 | | logging.debug('FilePicker.show: %r' % |
74 | | chooser.get_selected_object()) |
75 | 78 | jobject = chooser.get_selected_object() |
| 79 | logging.debug('FilePicker.show: %r', jobject) |
| 80 | |
76 | 81 | if jobject and jobject.file_path: |
77 | | ext = os.path.splitext(jobject.file_path)[1] |
78 | | f, new_temp = tempfile.mkstemp(ext) |
79 | | del f |
| 82 | tmp_dir = tempfile.mkdtemp( |
| 83 | prefix='', # tmp/tmp.. looks ugly |
| 84 | dir=os.path.join(get_activity_root(), 'tmp')) |
| 85 | self._file = os.path.join(tmp_dir, |
| 86 | self._basename_strip(jobject)) |
| 87 | |
| 88 | os.rename(jobject.file_path, self._file) |
80 | 89 | |
81 | | global _temp_files_to_clean |
82 | | _temp_files_to_clean.append(new_temp) |
83 | | shutil.copy(jobject.file_path, new_temp) |
| 90 | global _temp_dirs_to_clean |
| 91 | _temp_dirs_to_clean.append(tmp_dir) |
84 | 92 | |
85 | | self._file = new_temp |
| 93 | logging.debug('FilePicker.show: file=%r', self._file) |
86 | 94 | finally: |
| 95 | if jobject is not None: |
| 96 | jobject.destroy() |
87 | 97 | chooser.destroy() |
88 | 98 | del chooser |
89 | 99 | |
… |
… |
class FilePicker: |
92 | 102 | else: |
93 | 103 | return interfaces.nsIFilePicker.returnCancel |
94 | 104 | |
| 105 | def _basename_strip(self, jobject): |
| 106 | name = jobject.metadata.get('title', _('No Name')) |
| 107 | name = name.replace(os.sep, ' ').strip() |
| 108 | |
| 109 | root_, mime_extension = os.path.splitext(jobject.file_path) |
| 110 | |
| 111 | if not name.endswith(mime_extension): |
| 112 | if re.search('\.\S+$', name) is None: |
| 113 | # add mime_type extension only |
| 114 | # if 'title' doesn't have any extensions |
| 115 | name = name + mime_extension |
| 116 | |
| 117 | return name |
| 118 | |
95 | 119 | def set_defaultExtension(self, default_extension): |
96 | 120 | logging.warning('FilePicker.set_defaultExtension: UNIMPLEMENTED') |
97 | 121 | |