#!/usr/bin/gtkbasic002
include "/usr/lib/wxbasicscript/basefunctions.inc"

a = xwin_getdir(command(2))
xwin_cd(a & "/resource")


//-- load  file and show the window
gins_glade("grabpics.glade")
gins_main()

//-- manipulate a widget, e.g. move the window
gins_set("window1" , "10" , "10" , "<uposition>")

 //-------------------
//-- load user config

xwin_system("mkdir /tmp/grabpics 2>/dev/null")
xwin_system("mkdir ~/.grabpics 2>/dev/null")
result=xwin_system("echo ~")
HOME=trim(result[0])

tempfoldersaved=readfiletolist(HOME & "/.grabpics/tempfolder.txt")
tempfolder = trim(tempfoldersaved[0])

result = xwin_system("test -d '" & tempfolder & "' ; echo $?" )
if trim(result[0]) != "0" then
 tempfolder = "/tmp/grabpics"
end if

gins_set("label2" , "label" , tempfolder , "<str>")

gins_set("label1" , "label" , "ready..." , "<str>")



//-- mainloop with eventhandling
while 1

  checkclose()
  widget , event , pdata = gins_event()

  if widget != Nothing then
  '  print widget
  '  print event
  '  print pdata

    if event = "GDK_DELETE" then
      gins_exit()
      end
    end if

    if event = "GDK_BUTTON_RELEASE" then


      if widget = "button1" then
        subbutton1()
      end if
      if widget = "button2" then
        subbutton2()
      end if
      if widget = "button3" then
        subbutton3()
      end if
    end if
  end if
  xwin_usleep(10000)

wend

sub subbutton1 ()

gins_set("label1" , "label" , "\n\nwait, searching pictures\n\n" , "<str>")


  caches = xwin_system("find ~/.mozilla -type d -name Cache")

  for each cache in caches
    files = xwin_system("ls " & cache)

    for each thefile in files
      checkfile = trim(cache) & "/" & trim(thefile)
      result = xwin_system("file " & checkfile)
//print result[0]
      if instr(result[0] , "JPEG image data") then
        xwin_system("cp -ax \"" & checkfile & "\" \"" & tempfolder & "/" & trim(thefile) & ".jpg\"")
      end if
      if instr(result[0] , "PNG image data") then
        xwin_system("cp -ax \"" & checkfile & "\" \"" & tempfolder & "/" & trim(thefile) & ".png\"")
      end if
      if instr(result[0] , "PC bitmap data") then
        xwin_system("cp -ax \"" & checkfile & "\" \"" & tempfolder & "/" & trim(thefile) & ".bmp\"")
      end if
      checkclose() //-- this can take long, so check if user wants to quit
    next
  next

  gins_set("label1" , "label" , "finished !" , "<str>")

end sub

sub subbutton2 ()

  xwin_system("rox " & tempfolder & "&")

end sub


sub subbutton3 ()
  fd = "/usr/local/Gtkbasic-002/dialog-multifilechooser/GBdialog-multifilechooser"
  result = xwin_system(fd & " 'Select a Folder' '" & tempfolder & "' '*.*' '3' '0'" )
  'print result
  result2 = xwin_system("test -d '" & trim(result[0]) & "' ; echo $?" )
  'print result2[0]
  if trim(result2[0]) = "0" then
    tempfolder = trim(result[0])
    gins_set("label2" , "label" , tempfolder , "<str>")
    writestringtofile(HOME & "/.grabpics/tempfolder.txt" , tempfolder)
  end if
end sub

sub checkclose()
	a=gins_get("window1" , "name" , "<str>")
	if a = Nothing then
		print "window was closed, exiting..."
		gins_exit()
		end
	end if
end sub