mental / minicomic

A tool for assembling web and print comics.

This URL has Read+Write access

mental (author)
Thu Jun 26 17:22:49 -0700 2008
commit  8b3e663b9361f1b051dad25b94450761c3891321
tree    192f10a266f304ad4c7451f81e53c16c591d34b4
parent  7c4e4c2530af46f7e98f9aea0c837c0c8b021ddd
minicomic / lib / minicomic / ui / preferences.rb
100644 93 lines (76 sloc) 1.844 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require 'fileutils'
require 'set'
require 'yaml'
 
require 'minicomic/util/observable'
 
module Minicomic
module UI
 
class Preferences < Util::Observable
  observer_method :recent_projects_changed, 1 # projects
 
  PREFERENCES_FILENAME = 'preferences.yaml'
  MAX_RECENT_PROJECTS = 10
 
  def initialize(sequencer, profile_dir)
    super(sequencer)
 
    @profile_dir = profile_dir.to_s.dup
    FileUtils.mkdir_p @profile_dir
    @filename = File.join(@profile_dir, PREFERENCES_FILENAME)
 
    @flush_needed = false
    begin
      @preferences = File.open(@filename, "r") { |stream|
        YAML.load(stream)
      }
    rescue Exception
      @preferences = {}
    end
  end
 
  def queue_flush
    unless @flush_needed
      @flush_needed = true
      flush
    end
  end
  private :queue_flush
 
  def do_flush
    if @flush_needed
      File.open(@filename, "w") do |stream|
        YAML.dump(@preferences, stream)
      end
      @flush_needed = false
    end
  end
  private :do_flush
 
  def flush
    later { do_flush }
  end
 
  def shutdown
    later { do_flush }
  end
 
  def recent_projects
    @preferences['recent_projects'] ||= []
  end
  private :recent_projects
 
  def add_recent_project(path)
    later do
      queue_flush
      projects = recent_projects
      old_index = projects.index path
      projects.delete_at old_index if old_index
      projects.unshift path
      projects.pop while projects.size > MAX_RECENT_PROJECTS
      @observers.recent_projects_changed(projects.dup.freeze)
    end
  end
 
  def clear_recent_projects
    later do
      queue_flush
      projects = recent_projects
      size = projects.size
      projects.clear
      @observers.recent_projects_changed([].freeze)
    end
  end
 
  def update_new_observer(observer)
    observer.recent_projects_changed(recent_projects.dup.freeze)
  end
end
 
end
end