ui: turn the push controller into a generic remote controller
[ugit.git] / ugit / controllers / push.py
blobeba18a7e28ea5278b2d0fa20466320c6f75cdc06
1 import os
2 from PyQt4.QtGui import QDialog
4 from ugit import utils
5 from ugit import qtutils
6 from ugit.views import PushView
7 from ugit.qobserver import QObserver
9 def push_branches(model, parent):
10 model = model.clone()
11 model.create( remotename='' )
12 view = PushView(parent)
13 controller = PushController(model, view)
14 view.show()
15 return view.exec_() == QDialog.Accepted
17 class PushController(QObserver):
18 def init(self, model, view):
19 self.add_observables(
20 'remotename',
21 'remotes',
22 'local_branch',
23 'local_branches',
24 'remote_branch',
25 'remote_branches',
27 self.add_actions( remotes = self.display_remotes )
28 self.add_callbacks(
29 remotes = self.update_remotes,
30 local_branches = self.update_local_branches,
31 remote_branches = self.update_remote_branches,
32 push_button = self.push_to_remote_branch,
34 self.refresh_view()
36 def display_remotes(self, widget):
37 displayed = []
38 for remotename in self.model.get_remotes():
39 url = self.model.remote_url(remotename)
40 display = '%s\t(%s %s)' \
41 % (remotename, unicode(self.tr('URL:')), url)
42 displayed.append(display)
43 qtutils.set_items(widget,displayed)
45 def push_to_remote_branch(self):
46 if not self.model.get_remotename():
47 errmsg = self.tr('No repository selected.')
48 qtutils.show_output(errmsg)
49 return
51 if not self.model.get_remote_branch():
52 errmsg = self.tr('Please supply a branch name.')
53 qtutils.show_output(errmsg)
54 return
56 if not self.model.get_local_branch():
57 msg = self.tr('Pushing with an empty local branch '
58 + 'will remove the remote branch.\n'
59 + 'Continue?')
60 if not qtutils.question(self.view, self.tr('warning'), msg):
61 return
63 remotename = self.model.get_remotename()
64 local_branch = self.model.get_local_branch()
65 remote_branch = self.model.get_remote_branch()
66 ffwd = self.view.ffwd_only_checkbox.isChecked()
67 tags = self.view.tags_checkbox.isChecked()
69 status, output = self.model.push_helper(
70 remotename,
71 local_branch,
72 remote_branch,
73 ffwd=ffwd,
74 tags=tags
76 qtutils.show_output(output)
77 if not status:
78 self.view.accept()
80 def update_remotes(self,*rest):
81 widget = self.view.remotes
82 remotes = self.model.get_remotes()
83 selection = qtutils.get_selected_item(widget,remotes)
84 if not selection: return
85 self.model.set_remotename(selection)
86 self.view.remotename.selectAll()
88 def update_local_branches(self,*rest):
89 branches = self.model.get_local_branches()
90 widget = self.view.local_branches
91 selection = qtutils.get_selected_item(widget,branches)
92 if not selection: return
94 self.model.set_local_branch(selection)
95 self.model.set_remote_branch(selection)
97 self.view.local_branch.selectAll()
98 self.view.remote_branch.selectAll()
100 def update_remote_branches(self,*rest):
101 widget = self.view.remote_branches
102 branches = self.model.get_remote_branches()
103 selection = qtutils.get_selected_item(widget,branches)
104 if not selection: return
106 branch = utils.basename(selection)
107 if branch == 'HEAD': return
108 self.model.set_remote_branch(branch)
109 self.view.remote_branch.selectAll()