#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''****************************************************************************
**
** Copyright (C) 2008 Jędrzej Nowak <me@pigmej.eu>
** 
** This file may be used under the terms of the GNU General Public License
** version 2 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************'''

#launcher do dodania na panel gnome, można przeciągać pliki na niego
#wymaga klienta textowego
#umożliwia tworzenie.
#instalacja
#sudo mv wklej_to_applet_get.py /usr/local/bin/wklej_to_applet_get
#sudo chmod +x /usr/local/bin/wklej_to_applet_get


import sys
from subprocess import Popen, PIPE, call

try:
    plik=sys.argv[1]
except IndexError:
    call("notify-send -t 5000 -u critical 'Wklej.to: Błąd' 'Wymagane jest podanie pliku'",shell=True)
    sys.exit(1)
else:
    # skopiowane
    uid=Popen('xsel -b -i',shell=True,stdout = PIPE,stderr = PIPE)
    if not uid.wait():
        uid=uid.stdout.read()
    else:
        # zaznaczenie
        uid=Popen('xsel -p -i',shell=True,stdout = PIPE,stderr = PIPE)
        if not uid.wait():
            uid=uid.stdout.read()
        else:
            call("notify-send -t 5000 -u critical 'Wklej.to: Błąd' 'Błąd obsługi schowka'",shell=True)
            sys.exit(1)
    out=Popen('wklej_to -g -u %s -t text'%uid,shell=True,stdout = PIPE,stderr = PIPE)
    wy,err=out.communicate()
    if err:
        call("notify-send -t 5000 -u critical 'Wklej.to: Błąd' '%s'"%err,shell=True)
        sys.exit(1)
    call("notify-send -i ~/.wklej_to/icons/notify.png -t 5000 'Wykonano' 'W wybranym pliku: %s\nznajduje się zawartość paste: %s.'"%(plik,uid),shell=True)
    f=open(plik,'w')
    f.write(wy)
    f.close()
    sys.exit(0)
