~alex-k-1/pictoric/pictoric

« back to all changes in this revision

Viewing changes to pictoric/PictoricWindow.py

  • Committer: Alexander Kendrick
  • Date: 2012-07-14 21:05:42 UTC
  • mfrom: (0.1.5 pictoric)
  • Revision ID: alex_k@cybermesa.com-20120714210542-elqukpdjqv7k59xb
Merged with app-review revisions

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
 
### BEGIN LICENSE
3
 
# Copyright (C) 2012 Cole Kendrick colej.kendrick@gmail.com Alexander Kendrick kentalex8@gmail.com
4
 
# This program is free software: you can redistribute it and/or modify it 
5
 
# under the terms of the GNU General Public License version 3, as published 
6
 
# by the Free Software Foundation.
7
 
8
 
# This program is distributed in the hope that it will be useful, but 
9
 
# WITHOUT ANY WARRANTY; without even the implied warranties of 
10
 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
11
 
# PURPOSE.  See the GNU General Public License for more details.
12
 
13
 
# You should have received a copy of the GNU General Public License along 
14
 
# with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
### END LICENSE
16
 
 
17
 
import locale, os, time
18
 
 
19
 
from gi.repository import Gtk, Gio, Gdk # pylint: disable=E0611
20
 
from gi.repository.GdkPixbuf import Pixbuf
21
 
import logging
22
 
logger = logging.getLogger('pictoric')
23
 
 
24
 
TEXTDOMAIN = 'pictoric'
25
 
LOCALEDIR = '/opt/extras.ubuntu.com/pictoric/share/locale'
26
 
from locale import gettext as _
27
 
locale.bindtextdomain(TEXTDOMAIN, LOCALEDIR)
28
 
locale.textdomain(TEXTDOMAIN)
29
 
 
30
 
from pictoric_lib import Window
31
 
 
32
 
class PictoricWindow(Window):
33
 
    __gtype_name__ = "PictoricWindow"
34
 
    SCHEMA = 'org.gnome.desktop.background'
35
 
    KEY = 'picture-uri'
36
 
    minute=60
37
 
    defaultDirectory = '/home/%s/Pictures' % (os.environ['USER'])
38
 
    defaultInterval = minute*5  
39
 
    sourceFolder = defaultDirectory
40
 
    iconStore = Gtk.ListStore(Pixbuf)
41
 
    
42
 
    def finish_initializing(self, builder, sourceFolder=defaultDirectory, sourceInterval=defaultInterval, configPath ='/home/%s/.config/pictoric/pictoric.conf' % (os.environ['USER']), minute=60): # pylint: disable=E1002
43
 
        """Set up the main window"""
44
 
        # self.set_default_size(400,500)
45
 
        autofile = '/home/%s/.config/autostart/pictoric.desktop' % (os.environ['USER'])
46
 
        confdir = '/home/%s/.config/pictoric/' % (os.environ['USER'])
47
 
        runfile = '/home/%s/.config/pictoric/run_pictoric.sh' % (os.environ['USER'])
48
 
        if os.path.exists(confdir):
49
 
            if os.path.isfile(autofile) == False: 
50
 
                print "Writing '%s'" % (autofile)
51
 
                autofile = open("%s" % (autofile), 'w')
52
 
                autofile.write("[Desktop Entry]\n")
53
 
                autofile.write("Type=Application\n")
54
 
                autofile.write("Exec=/home/alex/.config/pictoric/run_pictoric.sh\n")
55
 
                autofile.write("Hidden=false\n")
56
 
                autofile.write("NoDisplay=false\n")
57
 
                autofile.write("X-GNOME-Autostart-enabled=true\n")
58
 
                autofile.write("Name[en_US]=Pictoric\n")
59
 
                autofile.write("Name=Pictoric\n")
60
 
                autofile.write("Comment[en_US]=Start Wallpaper Slideshow\n")
61
 
                autofile.write("Comment=Start Wallpaper Slideshow\n")
62
 
                autofile.close()
63
 
            if os.path.isfile(runfile) == False:
64
 
                print "Writing '%s'" % (runfile)
65
 
                runfile = open("%s" % (runfile), 'w')
66
 
                runfile.write("#!/bin/bash\n")
67
 
                runfile.write("python /opt/extras.ubuntu.com/pictoric/pictoric/PictoricBg.py")
68
 
                runfile.close()
69
 
            if os.path.isfile(configPath) == False: #Tests if config file already exists
70
 
                print "Writing '%s'" % (configPath)
71
 
                config = open("%s" % (configPath), 'w')
72
 
                config.write("%s\n" % (sourceFolder))
73
 
                config.write("%s\n" % (sourceInterval))
74
 
                config.close()
75
 
                if os.path.isfile(configPath) == True: #If exists, open for reading and future writing
76
 
                    config = open("%s" % (configPath), 'r')
77
 
                    sourceFolder = config.readline()
78
 
                    sourceInterval = config.readline()
79
 
                    config.close()
80
 
        else:
81
 
            os.makedirs(confdir)
82
 
            print "Writing '%s'" % (configPath)
83
 
            config = open("%s" % (configPath), 'w')
84
 
            config.write("%s\n" % (sourceFolder))
85
 
            config.write("%s\n" % (sourceInterval))
86
 
            config.close()
87
 
            print "Writing '%s'" % (runfile)
88
 
            runfile = open("%s" % (runfile), 'w')
89
 
            runfile.write("#!/bin/bash\n")
90
 
            runfile.write("python /opt/extras.ubuntu.com/pictoric/pictoric/PictoricBg.py")
91
 
            runfile.close()            
92
 
            if os.path.isfile(autofile) == False:
93
 
                print "Writing '%s'" % (autofile)
94
 
                autofile = open("%s" % (autofile), 'w')
95
 
                autofile.write("[Desktop Entry]\n")
96
 
                autofile.write("Type=Application\n")
97
 
                autofile.write("Exec=/home/alex/.config/pictoric/run_pictoric.sh\n")
98
 
                autofile.write("Hidden=false\n")
99
 
                autofile.write("NoDisplay=false\n")
100
 
                autofile.write("X-GNOME-Autostart-enabled=true\n")
101
 
                autofile.write("Name[en_US]=Pictoric\n")
102
 
                autofile.write("Name=Pictoric\n")
103
 
                autofile.write("Comment[en_US]=Start Wallpaper Slideshow\n")
104
 
                autofile.write("Comment=Start Wallpaper Slideshow\n")
105
 
                autofile.close()
106
 
            
107
 
        super(PictoricWindow, self).finish_initializing(builder)
108
 
 
109
 
    def changer(self,dir_path,sourceInterval=defaultInterval, minute=60):
110
 
        print "Starting wallpaper slideshow with folder->'%s' and interval->%s" % (dir_path,sourceInterval)
111
 
        gsettings = Gio.Settings.new(self.SCHEMA)
112
 
        for name in os.listdir(dir_path):
113
 
            background = (os.path.join(dir_path, name))
114
 
            if name.endswith(".jpg") or name.endswith(".png") or name.endswith(".jpeg"):
115
 
                gsettings.set_string(self.KEY, "file://" + background)
116
 
                time.sleep(float(sourceInterval)*minute)
117
 
                print "Set new background '%s' in %s minute(s)!" % (background, sourceInterval)
118
 
            else:
119
 
                print "Warning: Tried setting background, but '%s' is not a image file!" % (background)
120
 
        return
121
 
        
122
 
    def button_apply(self, builder, sourceFolder=defaultDirectory,sourceInterval=defaultInterval, configPath ='/home/%s/.config/pictoric/pictoric.conf' % (os.environ['USER'])):
123
 
        print "Saving '%s'" % (configPath)
124
 
        sourceFolder = self.builder.get_object("folderChooser").get_filename()
125
 
        sourceInterval = self.builder.get_object("intervalEdit").get_text()
126
 
        if sourceInterval == '0':
127
 
            sourceInterval = 5
128
 
        config = open("%s" % (configPath), 'w')
129
 
        config.write("%s\n" % (sourceFolder))
130
 
        config.write("%s\n" % (sourceInterval))
131
 
        self.destroy()
132
 
    
133
 
        
134
 
    def button_cancel(self, builder):
135
 
        Gtk.main_quit()
136
 
        raise SystemExit(0)
137
 
 
138
 
    def update_folder(self, builder,sourceFolder=sourceFolder,iconStore=iconStore):
139
 
        files = 0
140
 
        files_jpg = 0
141
 
        files_png = 0
142
 
        iconStore.clear()
143
 
        sourceFolder = self.builder.get_object("folderChooser").get_filename()
144
 
        iconview = self.builder.get_object("displayView")
145
 
        iconview.set_columns(-1)
146
 
        iconview.set_model(iconStore)
147
 
        iconview.set_pixbuf_column(0)
148
 
        iconview.set_selection_mode(Gtk.SelectionMode.MULTIPLE)
149
 
        
150
 
        Status = self.builder.get_object("status")
151
 
        #sourceFolder = "/home/cole/testme_dir2"
152
 
 
153
 
        #see if it exists
154
 
        if os.path.exists(sourceFolder):
155
 
            print "Directory "+sourceFolder+" is valid"
156
 
        else:
157
 
        # maybe do a dialog here?
158
 
            try:
159
 
                print "Could not find directory "+sourceFolder+" attempting to create it."
160
 
                os.makedirs(sourceFolder)
161
 
                print "Directory created at "+sourceFolder
162
 
            except OSError:
163
 
                print "Could not create directory at "+sourceFolder
164
 
                raise SystemExit(0)
165
 
        
166
 
        for name in os.listdir(sourceFolder):
167
 
            filename = os.path.join(sourceFolder, name)
168
 
            if name.endswith(".jpg") or name.endswith(".png") or name.endswith(".jpeg"):
169
 
                if name.endswith(".jpg") or name.endswith(".jpeg"):
170
 
                    files_jpg = files_jpg + 1
171
 
                if name.endswith(".png"):
172
 
                    files_png = files_png + 1
173
 
                files = files + 1
174
 
                pixbuf = Pixbuf.new_from_file_at_size(filename,96,96)
175
 
                iconStore.append([pixbuf])
176
 
            else:
177
 
                print "Warning: '%s' is not a image file!" % (filename)
178
 
        if files == 0:
179
 
            Status.push(1,"No images found in %s" % (sourceFolder))
180
 
        else:
181
 
            Status.push(1,"%s images found (%s .jpg, %s .png)" % (files, files_jpg, files_png))
182
 
 
183
 
        print "+++++++++"
184
 
    
185
 
 
186
 
    def change_interval(self, builder):
187
 
        return
188
 
        
189
 
 
190