public
Description: Repository for a PyObjC project built in one of my tutorial series.
Homepage: http://lethain.com/
Clone URL: git://github.com/lethain/metawindow.git
metawindow / MWController.py
100644 101 lines (86 sloc) 3.378 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
#
# MWController.py
# MetaWindow
#
# Created by Will Larson on 8/22/08.
# Copyright (c) 2008 Will Larson. All rights reserved.
#
 
import objc, metaweb, webbrowser, pickle, datetime, md5, threading
from MWRowWindowController import MWRowWindowController
from AppKit import *
from Foundation import *
 
 
class MWController(NSObject):
    tableView = objc.IBOutlet()
    textField = objc.IBOutlet()
    arrayController = objc.IBOutlet()
    indicator = objc.IBOutlet()
    results = []
    rowCache = {}
    _cache = None
    
    def getCache(self):
        if self._cache is None:
            self._cache = NSApp.delegate().cache
        return self._cache
    cache = property(getCache,None,None,"Cache of searches.")
    
    def awakeFromNib(self):
        if self.tableView:
            self.tableView.setTarget_(self)
            self.tableView.setDoubleAction_("open:")
            self.tableView.setDraggingSourceOperationMask_forLocal_(NSDragOperationCopy, False)
            
    def dealloc(self):
        for key in self.rowCache:
            value = self.rowCache[key]
            value.release()
        super(MWController,self).dealloc()
    
    def open_(self,sender):
        selectedObjs = self.arrayController.selectedObjects()
        if len(selectedObjs) == 0:
            NSLog(u"No selected row!")
            return
            
        row = selectedObjs[0]
        NSLog(u"Row: %s" % row)
        if not row.has_key('id') or row['id'] == None:
            NSLog(u"Row has no id!")
            return
        
        if self.rowCache.has_key(row):
            rwc = self.rowCache[row]
            rwc.showWindow_(self)
        else:
            rwc =MWRowWindowController.alloc().initWithWindowNibName_(u"RowWindow")
            rwc.rowDict = row
            rwc.showWindow_(self)
            rwc.retain()
            self.rowCache[row] = rwc
        
    
    def getCachedSearch(self,searchString):
        if self.cache.has_key(searchString):
            filename,timestamp = self.cache[searchString]
            age = datetime.datetime.now() - timestamp
            if age > datetime.timedelta(days=1):
                return None
            filepath = NSApp.delegate().pathForFile(filename)
            file = open(filepath,'r')
            data = pickle.load(file)
            file.close()
            return data
        return None
        
    def cacheResultsForSearch(self,searchString,results):
        filename = u"%s.cached" % md5.md5(searchString).hexdigest()[12:]
        filepath = NSApp.delegate().pathForFile(filename)
        file = open(filepath,'w')
        pickle.dump(results,file)
        file.close()
        self.cache[searchString] = (filename,datetime.datetime.now())
        
    def dragSearch(self,searchString):
        self.textField.setStringValue_(searchString)
        self.search_(self)
    
    @objc.IBAction
    def search_(self,sender):
        search_value = self.textField.stringValue()
        cached = self.getCachedSearch(search_value)
        if cached is None:
            self.indicator.startAnimation_(self)
            cached = metaweb.search(search_value)
            self.cacheResultsForSearch(search_value,cached)
            self.indicator.stopAnimation_(self)
        self.results = [ NSDictionary.dictionaryWithDictionary_(x) for x in cached]
        self.arrayController.rearrangeObjects()