Skip to content

Commit

Permalink
For #7, catching some errors and showing alert. Not catching all out-…
Browse files Browse the repository at this point in the history
…of-space errors
  • Loading branch information
Adam Rule committed Jul 23, 2014
1 parent 5cd0a78 commit 5ece3e5
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 47 deletions.
6 changes: 3 additions & 3 deletions selfspy/Debriefer.xib
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="1250" height="637"/>
<rect key="screenRect" x="0.0" y="0.0" width="1920" height="1058"/>
<rect key="screenRect" x="0.0" y="0.0" width="1280" height="778"/>
<view key="contentView" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="1250" height="637"/>
<autoresizingMask key="autoresizingMask"/>
Expand Down Expand Up @@ -142,7 +142,7 @@
<rect key="frame" x="994" y="85" width="212" height="58"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
<size key="cellSize" width="212" height="18"/>
<size key="cellSize" width="208" height="18"/>
<size key="intercellSpacing" width="4" height="2"/>
<buttonCell key="prototype" type="radio" title="Radio" imagePosition="left" alignment="left" inset="2" id="WGy-ke-NjE">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
Expand All @@ -169,7 +169,7 @@
</cells>
</matrix>
<slider verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="KKz-5E-Zqe">
<rect key="frame" x="1012" y="162" width="190" height="26"/>
<rect key="frame" x="1012" y="162" width="190" height="25"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<sliderCell key="cell" state="on" alignment="left" minValue="1" maxValue="5" doubleValue="3" tickMarkPosition="above" numberOfTickMarks="5" allowsTickMarkValuesOnly="YES" sliderType="linear" id="Oc8-U9-Mj2"/>
<connections>
Expand Down
30 changes: 27 additions & 3 deletions selfspy/activity_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@


import os
import sys
import time
import datetime
import errno

import sqlalchemy
import urllib
Expand Down Expand Up @@ -86,7 +88,18 @@ def __init__(self, db_name):
except OSError:
pass

self.session_maker = models.initialize(os.path.join(cfg.CURRENT_DIR, db_name))
try:
self.session_maker = models.initialize(os.path.join(cfg.CURRENT_DIR, db_name))
except sqlalchemy.exc.OperationalError:
print "Database operational error. Your storage device may be full. Exiting Selfspy..."

alert = NSAlert.alloc().init()
alert.addButtonWithTitle_("OK")
alert.setMessageText_("Database operational error. Your storage device may be full. Exiting Selfspy.")
alert.setAlertStyle_(NSWarningAlertStyle)
alert.runModal()

sys.exit()

self.key_presses = []
self.mouse_path = []
Expand Down Expand Up @@ -195,9 +208,20 @@ def trycommit(self):
self.session.commit()
break
except sqlalchemy.exc.OperationalError:
time.sleep(1)
print "Database operational error. Your storage device may be full. Turning off Selfspy recording."
self.sniffer.delegate.toggleLogging_(self)
self.session.rollback()

alert = NSAlert.alloc().init()
alert.addButtonWithTitle_("OK")
alert.setMessageText_("Database operational error. Your storage device may be full. Turning off Selfspy recording.")
alert.setAlertStyle_(NSWarningAlertStyle)
alert.runModal()

break
except:
self.session.rollback()
print "Rollback"
self.session.rollback()

def got_screen_change(self, process_name, window_name, win_x, win_y, win_width, win_height):
""" Receives a screen change and stores any changes. If the process or window has
Expand Down
2 changes: 1 addition & 1 deletion selfspy/debriefer.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def toggleAudioRecording_(self, sender):
print "Stop Audio recording"
# seems to miss reading the name sometimes
imageName = str(controller.mainPanel.image().name())[0:-4]
print "Audio name should be " + imageName
if (imageName == None) | (imageName == ''):
imageName = datetime.now().strftime("%y%m%d-%H%M%S%f") + '-audio'
imageName = str(os.path.join(cfg.CURRENT_DIR, "audio/")) + imageName + '.m4a'
Expand Down Expand Up @@ -218,7 +219,6 @@ def advanceExperienceWindow_(self, sender):
experienceImage.setScalesWhenResized_(True)
experienceImage.setSize_((width, height))
experienceImage.setName_(path.split("/")[-1])
print "name at setting point is " + path.split("/")[-1]
controller.mainPanel.setImage_(experienceImage)

controller.progressLabel.setStringValue_( str(i + 1) + '/' + str(l) )
Expand Down
98 changes: 58 additions & 40 deletions selfspy/sniff_cocoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import string
import objc, re, os
import errno

from objc import IBAction, IBOutlet

Expand Down Expand Up @@ -492,47 +493,60 @@ def makeAppActive_(self, notification):
self.app.activateIgnoringOtherApps_(True)

def takeExperienceScreenshot_(self, notification):
mouseLoc = NSEvent.mouseLocation()
x = str(int(mouseLoc.x))
y = str(int(mouseLoc.y))
folder = os.path.join(cfg.CURRENT_DIR,"screenshots")
filename = datetime.now().strftime("%y%m%d-%H%M%S%f") + "_" + x + "_" + y + '-experience'
path = os.path.join(folder,""+filename+".jpg")

# -i makes the screenshot interactive
# -C captures the mouse cursor.
# -x removes the screenshot sound
if notification.object().takeFullScreenshot:
command = "screencapture -x -C '" + path + "'"
else:
command = "screencapture -i -x -C '" + path + "'"
# delete current full-screen screenshot for this experience
os.system("rm "+ notification.object().currentScreenshot )

print command
os.system(command)

notification.object().currentScreenshot = path

if not notification.object().takeFullScreenshot:

path = os.path.expanduser(path)

experienceImage = NSImage.alloc().initByReferencingFile_(path)
width = experienceImage.size().width
height = experienceImage.size().height
ratio = width / height
if( width > 360 or height > 225 ):
if (ratio > 1.6):
width = 360
height = 360 / ratio
else:
width = 225 * ratio
height = 225
try:
mouseLoc = NSEvent.mouseLocation()
x = str(int(mouseLoc.x))
y = str(int(mouseLoc.y))
folder = os.path.join(cfg.CURRENT_DIR,"screenshots")
filename = datetime.now().strftime("%y%m%d-%H%M%S%f") + "_" + x + "_" + y + '-experience'
path = os.path.join(folder,""+filename+".jpg")

# -i makes the screenshot interactive
# -C captures the mouse cursor.
# -x removes the screenshot sound
if notification.object().takeFullScreenshot:
command = "screencapture -x -C '" + path + "'"
else:
command = "screencapture -i -x -C '" + path + "'"
# delete current full-screen screenshot for this experience
os.system("rm "+ notification.object().currentScreenshot )

print command
os.system(command)

notification.object().currentScreenshot = path

experienceImage.setScalesWhenResized_(True)
experienceImage.setSize_((width, height))
notification.object().screenshotDisplay.setImage_(experienceImage)
if not notification.object().takeFullScreenshot:

path = os.path.expanduser(path)

experienceImage = NSImage.alloc().initByReferencingFile_(path)
width = experienceImage.size().width
height = experienceImage.size().height
ratio = width / height
if( width > 360 or height > 225 ):
if (ratio > 1.6):
width = 360
height = 360 / ratio
else:
width = 225 * ratio
height = 225

experienceImage.setScalesWhenResized_(True)
experienceImage.setSize_((width, height))
notification.object().screenshotDisplay.setImage_(experienceImage)
except errno.ENOSPC:
NSLog("No space left on storage device. Turning off Selfspy recording.")
self.delegate.toggleLogging_(self,self)

alert = NSAlert.alloc().init()
alert.addButtonWithTitle_("OK")
alert.setMessageText_("No space left on storage device. Turning off Selfspy recording.")
alert.setAlertStyle_(NSWarningAlertStyle)
alert.runModal()

except:
NSLog("Could not save image")

def screenshot(self, path, region = None):
#https://pythonhosted.org/pyobjc/examples/Quartz/Core%20Graphics/CGRotation/index.html
Expand Down Expand Up @@ -659,7 +673,11 @@ def screenshot(self, path, region = None):
print 'took ' + str(height) + 'px image in ' + str(stop-start)[:5] + ' seconds'

except KeyboardInterrupt:
print "Keyboard interrupt"
AppHelper.stopEventLoop()
except errno.ENOSPC:
NSLog("No space left on storage device. Turning off Selfspy recording.")
self.delegate.toggleLogging_(self)
except:
NSLog("couldn't save image")

Expand Down

0 comments on commit 5ece3e5

Please sign in to comment.