-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.rb
More file actions
252 lines (183 loc) · 4.26 KB
/
Copy pathcontroller.rb
File metadata and controls
252 lines (183 loc) · 4.26 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# Work logger
#
# Copyright (c) 2009, Anja Berens
#
# You can redistribute it and/or modify it under the terms of
# the GPL's licence.
#
# With much thanks to "Masao Mutoh" for his "Simple Text Editor"
# which provided the inspiration and hints/guideline for this project
#
require 'gtk2'
require 'config'
module Work
class Controller
def initialize
@date = date_today
# Text changed indicators
@text_changed = false
@user_action = false
# Load up the config
@config = Config.new
# Scan the filetype directory for each filetype and register/load it
@file_type = Hash.new
Dir.glob("filetype/*.rb") do |file|
load file
filetype = /.*\/(.*)\.[Rr][Bb]/.match(file)[1]
tmp = eval("Filetype::#{filetype.capitalize}.new")
@file_type[tmp.file_type] = tmp
puts "Loaded support for: #{filetype}"
end
# Load the "last file" type driver code
if @config['file'] != nil
filetype = @config['file']['type']
if filetype != nil
@db = @file_type[filetype]
else
puts "Got a directory loading for filetype working..."
puts "Exiting program for now, because adding file types support"
puts "isn't an easy task so exiting for now"
exit 1
end
end
end
attr_accessor :text_changed, :user_action
def set_view(view)
@view = view
# At this point its not exactly the most ideal spot to put this
# code, but...
# Loads the last open file
if @config['file'] != nil
file = @config['file']['last']
if not (file.nil?)
if File.file?(file)
if File.readable?(file)
puts "Loading: #{file}"
open_database(file)
@view.sensitive
end
end
end
end
end
def date=(date)
text_changed?
@date = date
@view.date_update(@date)
result = @db.fetch_text_entry(@date)
if !result.nil?
@view.update_textview(result)
end
end
def date_today
text_changed?
@date = Date.today(Date::ENGLAND)
if !@view.nil?
@view.date_update(@date)
result = @db.fetch_text_entry(@date)
if !result.nil?
@view.update_textview(result)
end
end
return @date
end
def date_back
text_changed?
@date = @date - 1
@view.date_update(@date)
result = @db.fetch_text_entry(@date)
if !result.nil?
@view.update_textview(result)
end
return @date
end
def date_forward
text_changed?
@date = @date + 1
@view.date_update(@date)
result = @db.fetch_text_entry(@date)
if !result.nil?
@view.update_textview(result)
end
return @date
end
def new_database(filename)
if @db.open?
text_changed?
@db.close
end
@db.create(filename)
if @config['file'] != nil
@config['file']['last'] = filename
@config['file']['type'] = @db.file_type
end
@view.update_textview("")
end
def open_database(filename)
if @db.open?
text_changed?
@db.close
end
@db.open(filename)
# If last_file is nil or not the same then store the opened file
if @config['file'] != nil
if @config['file']['last'] == nil
@config['file']['last'] = filename
@config['file']['type'] = @db.file_type
elsif @config['file']['last'] != filename
@config['file']['last'] = filename
@config['file']['type'] = @db.file_type
end
end
@view.date_update(date_today)
result = @db.fetch_text_entry(@date)
if !result.nil?
@view.update_textview(result)
end
end
def close_database
if @db.open?
text_changed?
@db.close
@view.update_textview("")
end
end
def save_entry(text)
@db.store_text_entry(@date, text)
end
def text_changed?
if (@text_changed and @user_action)
if @view.save_text?
save_entry(@view.get_text)
end
end
end
def file_regex
ret = Array.new
@file_type.each_value do |db|
ret.push(db.file_regex)
end
return ret
end
def santize_filename(filename)
if ((filename != nil) && !(filename.empty?))
@file_type.each_value do |db|
if filename.match("#{db.file_type}")
@db = db
return filename
end
end
begin
return @db.check_filename(filename)
rescue
return nil
end
end
return nil
end
def quit
close_database
@config.save_config
end
end
end