controllers: add a remote controller
[ugit.git] / ugit / controllers / remote.py
blobd2606c2a4bd20370116d78ab2411834cea205930
1 import os
2 from PyQt4.QtGui import QDialog
4 from ugit import utils
5 from ugit import qtutils
6 from ugit.views import RemoteView
7 from ugit.qobserver import QObserver
9 def remote_action(model, parent, action):
10 model = model.clone()
11 model.create(
12 remotename='',
13 tags_checkbox=False,
14 ffwd_only_checkbox=True,
16 if action == "Fetch" or action == "Pull":
17 model.set_tags_checkbox(True)
18 view = RemoteView(parent, action)
19 controller = RemoteController(model, view, action)
20 view.show()
21 return view.exec_() == QDialog.Accepted
23 class RemoteController(QObserver):
24 def init(self, model, view, action):
25 self.add_observables(
26 'remotename',
27 'remotes',
28 'local_branch',
29 'local_branches',
30 'remote_branch',
31 'remote_branches',
32 'tags_checkbox',
33 'ffwd_only_checkbox',
36 self.action_method = {
37 "Fetch": self.fetch_action,
38 "Push": self.push_action,
39 "Pull": self.pull_action,
40 }[action]
42 self.add_actions( remotes = self.display_remotes )
43 self.add_callbacks(
44 action_button = self.action_method,
45 remotes = self.update_remotes,
46 local_branches = self.update_local_branches,
47 remote_branches = self.update_remote_branches,
49 self.refresh_view()
50 if self.view.select_first_remote():
51 self.model.set_remotename(self.model.get_remotes()[0])
53 def display_remotes(self, widget):
54 displayed = []
55 for remotename in self.model.get_remotes():
56 url = self.model.remote_url(remotename)
57 display = '%s\t(%s %s)' \
58 % (remotename, unicode(self.tr('URL:')), url)
59 displayed.append(display)
60 qtutils.set_items(widget,displayed)
62 def update_remotes(self,*rest):
63 widget = self.view.remotes
64 remotes = self.model.get_remotes()
65 selection = qtutils.get_selected_item(widget,remotes)
66 if not selection: return
67 self.model.set_remotename(selection)
68 self.view.remotename.selectAll()
70 def update_local_branches(self,*rest):
71 branches = self.model.get_local_branches()
72 widget = self.view.local_branches
73 selection = qtutils.get_selected_item(widget,branches)
74 if not selection: 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: return
88 branch = utils.basename(selection)
89 if branch == 'HEAD': return
90 self.model.set_remote_branch(branch)
91 self.view.remote_branch.selectAll()
93 def check_remote(self):
94 if not self.model.get_remotename():
95 errmsg = self.tr('No repository selected.')
96 qtutils.show_output(errmsg)
97 return False
98 else:
99 return True
101 def get_common_args(self):
102 return (
103 (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(),
112 def show_results(self, status, output):
113 if not status:
114 qtutils.log(output)
115 self.view.accept()
116 else:
117 qtutils.raise_logger()
119 #+-------------------------------------------------------------
120 #+ Actions
121 def fetch_action(self):
122 if not self.check_remote():
123 return
124 args, kwargs = self.get_common_args()
125 self.show_results(*self.model.fetch_helper(*args, **kwargs))
126 return
128 def pull_action(self):
129 if not self.check_remote():
130 return
131 args, kwargs = self.get_common_args()
132 self.show_results(*self.model.pull_helper(*args, **kwargs))
134 def push_action(self):
135 if not self.check_remote():
136 return
137 args, kwargs = self.get_common_args()
138 self.show_results(*self.model.push_helper(*args, **kwargs))