cola: add more documentation strings to the cola modules
[git-cola.git] / cola / controllers / remote.py
blob1ebd19a3041b79ece9986b0d373343a2e84ed7a2
1 """This controller handles the remote dialog."""
4 import os
5 from PyQt4.QtGui import QDialog
7 from cola import utils
8 from cola import qtutils
9 from cola.views import RemoteView
10 from cola.qobserver import QObserver
12 def remote_action(model, parent, action):
13 model = model.clone()
14 model.create(remotename='',
15 tags_checkbox=False,
16 ffwd_only_checkbox=True)
17 view = RemoteView(parent, action)
18 if action == 'Fetch' or action == 'Pull':
19 model.set_tags_checkbox(False)
20 if action == 'Pull':
21 view.tags_checkbox.hide()
22 controller = RemoteController(model, view, action)
23 view.show()
24 return view.exec_() == QDialog.Accepted
26 class RemoteController(QObserver):
27 def init(self, model, view, action):
28 self.add_observables('remotename',
29 'remotes',
30 'local_branch',
31 'local_branches',
32 'remote_branch',
33 'remote_branches',
34 'tags_checkbox',
35 'ffwd_only_checkbox')
36 self.action_method = {
37 'Fetch': self.gen_remote_callback(self.model.fetch_helper),
38 'Push': self.gen_remote_callback(self.model.push_helper),
39 'Pull': self.gen_remote_callback(self.model.pull_helper),
40 } [action]
42 self.add_actions(remotes = self.display_remotes)
43 self.add_callbacks(action_button = self.action_method,
44 remotes = self.update_remotes,
45 local_branches = self.update_local_branches,
46 remote_branches = self.update_remote_branches)
47 self.refresh_view()
48 if self.view.select_first_remote():
49 self.model.set_remotename(self.model.get_remotes()[0])
51 def display_remotes(self, widget):
52 displayed = []
53 for remotename in self.model.get_remotes():
54 url = self.model.remote_url(remotename)
55 display = ('%s\t(%s %s)'
56 % (remotename, unicode(self.tr('URL:')), url))
57 displayed.append(display)
58 qtutils.set_items(widget,displayed)
60 def update_remotes(self,*rest):
61 widget = self.view.remotes
62 remotes = self.model.get_remotes()
63 selection = qtutils.get_selected_item(widget, remotes)
64 if not selection:
65 return
66 self.model.set_remotename(selection)
67 self.view.remotename.selectAll()
69 def update_local_branches(self,*rest):
70 branches = self.model.get_local_branches()
71 widget = self.view.local_branches
72 selection = qtutils.get_selected_item(widget, branches)
73 if not selection:
74 return
76 self.model.set_local_branch(selection)
77 self.model.set_remote_branch(selection)
79 self.view.local_branch.selectAll()
80 self.view.remote_branch.selectAll()
82 def update_remote_branches(self,*rest):
83 widget = self.view.remote_branches
84 branches = self.model.get_remote_branches()
85 selection = qtutils.get_selected_item(widget,branches)
86 if not selection:
87 return
88 branch = utils.basename(selection)
89 if branch == 'HEAD':
90 return
91 self.model.set_remote_branch(branch)
92 self.view.remote_branch.selectAll()
94 def check_remote(self):
95 if not self.model.get_remotename():
96 errmsg = self.tr('No repository selected.')
97 qtutils.show_output(errmsg)
98 return False
99 else:
100 return True
102 def get_common_args(self):
103 return (self.model.get_remotename(),
105 'local_branch': self.model.get_local_branch(),
106 'remote_branch': self.model.get_remote_branch(),
107 'ffwd': self.model.get_ffwd_only_checkbox(),
108 'tags': self.model.get_tags_checkbox(),
111 def show_results(self, output):
112 qtutils.show_output(output)
113 self.view.accept()
114 qtutils.raise_logger()
116 #+-------------------------------------------------------------
117 #+ Actions
118 def gen_remote_callback(self, modelaction):
119 """Generates a Qt callback for fetch/push/pull.
121 def remote_callback():
122 if not self.check_remote():
123 return
124 remote, kwargs = self.get_common_args()
125 output = modelaction(remote, **kwargs)
126 if not output: # git fetch --tags --verbose doesn't print anything...
127 output = self.tr('Already up-to-date.')
128 self.show_results(output)
129 return remote_callback