~ubuntu-branches/ubuntu/jaunty/pida/jaunty

« back to all changes in this revision

Viewing changes to pida/services/newfile.py

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-08-01 13:08:56 UTC
  • mfrom: (0.1.2 etch) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060801130856-v92ktopgdxc8rv7q
Tags: 0.3.1-2ubuntu1
* Re-sync with Debian
* Remove bashisms from debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*- 
 
2
 
 
3
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
 
4
#Copyright (c) 2006 Ali Afshar aafshar@gmail.com
 
5
 
 
6
#Permission is hereby granted, free of charge, to any person obtaining a copy
 
7
#of this software and associated documentation files (the "Software"), to deal
 
8
#in the Software without restriction, including without limitation the rights
 
9
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
10
#copies of the Software, and to permit persons to whom the Software is
 
11
#furnished to do so, subject to the following conditions:
 
12
 
 
13
#The above copyright notice and this permission notice shall be included in
 
14
#all copies or substantial portions of the Software.
 
15
 
 
16
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
17
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
18
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
19
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
20
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
21
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
22
#SOFTWARE.
 
23
 
 
24
import os
 
25
 
 
26
import pida.core.service as service
 
27
 
 
28
defs = service.definitions
 
29
types = service.types
 
30
 
 
31
import pida.pidagtk.contentview as contentview
 
32
 
 
33
import gtk
 
34
 
 
35
class new_file_options(gtk.VBox):
 
36
    
 
37
    def __init__(self):
 
38
        super(new_file_options, self).__init__(spacing=6)
 
39
        hb = gtk.HBox(spacing=6)
 
40
        self.pack_start(hb, expand=False)
 
41
        copycheck = gtk.CheckButton(label='License')
 
42
        copycheck.connect('toggled', self.cb_copy_toggled)
 
43
        hb.pack_start(copycheck, expand=False)
 
44
        self.__copy_combo = gtk.combo_box_new_text()
 
45
        hb.pack_start(self.__copy_combo)
 
46
        copycheck.set_active(False)
 
47
        self.__copy_combo.set_sensitive(False)
 
48
        hb = gtk.HBox(spacing=6)
 
49
        self.pack_start(hb, expand=False)
 
50
        copycheck = gtk.CheckButton(label='Comment Style')
 
51
        copycheck.connect('toggled', self.cb_comment_toggled)
 
52
        hb.pack_start(copycheck, expand=False)
 
53
        self.__comment_combo = gtk.combo_box_new_text()
 
54
        hb.pack_start(self.__comment_combo)
 
55
        copycheck.set_active(True)
 
56
        self.show_all()
 
57
 
 
58
    def cb_copy_toggled(self, tgl):
 
59
        self.__copy_combo.set_sensitive(tgl.get_active())
 
60
 
 
61
    def cb_comment_toggled(self, tgl):
 
62
        self.__comment_combo.set_sensitive(tgl.get_active())
 
63
            
 
64
class new_file(service.service):
 
65
 
 
66
    class locations(defs.optiongroup):
 
67
        """Options relating to the location of new files."""
 
68
        class start_in_current_project_directory(defs.option):
 
69
            """Whether files will be made in the current project directory."""
 
70
            rtype = types.boolean
 
71
            default = True
 
72
        class use_project_directories_as_shortcuts(defs.option):
 
73
            """Whether the project directories will be added as shortcuts to the file chooser."""
 
74
            rtype = types.boolean
 
75
            default = True
 
76
 
 
77
    def init(self):
 
78
        self.__projlinks = os.path.join(self.boss.pida_home, 'data',
 
79
                                        'project-shortcuts')
 
80
        if not os.path.exists(self.__projlinks):
 
81
            os.mkdir(self.__projlinks)
 
82
 
 
83
    def cmd_create_interactive(self, directory=None,
 
84
                               mkdir=False):
 
85
        if directory is None:
 
86
            directory = os.getcwd()
 
87
            if self.opt('locations', 'start_in_current_project_directory'):
 
88
                proj = self.boss.call_command('projectmanager',
 
89
                                              'get_current_project')
 
90
                if proj is not None:
 
91
                    directory = proj.source_directory
 
92
        if mkdir:
 
93
            def _callback(name):
 
94
                path = os.path.join(directory, name)
 
95
                os.mkdir(path)
 
96
            prompt = 'Directory Name'
 
97
            self.boss.call_command('window', 'input',
 
98
                               callback_function=_callback,
 
99
                                prompt=prompt)
 
100
        else:
 
101
            view = self.create_view()
 
102
            view.set_current_folder(directory)
 
103
            view.run()
 
104
            
 
105
 
 
106
    def cmd_create(self, filename):
 
107
        f = open(filename, 'w')
 
108
        f.close()
 
109
 
 
110
    def create_view(self):
 
111
        chooser = gtk.FileChooserDialog("Create New File",
 
112
                        self.boss.get_main_window(),
 
113
                        gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
 
114
                        (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT,
 
115
                        gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT))
 
116
        chooser.set_action(gtk.FILE_CHOOSER_ACTION_SAVE)
 
117
        try:
 
118
            chooser.set_do_overwrite_confirmation(True)
 
119
        except AttributeError:
 
120
            pass
 
121
        chooser.connect('response', self.cb_response)
 
122
        if self.opt('locations', 'use_project_directories_as_shortcuts'):
 
123
            for directory in self.__create_project_links():
 
124
                chooser.add_shortcut_folder(directory)
 
125
        options = new_file_options()
 
126
        #chooser.vbox.pack_start(options, expand=False)
 
127
        return chooser
 
128
 
 
129
    def cb_response(self, dlg, response):
 
130
        if response == gtk.RESPONSE_ACCEPT:
 
131
            filename = dlg.get_filename()
 
132
            self.call('create', filename=dlg.get_filename())
 
133
            self.boss.call_command('buffermanager', 'open_file',
 
134
                                   filename=filename)
 
135
        dlg.destroy()
 
136
 
 
137
    def __create_project_links(self):
 
138
        projs = self.boss.call_command('projectmanager', 'get_projects')
 
139
        for proj in projs:
 
140
            src = proj.source_directory
 
141
            dst = os.path.join(self.__projlinks, proj.name)
 
142
            if os.path.exists(dst):
 
143
                os.unlink(dst)
 
144
            os.symlink(src, dst)
 
145
            yield dst
 
146
        
 
147
 
 
148
Service = new_file