~openerp-commiter/openobject-addons/extra-6.0

« back to all changes in this revision

Viewing changes to openerp-outlook-plugin/manager.py

  • Committer: pap(openerp)
  • Date: 2010-01-11 13:58:16 UTC
  • Revision ID: pap@tinyerp.co.in-20100111135816-oxe0n3xsst2me3vu
[ADD]:added new module openerp-outlook-plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import sys
 
3
import win32api, win32con, win32gui
 
4
import win32com.client
 
5
import win32com.client.gencache
 
6
import pythoncom
 
7
 
 
8
try:
 
9
    True, False
 
10
except NameError:
 
11
    # Maintain compatibility with Python 2.2
 
12
    True, False = 1, 0
 
13
 
 
14
try:
 
15
    filesystem_encoding = sys.getfilesystemencoding()
 
16
except AttributeError:
 
17
    filesystem_encoding = "mbcs"
 
18
 
 
19
# Work out our "application directory", which is
 
20
# the directory of our main .py/.dll/.exe file we
 
21
# are running from.
 
22
if hasattr(sys, "frozen"):
 
23
    assert sys.frozen == "dll", "outlook only supports inproc servers"
 
24
    this_filename = win32api.GetModuleFileName(sys.frozendllhandle)
 
25
else:
 
26
    try:
 
27
        this_filename = os.path.abspath(__file__)
 
28
    except NameError: # no __file__ - means Py2.2 and __name__=='__main__'
 
29
        this_filename = os.path.abspath(sys.argv[0])
 
30
# See if we can use the new bsddb module. (The old one is unreliable
 
31
# on Windows, so we don't use that)
 
32
try:
 
33
    import bsddb3 as bsddb
 
34
    # bsddb3 is definitely not broken
 
35
    use_db = True
 
36
except ImportError:
 
37
    # Not using the 3rd party bsddb3, so try the one in the std library
 
38
    try:
 
39
        import bsddb
 
40
        use_db = hasattr(bsddb, "db") # This name is not in the old one.
 
41
    except ImportError:
 
42
        # No DB library at all!
 
43
        assert not hasattr(sys, "frozen"), \
 
44
               "Don't build binary versions without bsddb!"
 
45
        use_db = False
 
46
 
 
47
# Our main "bayes manager"
 
48
 
 
49
class OpenERPManager:
 
50
    def __init__(self, config_base="default", outlook=None, verbose=0):
 
51
        self.outlook = outlook
 
52
        self.dialog_parser = None
 
53
        self.application_directory = os.path.dirname(this_filename)
 
54
        self.windows_data_directory = self.LocateDataDirectory()
 
55
        self.data_directory = self.windows_data_directory
 
56
        self.default_objects = [('Partners','res.partner',''),('Partner Address','res.partner.address',''), \
 
57
                               ('Account Invoices','account.invoice',''), ('Accounts','account.account',''), \
 
58
                               ('Projects', 'project.project',''),('Sale Orders','sale.order',''), \
 
59
                               ('CRM Cases', 'crm.case',''),('Project Tasks','project.task',''), ('Products', 'product.product', '')]
 
60
        self.config=self.LoadConfig()
 
61
 
 
62
    def WorkerThreadStarting(self):
 
63
        pythoncom.CoInitialize()
 
64
 
 
65
    def WorkerThreadEnding(self):
 
66
        pythoncom.CoUninitialize()
 
67
 
 
68
    def LocateDataDirectory(self):
 
69
        # Locate the best directory for our data files.
 
70
        from win32com.shell import shell, shellcon
 
71
        try:
 
72
            appdata = shell.SHGetFolderPath(0,shellcon.CSIDL_APPDATA,0,0)
 
73
            path = os.path.join(appdata, "OpenERP-Plugin")
 
74
            if not os.path.isdir(path):
 
75
                os.makedirs(path)
 
76
            return path
 
77
        except pythoncom.com_error:
 
78
            # Function doesn't exist on early win95,
 
79
            # and it may just fail anyway!
 
80
            return self.application_directory
 
81
        except EnvironmentError:
 
82
            # Can't make the directory.
 
83
            return self.application_directory
 
84
 
 
85
    def ShowManager(self, id="IDD_MANAGER"):
 
86
        import dialogs
 
87
        dialogs.ShowDialog(0, self, self.config, id)
 
88
 
 
89
    def LoadConfig(self):
 
90
        path = os.path.join(self.data_directory, 'tiny.ini')
 
91
        data = {'server' : 'localhost', 'port' : '8069', 'database' : '', 'objects' : self.default_objects, 'uname':'admin', 'pwd':'a', 'login':False}
 
92
        if os.path.exists(path):
 
93
            fp = open(path, 'r')
 
94
            data = fp.readlines()
 
95
            try:
 
96
                data = eval(data[0])
 
97
                return data
 
98
            except:
 
99
                return data
 
100
        else:
 
101
            return data
 
102
 
 
103
    def SaveConfig(self):
 
104
        path = os.path.join(self.data_directory, 'tiny.ini')
 
105
        fp = open(path, 'w')
 
106
        fp.write(str(self.config))
 
107
        fp.close()
 
108
_mgr = None
 
109
 
 
110
def GetManager(outlook = None):
 
111
    global _mgr
 
112
    if _mgr is None:
 
113
        if outlook is None:
 
114
            outlook = win32com.client.Dispatch("Outlook.Application")
 
115
        _mgr = OpenERPManager(outlook=outlook)
 
116
    return _mgr
 
117
 
 
118
def ShowManager(mgr):
 
119
    mgr.ShowManager()
 
120
 
 
121
def main(verbose_level = 1):
 
122
    mgr = GetManager()
 
123
    ShowManager(mgr)
 
124
    return 0
 
125
 
 
126
def usage():
 
127
    print "Usage: manager [-v ...]"
 
128
    sys.exit(1)
 
129
 
 
130
if __name__=='__main__':
 
131
    verbose = 1
 
132
    import getopt
 
133
    opts, args = getopt.getopt(sys.argv[1:], "v")
 
134
    if args:
 
135
        usage()
 
136
    for opt, val in opts:
 
137
        if opt=="-v":
 
138
            verbose += 1
 
139
        else:
 
140
            usage()
 
141
    sys.exit(main(verbose))