gosuki/browsers.go

96 lines
2.1 KiB
Go
Raw Normal View History

2017-11-19 16:02:53 +00:00
package main
2017-11-19 20:28:24 +00:00
import (
2017-11-20 15:05:44 +00:00
"fmt"
"path"
2017-11-19 20:28:24 +00:00
2017-11-20 15:05:44 +00:00
"github.com/fsnotify/fsnotify"
2017-11-19 20:28:24 +00:00
)
2017-11-19 19:46:24 +00:00
type BrowserType uint8
2017-11-19 16:02:53 +00:00
2017-11-20 15:05:44 +00:00
// Browser types
2017-11-19 16:02:53 +00:00
const (
2017-11-20 15:05:44 +00:00
TChrome BrowserType = iota
TFirefox
2017-11-19 16:02:53 +00:00
)
2017-11-19 20:28:24 +00:00
2017-11-20 15:05:44 +00:00
// Chrome details
var Chrome = struct {
BookmarkFile string
BookmarkDir string
}{
"Bookmarks",
"/home/spike/.config/google-chrome/Default/",
}
type IWatchable interface {
Watch() bool
Watcher() *fsnotify.Watcher // returns browser watcher
Parse() // Main parsing method
GetPath() string //returns bookmark path
GetDir() string // returns bookmarks dir
}
type IBrowser interface {
SetupWatcher() // Starts watching bookmarks and runs Load on change
Watch() bool
InitBuffer() // init buffer db, should be defered to close after call
Load() // Loads bookmarks to db without watching
2017-11-19 20:28:24 +00:00
//Parse(...ParseHook) // Main parsing method with different parsing hooks
Close() // Gracefully finish work and stop watching
}
2017-11-19 20:42:20 +00:00
// Base browser class serves as reference for implmented browser types
// Browser should contain enough data internally to not rely on any global
// variable or constant if possible.
2017-11-20 15:05:44 +00:00
// To create new browsers, you must implement a New<BrowserType>() function
2017-11-19 20:42:20 +00:00
2017-11-19 20:28:24 +00:00
type BaseBrowser struct {
2017-11-20 15:05:44 +00:00
watcher *fsnotify.Watcher
baseDir string
bkFile string
bufferDB *DB
stats *ParserStats
bType BrowserType
name string
isWatching bool
}
func (bw *BaseBrowser) Watcher() *fsnotify.Watcher {
return bw.watcher
}
func (bw *BaseBrowser) Load() {
log.Debug("BaseBrowser Load()")
}
func (bw *BaseBrowser) GetPath() string {
return path.Join(bw.baseDir, bw.bkFile)
}
func (bw *BaseBrowser) GetDir() string {
return bw.baseDir
}
func (bw *BaseBrowser) SetupWatcher() {
var err error
bw.watcher, err = fsnotify.NewWatcher()
logPanic(err)
err = bw.watcher.Add(bw.baseDir)
logPanic(err)
}
func (bw *BaseBrowser) Close() {
err := bw.watcher.Close()
bw.bufferDB.Close()
logPanic(err)
2017-11-19 20:28:24 +00:00
}
2017-11-20 15:05:44 +00:00
func (b *BaseBrowser) InitBuffer() {
bufferName := fmt.Sprintf("buffer_%s", b.name)
bufferPath := fmt.Sprintf(DBBufferFmt, bufferName)
b.bufferDB = DB{}.New(bufferName, bufferPath)
b.bufferDB.Init()
2017-11-19 20:28:24 +00:00
}