Simple iPhone connection monitor for Linux with GLib/Gio.

A very simple Gio iPhone connection monitor in Python 3. I wrote a crappier version of this awhile ago and revamped it. Hope it’s helpful to someone.

#!/usr/bin/env python3

'''
Simple iPhone connection monitor for Linux.
Requires Gtk (GLib, Gio) along with the Python 3 bindings.
Author: C. Nichols <mohawke@gmail.com>
'''

from gi.repository import Gio, GLib
import os

def notify(appname, message):
    
    '''Gnome notification.'''
    
    cmd = 'zenity --name="{0}" --notification --text="{1}" "$THREAT\n$STRING"'.format(appname, message)
    os.system(cmd)
    
    
def on_iphone_added(monitor, volume):
    
    '''List iPhone Document contents when connected.'''
    
    #notify("Simple iPhone Monitor", "{0} connected.".format(volume.get_name()))
    print("{0} connected.".format(volume.get_name()))
    gvfs_path = "/run/user/1000/gvfs/afc:host={0},port=3/".format(volume.get_uuid())
    not_ready = True
    while not_ready:
        if os.path.exists(gvfs_path):
            for doc_contents in os.listdir(gvfs_path):
                print(doc_contents)
            not_ready = False
            

def on_iphone_removed(monitor, volume):
    
    '''Display disconnected message.'''
    
    #notify("Simple iPhone Monitor", "iPhone disconnected.")
    print("disconnected")
    
    
def monitor():

    '''Uses GTK Gio to access Gnome virtual filesystem and determine the 
    path to iPhone Documents. Return a path we can use to push files 
    over.'''
    
    connection_loop = GLib.MainLoop()
    
    monitor_iphone = Gio.VolumeMonitor.get()
    
    monitor_iphone.connect("volume-added", on_iphone_added)
    monitor_iphone.connect("volume-removed", on_iphone_removed)
    for volume in monitor_iphone.get_volumes():
        try:
            activation_root = volume.get_activation_root()
            if activation_root:
                if activation_root.get_uri_scheme() == 'afc':
                    break
        except:
            pass # Something went wrong.
            
    connection_loop.run()
    

# Main :: Start monitoring
def main():
    monitor()
    
main() # Run program.