1 # Copyright (c) 2008 David Aguilar
2 """This handles saving complex settings such as bookmarks, etc.
32 class Settings(object):
33 _file
= xdg
.config_home('settings')
35 def __init__(self
, verify
=git
.is_git_worktree
):
36 """Load existing settings if they exist"""
47 def remove_missing(self
):
48 missing_bookmarks
= []
51 for bookmark
in self
.bookmarks
:
52 if not self
.verify(core
.encode(bookmark
)):
53 missing_bookmarks
.append(bookmark
)
55 for bookmark
in missing_bookmarks
:
57 self
.bookmarks
.remove(bookmark
)
61 for recent
in self
.recent
:
62 if not self
.verify(core
.encode(recent
)):
63 missing_recent
.append(recent
)
65 for recent
in missing_recent
:
67 self
.recent
.remove(recent
)
72 def _get_bookmarks(self
):
73 return mklist(self
.values
['bookmarks'])
75 def _get_gui_state(self
):
76 return mkdict(self
.values
['gui_state'])
78 def _get_recent(self
):
79 return mklist(self
.values
['recent'])
81 bookmarks
= property(_get_bookmarks
)
82 gui_state
= property(_get_gui_state
)
83 recent
= property(_get_recent
)
85 def add_bookmark(self
, bookmark
):
86 """Adds a bookmark to the saved settings"""
87 if bookmark
not in self
.bookmarks
:
88 self
.bookmarks
.append(bookmark
)
90 def remove_bookmark(self
, bookmark
):
91 """Removes a bookmark from the saved settings"""
92 if bookmark
in self
.bookmarks
:
93 self
.bookmarks
.remove(bookmark
)
95 def add_recent(self
, entry
):
96 if entry
in self
.recent
:
97 self
.recent
.remove(entry
)
98 self
.recent
.insert(0, entry
)
99 if len(self
.recent
) > 8:
108 parent
= os
.path
.dirname(path
)
109 if not os
.path
.isdir(parent
):
111 fp
= open(path
, 'wb')
112 json
.dump(self
.values
, fp
, indent
=4)
115 sys
.stderr
.write('git-cola: error writing "%s"\n' % path
)
118 self
.values
.update(self
._load
())
122 if not os
.path
.exists(path
):
123 return self
._load
_dot
_cola
()
125 fp
= open(path
, 'rb')
126 return mkdict(json
.load(fp
))
130 def reload_recent(self
):
131 values
= self
._load
()
132 self
.values
['recent'] = mklist(values
.get('recent', []))
134 def _load_dot_cola(self
):
136 path
= os
.path
.join(os
.path
.expanduser('~'), '.cola')
137 if not os
.path
.exists(path
):
140 fp
= open(path
, 'rb')
141 json_values
= json
.load(fp
)
146 # Keep only the entries we care about
147 for key
in self
.values
:
149 values
[key
] = json_values
[key
]
155 def save_gui_state(self
, gui
):
156 """Saves settings for a cola view"""
158 self
.gui_state
[name
] = mkdict(gui
.export_state())
161 def get_gui_state(self
, gui
):
162 """Returns the state for a gui"""
164 state
= mkdict(self
.gui_state
[gui
.name()])
166 state
= self
.gui_state
[gui
.name()] = {}