~ubuntu-branches/ubuntu/maverick/gui-ufw/maverick

« back to all changes in this revision

Viewing changes to instance.py

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2010-03-15 22:19:04 UTC
  • mfrom: (1.1.15 upstream)
  • Revision ID: james.westby@ubuntu.com-20100315221904-bsiaax6ayc9j124o
Tags: 10.04.2-0ubuntu1
* New upstream release (LP: #538649).
* Do not install /etc/gufw/*.cfg anymore, only remove /etc/gufw/gufw.cfg if
  needed (remove debian/postinst).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Gufw 10.04.1 - http://gufw.tuxfamily.org
2
 
# Copyright (C) 2009 Raul Soriano & Marcos Alvarez Costales
3
 
#
4
 
# Gufw is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; either version 3 of the License, or
7
 
# (at your option) any later version.
8
 
9
 
# Gufw is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with Gufw; if not, see http://www.gnu.org/licenses for more
16
 
# information.
17
 
 
18
 
 
19
 
import gtk
20
 
import os
21
 
import os.path
22
 
from model.Variable import Variable
23
 
 
24
 
 
25
 
variable = Variable()
26
 
 
27
 
    
28
 
# Check if gufw is root application
29
 
class Check:
30
 
 
31
 
    def is_root(self):
32
 
    
33
 
        if os.geteuid() == 0:
34
 
            return True
35
 
        else:
36
 
            dlg = gtk.MessageDialog(None, buttons=gtk.BUTTONS_CLOSE, message_format=variable.get_text("018"))
37
 
            dlg.run()
38
 
            dlg.destroy()
39
 
            return False
40
 
        
41
 
        
42
 
# This class manages application instances
43
 
class Instance:
44
 
 
45
 
    #specify the file where the pid is stores (pid file)
46
 
    def __init__(self):
47
 
        self.pid_file = variable.get_path("pid_file")
48
 
        self.check()
49
 
        self.startApplication()
50
 
 
51
 
    #check wether the app is running
52
 
    def check(self):
53
 
        #check wether the pid file exists
54
 
        if not os.path.isfile(self.pid_file):
55
 
            return
56
 
 
57
 
        #read the pid from file
58
 
        pid = 0
59
 
        try:
60
 
            file = open(self.pid_file, 'rt')
61
 
            data = file.read()
62
 
            file.close()
63
 
            pid = int(data)
64
 
        except:
65
 
            pass
66
 
 
67
 
        #check wether the proccess specified exists
68
 
        if 0 == pid:
69
 
            return
70
 
        try:
71
 
            os.kill(pid, 0)  #this raises an exception if the pid is invalid
72
 
        except:
73
 
            return
74
 
 
75
 
        #exit the program
76
 
        exit(0) # exit reaises an exception, so there is no need for a try/except block
77
 
 
78
 
    #called when there is no running instances, storing the new pid (creating a instance)
79
 
    def startApplication(self):
80
 
        file = open(self.pid_file, 'wt')
81
 
        file.write( str(os.getpid()))
82
 
        file.close()
83
 
 
84
 
    #called when the running insnstance exits, removing the existing pid file (destroying the existing instance)
85
 
    def exitApplication(self):
86
 
    # Close WindowsxitApplication(self):
87
 
        try:
88
 
            os.remove(self.pid_file)
89
 
        except:
90
 
            pass