Cleanup temporary files on close, scale user image to better apply with AccountsService

This commit is contained in:
Sean Davis 2014-03-29 11:29:49 -04:00
parent 495105220d
commit d895b4a4eb
4 changed files with 56 additions and 13 deletions

View File

@ -27,9 +27,9 @@ from gi.repository import Gtk, GObject, Gst, GdkPixbuf
from gi.repository import GdkX11, GstVideo # lint:ok
import cairo
import tempfile
import os
from mugshot_lib import helpers
from mugshot_lib.CameraDialog import CameraDialog
@ -268,9 +268,7 @@ class CameraMugshotDialog(CameraDialog):
# Record (Capture) action.
else:
# Create a new temporary file.
tmpfile = tempfile.NamedTemporaryFile(delete=False)
tmpfile.close()
self.filename = tmpfile.name
self.filename = helpers.new_tempfile('camera')
# Capture the current image.
self.take_picture(self.filename)

View File

@ -27,13 +27,11 @@ import subprocess
# DBUS interface is used to update pidgin buddyicon when pidgin is running.
import dbus
import tempfile
from gi.repository import Gtk, GdkPixbuf, GLib, Gio # pylint: disable=E0611
import logging
logger = logging.getLogger('mugshot')
from mugshot_lib import Window, SudoDialog
from mugshot_lib import Window, SudoDialog, helpers
from mugshot.CameraMugshotDialog import CameraMugshotDialog
username = GLib.get_user_name()
@ -376,6 +374,16 @@ class MugshotWindow(Window):
# Copy the new file to ~/.face
if os.path.isfile(self.updated_image):
# Scale the image as necessary (lp: #1298665)
pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.updated_image)
if pixbuf.get_height() > 512 or pixbuf.get_width() > 512:
scaled_filename = helpers.new_tempfile('scaled')
scaled = pixbuf.scale_simple(512, 512,
GdkPixbuf.InterpType.HYPER)
scaled.savev(scaled_filename, "png", [], [])
self.updated_filename = scaled_filename
# Copy the file to ~/.face
shutil.copyfile(self.updated_image, face)
else:
face = ""
@ -821,11 +829,7 @@ class MugshotWindow(Window):
response = self.chooser.run()
if response == Gtk.ResponseType.APPLY:
# Update the user image, store the path for committing later.
if self.tmpfile and os.path.isfile(self.tmpfile.name):
os.remove(self.tmpfile.name)
self.tmpfile = tempfile.NamedTemporaryFile(delete=False)
self.tmpfile.close()
self.updated_image = self.tmpfile.name
self.updated_image = helpers.new_tempfile('browse')
self.filechooser_preview_pixbuf.savev(self.updated_image, "png",
[], [])
logger.debug("Selected %s" % self.updated_image)

View File

@ -23,7 +23,7 @@ from gi.repository import Gtk # pylint: disable=E0611
from mugshot import MugshotWindow
from mugshot_lib import set_up_logging, get_version
from mugshot_lib import set_up_logging, get_version, helpers
def parse_options():
@ -45,3 +45,6 @@ def main():
window = MugshotWindow.MugshotWindow()
window.show()
Gtk.main()
# Cleanup temporary files
helpers.clear_tempfiles()

View File

@ -19,6 +19,8 @@
import logging
import os
import tempfile
from . mugshotconfig import get_data_file
from . Builder import Builder
@ -101,3 +103,39 @@ def alias(alternative_function_name):
function.aliases.append(alternative_function_name)
return function
return decorator
# = Temporary File Management ============================================ #
temporary_files = {}
def new_tempfile(identifier):
"""Create a new temporary file, register it, and return the filename."""
remove_tempfile(identifier)
temporary_file = tempfile.NamedTemporaryFile(delete=False)
temporary_file.close()
filename = temporary_file.name
temporary_files[identifier] = filename
return filename
def get_tempfile(identifier):
"""Retrieve the specified temporary filename."""
if identifier in list(temporary_files.keys()):
return temporary_files[identifier]
return None
def remove_tempfile(identifier):
"""Remove the specified temporary file from the system."""
if identifier in list(temporary_files.keys()):
filename = temporary_files[identifier]
if os.path.isfile(filename):
os.remove(filename)
temporary_files.pop(identifier)
def clear_tempfiles():
"""Remove all temporary files registered to Mugshot."""
for identifier in list(temporary_files.keys()):
remove_tempfile(identifier)