Ejemplo n.º 1
0
 def show_site_settings(self, action, param):
     if self.settings_dialog is None:
         self.settings_dialog = SiteSettingsDialog(self.site,
                                                   self.window)
     self.settings_dialog.run()
Ejemplo n.º 2
0
class SiteWindowController:
    """
    This object turns main window into a site window with loaded site contents.
    """
    def __init__(self, main):
        self.window = main
        self.site = None
        self.file_browser = None
        self.server = None
        self.settings_dialog = None

    def load_site(self, site_path):
        self.site = site.Site(site_path)

        self._init_actions()
        self.file_browser = files_browser.FilesBrowser(self.window,
                                                       self.site)
        self.window.load_site(self.site, self.file_browser.widget)

        self.server = PreviewServer(self.site)
        self.server.start()

    def _init_actions(self):
        self.window.add_simple_action("new-file", self.on_new_file)
        self.window.add_simple_action("new-directory", self.on_new_directory)
        self.window.add_simple_action('add-file', self.on_add_file)
        self.window.add_simple_action('preview', self.display_preview)
        self.window.add_simple_action('terminal', self.display_terminal)
        self.window.add_simple_action('generate', self.on_generate)
        self.window.add_simple_action('upload', self.on_upload)
        self.window.add_simple_action('settings', self.show_site_settings)

    # Actions handlers #

    def on_new_file(self, action, param):
        response, name = self.ask_name("File")
        if response == Gtk.ResponseType.OK and name != '':
            self.site.new_file(name, self.file_browser.get_target_dir())

    def on_new_directory(self, action, param):
        response, name = self.ask_name("Directory")
        if response == Gtk.ResponseType.OK and name != '':
            self.site.new_directory(name, self.file_browser.get_target_dir())

    def on_add_file(self, action, param):
        chooser = Gtk.FileChooserDialog('Add File', parent=self.window,
            action=Gtk.FileChooserAction.OPEN,
            buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                Gtk.STOCK_OK, Gtk.ResponseType.OK))
        chooser.set_default_response(Gtk.ResponseType.OK)
        response = chooser.run()
        filename = chooser.get_filename()
        chooser.destroy()

        if response == Gtk.ResponseType.OK:
            self.site.add_file(filename, self.file_browser.get_target_dir())

    def ask_name(self, title):
        return util.input_dialog(self.window, "New " + title, "Name:",
                                 "Create")

    def display_preview(self, action, param):
        path = self.site.get_url_path()
        open_browser("http://localhost:%s/%s" % (self.server.port, path))

    def display_terminal(self, action, param):
        path = self.site.directory
        open_terminal(path)

    def on_generate(self, action, param):
        self.site.generate()

    def on_upload(self, action, param):
        dlg = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.QUESTION, 0,
            'Should the site be generated before uploading?')
        dlg.format_secondary_text('''If you made some changes to the site and didn't generate it after that, you should generate it now.''')
        dlg.add_button("Don't generate", Gtk.ResponseType.NO)
        dlg.add_button('Generate', Gtk.ResponseType.YES)
        response = dlg.run()
        dlg.destroy()

        if response == Gtk.ResponseType.YES:
            self.on_generate(None, None)

        # Start uploading
        process = upload.start_upload(self.site)

        if not process:
            dlg = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.ERROR,
                Gtk.ButtonsType.OK, 'Uploading is not configured')
            dlg.run()
            dlg.destroy()
            return

        # Create upload progress dialog
        self.upload_dlg = Gtk.Dialog('Upload', self.window,
            buttons=(Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE))
        self.upload_dlg.set_response_sensitive(Gtk.ResponseType.CLOSE, False)
        self.upload_dlg.set_default_size(500, 310)

        # Box with contents of dialog
        vbox = Gtk.VBox()
        vbox.set_border_width(6)
        vbox.set_spacing(6)

        self.progress_bar = Gtk.ProgressBar()
        vbox.pack_start(self.progress_bar, False, False, 0)

        self.upload_output = Gtk.TextView()
        scrolled = Gtk.ScrolledWindow()
        scrolled.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.ALWAYS)
        scrolled.add(self.upload_output)
        vbox.pack_start(scrolled, True, True, 0)

        self.upload_dlg.vbox.pack_start(vbox, True, True, 0)
        self.upload_dlg.show_all()

        GLib.timeout_add(100, self.check_upload, process)

        self.upload_dlg.run()
        self.upload_dlg.destroy()

    def check_upload(self, process):
        """Check upload status and move progressbar

        This function is called periodically by gobject timer
        """
        returncode, output = upload.check_upload(process)
        self.upload_output.get_buffer().insert_at_cursor(output)
        if returncode is None:
            self.progress_bar.pulse()
            return True
        else:
            self.progress_bar.set_fraction(1.0)
            self.upload_dlg.set_response_sensitive(Gtk.ResponseType.CLOSE, True)
            return False

    def show_site_settings(self, action, param):
        if self.settings_dialog is None:
            self.settings_dialog = SiteSettingsDialog(self.site,
                                                      self.window)
        self.settings_dialog.run()