1 # Copyright (c) 2008 David Aguilar
2 """This handles saving complex settings such as bookmarks, etc.
10 from cola
import resources
11 from cola
.compat
import json
28 class Settings(object):
29 _file
= resources
.config_home('settings')
30 bookmarks
= property(lambda self
: mklist(self
.values
['bookmarks']))
31 gui_state
= property(lambda self
: mkdict(self
.values
['gui_state']))
32 recent
= property(lambda self
: mklist(self
.values
['recent']))
34 def __init__(self
, verify
=git
.is_git_worktree
):
35 """Load existing settings if they exist"""
45 def remove_missing(self
):
46 missing_bookmarks
= []
49 for bookmark
in self
.bookmarks
:
50 if not self
.verify(bookmark
):
51 missing_bookmarks
.append(bookmark
)
53 for bookmark
in missing_bookmarks
:
55 self
.bookmarks
.remove(bookmark
)
59 for recent
in self
.recent
:
60 if not self
.verify(recent
):
61 missing_recent
.append(recent
)
63 for recent
in missing_recent
:
65 self
.recent
.remove(recent
)
69 def add_bookmark(self
, bookmark
):
70 """Adds a bookmark to the saved settings"""
71 if bookmark
not in self
.bookmarks
:
72 self
.bookmarks
.append(bookmark
)
74 def remove_bookmark(self
, bookmark
):
75 """Removes a bookmark from the saved settings"""
76 if bookmark
in self
.bookmarks
:
77 self
.bookmarks
.remove(bookmark
)
79 def add_recent(self
, entry
):
80 if entry
in self
.recent
:
81 self
.recent
.remove(entry
)
82 self
.recent
.insert(0, entry
)
83 if len(self
.recent
) > 8:
92 parent
= os
.path
.dirname(path
)
93 if not core
.isdir(parent
):
95 with core
.xopen(path
, 'wb') as fp
:
96 json
.dump(self
.values
, fp
, indent
=4)
98 sys
.stderr
.write('git-cola: error writing "%s"\n' % path
)
101 self
.values
.update(self
._load
())
105 if not core
.exists(path
):
106 return self
._load
_dot
_cola
()
108 fp
= core
.xopen(path
, 'rb')
109 return mkdict(json
.load(fp
))
113 def reload_recent(self
):
114 values
= self
._load
()
115 self
.values
['recent'] = mklist(values
.get('recent', []))
117 def _load_dot_cola(self
):
119 path
= os
.path
.join(core
.expanduser('~'), '.cola')
120 if not core
.exists(path
):
123 with core
.xopen(path
, 'r') as fp
:
124 json_values
= json
.load(fp
)
128 # Keep only the entries we care about
129 for key
in self
.values
:
131 values
[key
] = json_values
[key
]
137 def save_gui_state(self
, gui
):
138 """Saves settings for a cola view"""
140 self
.gui_state
[name
] = mkdict(gui
.export_state())
143 def get_gui_state(self
, gui
):
144 """Returns the state for a gui"""
146 state
= mkdict(self
.gui_state
[gui
.name()])
148 state
= self
.gui_state
[gui
.name()] = {}