Skip to content

Commit

Permalink
main: Add an 'Open Recent' menu
Browse files Browse the repository at this point in the history
Add a list of recent repositories to the file menu.

Closes #135

Suggested-by: Philip Whitfield (@underdoeg via github.com)
Signed-off-by: David Aguilar <davvid@gmail.com>
  • Loading branch information
davvid committed Jul 27, 2012
1 parent 6cf8081 commit 28e2f40
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
16 changes: 16 additions & 0 deletions cola/main/view.py
Expand Up @@ -18,6 +18,7 @@
from cola import qtcompat
from cola import qt
from cola import resources
from cola import settings
from cola import stash
from cola import utils
from cola import version
Expand Down Expand Up @@ -281,6 +282,8 @@ def __init__(self, model, parent):
self.file_menu.addAction(self.menu_preferences)
self.file_menu.addSeparator()
self.file_menu.addAction(self.menu_open_repo)
self.menu_open_recent = self.file_menu.addMenu(tr('Open Recent'))
self.file_menu.addSeparator()
self.file_menu.addAction(self.menu_clone_repo)
self.file_menu.addAction(self.menu_manage_bookmarks)
self.file_menu.addSeparator()
Expand Down Expand Up @@ -412,6 +415,9 @@ def __init__(self, model, parent):
connect_button(self.stage_button, self.stage)
connect_button(self.unstage_button, self.unstage)

self.connect(self.menu_open_recent, SIGNAL('aboutToShow()'),
self.build_recent_menu)

self.connect(self.commitmsgeditor, SIGNAL('cursorPosition(int,int)'),
self.show_cursor_position)
self.connect(self, SIGNAL('update'), self._update_callback)
Expand All @@ -435,6 +441,16 @@ def closeEvent(self, event):
qtutils.save_state(self)
MainWindow.closeEvent(self, event)

def build_recent_menu(self):
recent = settings.Settings().recent
menu = self.menu_open_recent
menu.clear()
for r in recent:
name = os.path.basename(r)
directory = os.path.dirname(r)
text = u'%s %s %s' % (name, unichr(0x2192), directory)
menu.addAction(text, qtutils.SLOT(signals.open_repo, r))

# Accessors
mode = property(lambda self: self.model.mode)

Expand Down
53 changes: 45 additions & 8 deletions cola/settings.py
Expand Up @@ -10,6 +10,8 @@
except ImportError:
import json

from cola import core


def mkdict(obj):
if type(obj) is dict:
Expand All @@ -25,8 +27,14 @@ def mklist(obj):
return []


def xdg_config_home(*args):
config = os.getenv('XDG_CONFIG_HOME',
os.path.join(os.path.expanduser('~'), '.config'))
return os.path.join(config, 'git-cola', *args)


class Settings(object):
_file = '~/.config/git-cola/settings'
_file = xdg_config_home('settings')

def __init__(self):
"""Load existing settings if they exist"""
Expand All @@ -48,8 +56,16 @@ def _get_gui_state(self):
gui_state = self.values['gui_state'] = {}
return gui_state

def _get_recent(self):
try:
recent = mklist(self.values['recent'])
except KeyError:
recent = self.values['recent'] = []
return recent

bookmarks = property(_get_bookmarks)
gui_state = property(_get_gui_state)
recent = property(_get_recent)

def add_bookmark(self, bookmark):
"""Adds a bookmark to the saved settings"""
Expand All @@ -61,8 +77,15 @@ def remove_bookmark(self, bookmark):
if bookmark in self.bookmarks:
self.bookmarks.remove(bookmark)

def add_recent(self, entry):
if entry in self.recent:
self.recent.remove(entry)
self.recent.insert(0, entry)
if len(self.recent) > 8:
self.recent.pop()

def path(self):
return os.path.expanduser(Settings._file)
return self._file

def save(self):
path = self.path()
Expand All @@ -71,38 +94,52 @@ def save(self):
if not os.path.isdir(parent):
os.makedirs(parent)

self.reload_recent()
self.add_recent(core.decode(os.getcwd()))

fp = open(path, 'wb')
json.dump(self.values, fp, indent=4)
fp.close()
except:
sys.stderr.write('git-cola: error writing "%s"\n' % path)

def load(self):
self.values = self._load()

def _load(self):
path = self.path()
if not os.path.exists(path):
self.load_dot_cola(path)
return
return self.load_dot_cola(path)
try:
fp = open(path, 'rb')
self.values = mkdict(json.load(fp))
return mkdict(json.load(fp))
except: # bad json
return {}

def reload_recent(self):
values = self._load()
try:
self.values['recent'] = mklist(values['recent'])
except KeyError:
pass

def load_dot_cola(self, path):
values = {}
path = os.path.join(os.path.expanduser('~'), '.cola')
if not os.path.exists(path):
return
return values
try:
fp = open(path, 'rb')
values = json.load(fp)
fp.close()
except: # bad json
return
return values
for key in ('bookmarks', 'gui_state'):
try:
self.values[key] = values[key]
values[key] = values[key]
except KeyError:
pass
return values

def save_gui_state(self, gui):
"""Saves settings for a cola view"""
Expand Down
2 changes: 1 addition & 1 deletion test/test_cola_settings.py
Expand Up @@ -8,7 +8,7 @@ class SettingsTestCase(unittest.TestCase):
"""Tests the cola.settings module"""
def setUp(self):
settings.Settings._file = self._file = helper.tmp_path('settings')
settings.Settings.load_dot_cola = lambda x, y: None
settings.Settings.load_dot_cola = lambda x, y: {}

def tearDown(self):
if os.path.exists(self._file):
Expand Down

0 comments on commit 28e2f40

Please sign in to comment.