Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[TIP] CUPS-PDF "Save As" with kdialog
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Havin_it
Veteran
Veteran


Joined: 17 Jul 2005
Posts: 1172
Location: Edinburgh, UK

PostPosted: Sat Sep 01, 2007 9:28 pm    Post subject: [TIP] CUPS-PDF "Save As" with kdialog Reply with quote

Hi,

So I got this idea. I use CUPS-PDF to do the job of PDF printing, but it lacks something I valued in CutePDF Writer on Windows: the Save As dialog. I just discovered the "Postprocessing" option in the cups-pdf.conf file, where you can pass the filename of the generated file to a handler-script to do something with it.

Great, thinks I. I can write a script using kdialog to add-in a "Save as" operation, like this:
Code:
#!/bin/sh

KDLG=`which kdialog`
SAVEAS=`$KDLG --getsavefilename ${1} application/pdf`

mv "${1}" "${SAVEAS}" || $KDLG --error "Well, that went wrong."


Only it doesn't work; it runs, kdialog runs, but the dialog doesn't appear on the screen so the script exits unsuccessfully. Any idea what I'm doing wrong, or if there's another way to get what I seek?

Thanks in advance :D


Last edited by Havin_it on Tue Sep 04, 2007 11:34 am; edited 1 time in total
Back to top
View user's profile Send private message
redpenguin
Tux's lil' helper
Tux's lil' helper


Joined: 22 May 2006
Posts: 134

PostPosted: Sat Sep 01, 2007 9:49 pm    Post subject: Reply with quote

I think you're missing the $DISPLAY environment variable in that script. When you run the X server, this variable gets initialized (for example), to ":0". This way the application knows on which display it should be run. Now, either try setting that variable in the script, or you could try adding "--display :0" to the kdialog arguments (taken from "kdialog --help-qt"). Don't forget to check what display you're actually using ;) I don't know if you could somehow dynamically get this variable, but I doubt it. If you manage to do it, let us know ;)

EDIT: Oh, yeah - I'm STILL not sure if that will help. :D
Back to top
View user's profile Send private message
Havin_it
Veteran
Veteran


Joined: 17 Jul 2005
Posts: 1172
Location: Edinburgh, UK

PostPosted: Sat Sep 01, 2007 11:21 pm    Post subject: Reply with quote

No change, unfortunately. I changed the script a bit so I could get some debugging output:

Code:
DISPLAY=:0.0
KDLG=/usr/kde/3.5/bin/kdialog
SAVEAS=`$KDLG --display :0.0 --getsavefilename ${1} application/pdf 2>&1`

if ! [ "${SAVEAS}" == "${1}" ] && ! [ "${SAVEAS}" == "" ]; then
        mv "${1}" "${SAVEAS}" || $KDLG --display :0 --error "Well, that went wrong."
fi
env >/home/robin/cups-pdf/postproc.log
echo "

${KDLG}
${SAVEAS}" >>/home/robin/cups-pdf/postproc.log


Here's what I now get in the log I generate:
Code:
IPP_PORT=631
TMPDIR=/var/tmp
CUPS_DOCROOT=/usr/share/cups/html
CUPS_FONTPATH=/usr/share/cups/fonts
USER=root
CUPS_SERVERROOT=/etc/cups
SOFTWARE=CUPS/1.2.12
CUPS_CACHEDIR=/var/cache/cups
CUPS_REQUESTROOT=/var/spool/cups
PATH=/usr/libexec/cups/filter:/usr/bin:/usr/sbin:/bin:/usr/bin
DEVICE_URI=cups-pdf:/
SERVER_ADMIN=root@localhost
PWD=/
LANG=en_GB
PPD=/etc/cups/ppd/CUPS-PDF.ppd
CUPS_ENCRYPTION=IfRequested
SHLVL=2
CUPS_DATADIR=/usr/share/cups
PRINTER=CUPS-PDF
RIP_MAX_CACHE=8m
CONTENT_TYPE=application/postscript
CUPS_SERVER=/var/run/cups/cups.sock
CHARSET=iso-8859-1
CUPS_SERVERBIN=/usr/libexec/cups
CUPS_STATEDIR=/var/run/cups
_=/usr/bin/env


/usr/kde/3.5/bin/kdialog
Xlib: connection to ":0.0" refused by server
Xlib: No protocol specified

kdialog: cannot connect to X server :0
Back to top
View user's profile Send private message
redpenguin
Tux's lil' helper
Tux's lil' helper


Joined: 22 May 2006
Posts: 134

PostPosted: Sat Sep 01, 2007 11:30 pm    Post subject: Reply with quote

Hm... Are you sure the DISPLAY of your active session is ":0"? One more thing occurred to me - under what user/group this script is executed? It could be permission-related. If it's permission-related, the thing probably won't work unless you add bigger privileges to the cups user/group, which is not advisable, to be honest :/
Back to top
View user's profile Send private message
Havin_it
Veteran
Veteran


Joined: 17 Jul 2005
Posts: 1172
Location: Edinburgh, UK

PostPosted: Sat Sep 01, 2007 11:54 pm    Post subject: Reply with quote

The DISPLAY variable is :0.0, not just :0 (although I've tried both now), and the script executes as the user who printed (me). However, there's obviously a lot of env-vars missing besides DISPLAY, as the output shows.

It seems that I need the shell in which the script runs to be take on the properties (or perhaps just certain env-vars) of a login shell. Is there a way I can do that?
Back to top
View user's profile Send private message
Havin_it
Veteran
Veteran


Joined: 17 Jul 2005
Posts: 1172
Location: Edinburgh, UK

PostPosted: Sun Sep 02, 2007 10:18 am    Post subject: Reply with quote

Hmm, I tried changing the invocation line in the script to
Code:
#!/bin/bash -i -l

which gave the script environment more variables, but the result is the same.

In the meantime, I came up with an alternative, 2-part solution. First, a simpler postprocessing handler that just makes a note of the printed filename in the user's cups-pdf dir:
Code:
#!/bin/bash
# Note: args passed to this script are the filename, followed by your username

echo "${1}" >>/home/${2}/cups-pdf/save_as.list

Then a second script, a daemon started by the user's KDE Autostart:
Code:
#!/bin/sh

while true; do
        sleep 10
        if [ -e ${HOME}/cups-pdf/save_as.list ] ; then
                for ITER in `cat ${HOME}/cups-pdf/save_as.list`; do
                        SAVEAS=`kdialog --getsavefilename "${ITER}" application/pdf`
                        if ! [ "${SAVEAS}" == "${ITER}" ] && ! [ "${SAVEAS}" == "" ] ; then
                                mv "${ITER}" "${SAVEAS}"
                        fi
                done
                rm ${HOME}/cups-pdf/save_as.list
        fi
done


It works, but I'm not too happy with it as it seems a bit overengineered for such a simple job. If the original problem has a solution, I'd still like to hear any ideas on the subject.
Back to top
View user's profile Send private message
redpenguin
Tux's lil' helper
Tux's lil' helper


Joined: 22 May 2006
Posts: 134

PostPosted: Sun Sep 02, 2007 11:04 am    Post subject: Reply with quote

Actually, the solution you made seems ok. But why don't you try to just tail -f the save_as.list file? This way you wouldn't need the 'sleep 10' code, I think.
Back to top
View user's profile Send private message
Havin_it
Veteran
Veteran


Joined: 17 Jul 2005
Posts: 1172
Location: Edinburgh, UK

PostPosted: Sun Sep 02, 2007 12:25 pm    Post subject: Reply with quote

That does seem better (I guess it would use less memory) but not sure how I'd use it in the daemon script. I was thinking of something using a fifo but I didn't see how it could work. Think I need to understand flow-control and redirection in bash a bit better.

How could a new line received from the file be made into a parameter for the kdialog call?
Back to top
View user's profile Send private message
redpenguin
Tux's lil' helper
Tux's lil' helper


Joined: 22 May 2006
Posts: 134

PostPosted: Sun Sep 02, 2007 12:49 pm    Post subject: Reply with quote

Here's a simple example:
Code:

while read line; do
    echo "$line";
done < <(tail -f -n1 "$HOME/file.txt")

That's the way you can feed the while loop with tail (-f for follow, -n1 for only the last line in the file to be fed - you might want to change it to something bigger). The FIFO file idea is interesting to me. The code would remain pretty much the same, except that you'd be creating a FIFO file via mkfifo command. The only problem could be -n1 (or -n10, which is the default for tail), since it would actually pickup the FIRST line instead of the last in the FIFO queue. Also, I've done a simple test now you'd want to keep in mind:

a) I created a FIFO file (let's call it test.txt)
b) On one console I entered: echo "Testing" > test.txt
c) Well, echo kept executing until on the other console I typed: tail test.txt

So, I guess you'd like to figure out how this waiting would work. You'd might want to send the commands that write to the FIFO file into background with '&'.
Back to top
View user's profile Send private message
Havin_it
Veteran
Veteran


Joined: 17 Jul 2005
Posts: 1172
Location: Edinburgh, UK

PostPosted: Sun Sep 02, 2007 2:07 pm    Post subject: Reply with quote

That does the job nicely :D
I didn't have to change the postprocessor, and here's the new version of the daemon:
Code:
#!/bin/bash
# Has to be bash because sh doesn't like the last line
while read LINE; do
        SAVEAS=`kdialog --getsavefilename "${LINE}" application/pdf`
                if ! [ "${SAVEAS}" == "${LINE}" ] && ! [ "${SAVEAS}" == "" ] ; then
                        mv "${LINE}" "${SAVEAS}"
                fi
done < <(tail -f "$HOME/cups-pdf/save_as.list")

Still would be nice to have a one-file solution, but this is a step in the right direction, and I imagine would be easy enough to re-work for xdialog or whatever GTK equivalent.

EDIT: still needs a bit of polish, of course. One thing that just occurred to me was it needs overwrite-confirmation -- this was actually why I got started doing this, because like-named web-pages were getting silently overwritten. Should be easy enough to do with kdialog.
Back to top
View user's profile Send private message
Havin_it
Veteran
Veteran


Joined: 17 Jul 2005
Posts: 1172
Location: Edinburgh, UK

PostPosted: Tue Sep 04, 2007 11:33 am    Post subject: Reply with quote

Okay, final version (for now anyway).

I set cups-pdf.conf back to defaults, so the file is initially saved to /var/spool/cups-pdf/$USER. Then the daemon takes the filename and opens a save-dialog to put the filename in the directory specified in SAVEDIR. It now has an overwrite warning, and if you cancel or give null input to any dialog it will notify that the file is still saved in the spool dir.

Code:
#!/bin/bash

SAVEDIR=${HOME}/cups-pdf

while read LINE; do
        FNAME=`echo ${LINE} | egrep -o '[^/]+$'`
        SAVEAS=`kdialog --getsavefilename "${SAVEDIR}/${FNAME}" application/pdf`
        if [ -e "${SAVEAS}" ] ; then
                kdialog --warningyesno "File ${SAVEAS} exists. Overwrite?" || SAVEAS=""
        fi
        if [ "${SAVEAS}" == "${LINE}" ] || [ "${SAVEAS}" == "" ] ; then
                kdialog --passivepopup "File was left at ${LINE}" 3
        else
                mv "${LINE}" "${SAVEAS}"
                kdialog --passivepopup "File saved at ${SAVEAS}" 3
        fi
done < <(tail -f "$SAVEDIR/save_as.list")


Any suggestions for improvements welcome.
Back to top
View user's profile Send private message
Fitzcarraldo
Veteran
Veteran


Joined: 30 Aug 2008
Posts: 1769
Location: United Kingdom

PostPosted: Wed Jul 01, 2009 7:00 am    Post subject: Reply with quote

The following method using zenity worked perfectly for me:

Code:
# emerge zenity


I created the following script in my home directory and gave it the meaningful name cups-pdf_script.sh:

Code:
#!/bin/bash
CURRENT_PDF="${1}"
CURRENT_USER="${2}"
DISPLAY=:0.0
export DISPLAY
XAUTHORITY=/home/${CURRENT_USER}/.Xauthority
export XAUTHORITY
PDFNAME=$(zenity --file-selection --save --confirm-overwrite)
mv "$CURRENT_PDF" "$PDFNAME"


I made the file executable:

Code:
# chmod ugo=rwx cups-pdf_script.sh


I edited /etc/cups/cups-pdf.conf, found the line starting "#PostProcessing" and changed it to:

Code:
PostProcessing /home/fitzcarraldo/cups-pdf_script.sh


I restarted CUPS:

Code:
# /etc/init.d/cupsd restart


Now, when I select the Print_to_PDF_file virtual printer in my applications, a pop-up window appears allowing me to select the target directory for the PDF file and to enter a file name of my choice.

However I can't get a version of my script working using kdialog instead of zenity. The following postprocessing script does not result in a Save As box being displayed and the PDF file is saved in the default /var/spool/cups-pdf/fitzcarraldo/ directory, whereas the version above using zenity worked. Any ideas where I am going wrong?

Code:
#!/bin/bash
CURRENT_PDF="${1}"
CURRENT_USER="${2}"
DISPLAY=:0.0
export DISPLAY
XAUTHORITY=/home/${CURRENT_USER}/.Xauthority
export XAUTHORITY
#PDFNAME=$(zenity --file-selection --save --confirm-overwrite)
PDFNAME=$(/usr/kde/4.2/bin/kdialog --getsavefilename :label1)
mv "$CURRENT_PDF" "$PDFNAME"
Back to top
View user's profile Send private message
Havin_it
Veteran
Veteran


Joined: 17 Jul 2005
Posts: 1172
Location: Edinburgh, UK

PostPosted: Wed Jul 01, 2009 11:51 am    Post subject: Reply with quote

Whoa, attack of the zombie thread ;)

I suspect the problem is with the environment the script runs under missing that kdialog needs (and zenity doesn't, which makes me think I should just have used that!)

As you can see above, the only way I could make it work was to have a daemon running from my KDE Autostart (which gives it whatever env is needed, I guess), listening to a fifo. The postprocessing script simply passes the original filename to the fifo, then the daemon runs kdialog when it receives a filename through the fifo.

Sidenote: I don't use this any more really, as the two programs I actually print from (Firefox and OOo) have PDF support built-in. I'd certainly be interested if you can succeed where I gave up and come up with a one-script solution - good luck :D
Back to top
View user's profile Send private message
Fitzcarraldo
Veteran
Veteran


Joined: 30 Aug 2008
Posts: 1769
Location: United Kingdom

PostPosted: Sat Jul 04, 2009 9:33 pm    Post subject: Reply with quote

After many hours I gave up trying to get a version with kdialog working. The zenity solution in my previous post works perfectly so I'll just have to stick with that, even if zenity is from GNOME not KDE.

It's a mystery to me why zenity works but kdialog does not. I even tried the method given in the thread https://forums.gentoo.org/viewtopic-t-222059-highlight-.html, i.e. I wrote a PostProcessing script (see below) that creates a temporary script which the PostProcessing script then runs as the normal user (me) by using su, but that also did not display the kdialog window. However that method of running kdialog as the normal user did work when I ran a similar script from the command line either as the normal user or as root user, which makes it even more perplexing why that method does not work when the parent script is launched by cups.

Code:
#!/bin/bash
#####################################################
# PostProcessing script for cups-pdf. Test version 3.
#####################################################

CURRENT_PDF="${1}"
CURRENT_USER="${2}"

# Create a temporary script that prompts the user to specify the directory and name for the PDF file,
# and then moves the PDF file from the spool directory to the user-specified directory/file name:
echo "#!/bin/bash" > /tmp/kdialog_cmd
echo "CURRENT_PDF="$CURRENT_PDF >> /tmp/kdialog_cmd
echo "CURRENT_USER="$CURRENT_USER >> /tmp/kdialog_cmd
echo "DISPLAY=:0.0" >> /tmp/kdialog_cmd
echo "export DISPLAY" >> /tmp/kdialog_cmd
echo 'XAUTHORITY=/home/'$CURRENT_USER'/.Xauthority' >> /tmp/kdialog_cmd
echo "export XAUTHORITY" >> /tmp/kdialog_cmd
echo 'PDFNAME=$(/usr/kde/4.2/bin/kdialog --getsavefilename :label1)' >> /tmp/kdialog_cmd
echo 'mv "$CURRENT_PDF" "$PDFNAME"' >> /tmp/kdialog_cmd

# Now make sure the temporary script is executable:
chmod a+x /tmp/kdialog_cmd

# Now run the temporary script as the normal user:
su - $CURRENT_USER -c /tmp/kdialog_cmd


When I printed a document to the cups-pdf virtual printer, the above PostProcessing script generated a /tmp/kdialog_cmd script containing the following:

Code:
#!/bin/bash
CURRENT_PDF=/var/spool/cups-pdf/fitzcarraldo/test.pdf
CURRENT_USER=fitzcarraldo
DISPLAY=:0.0
export DISPLAY
XAUTHORITY=/home/fitzcarraldo/.Xauthority
export XAUTHORITY
PDFNAME=$(/usr/kde/4.2/bin/kdialog --getsavefilename :label1)
mv "$CURRENT_PDF" "$PDFNAME"

which looks OK to me but did not result in a pop-up kdialog window when it was run by the su command in the PostProcessing script.

I also tried using:

Code:
su $CURRENT_USER -c /tmp/kdialog_cmd

instead of:
Code:
su - $CURRENT_USER -c /tmp/kdialog_cmd


in the PostProcessing script but the result was the same: no kdialog pop-up window. :?
Back to top
View user's profile Send private message
avendesora
Veteran
Veteran


Joined: 16 Aug 2002
Posts: 1739
Location: Betelgeuse vicinity

PostPosted: Sun Jul 05, 2009 8:21 am    Post subject: Reply with quote

I've just tried this here for fun and the thing is that su needs a terminal, so it's probably not running your script at all.
Try with sudo instead.
Code:
sudo -u $CURRENT_USER /your/script

I don't think that second script needs to be regenerated each time. Just pass it the arguments it needs, otherwise you
could get strange stuff if two pdf printouts are done "simultaneously".

BTW: tip: to debug the thing, you should log the output & error of the commands to know what's going on.
e.g. I had in my script:
Code:
su - $cups_user -c /tmp/cups_dialog > /tmp/$$.out 2> /tmp/$$.err

No way you can guess what is going on otherwise.
Back to top
View user's profile Send private message
Fitzcarraldo
Veteran
Veteran


Joined: 30 Aug 2008
Posts: 1769
Location: United Kingdom

PostPosted: Sun Jul 05, 2009 4:08 pm    Post subject: Reply with quote

Thanks for your post, avendesora. However, using:
Code:
sudo -u $CURRENT_USER /tmp/kdialog_cmd

instead of:
Code:
su $CURRENT_USER -c /tmp/kdialog_cmd

did not result in any pop-up window from KDialog either, and the contents of the /tmp/$$.err file were:
Code:
fitzcarraldo@acertm8215 ~ $ cat /tmp/*.err
Sorry, user fitzcarraldo is not allowed to execute '/tmp/kdialog_cmd' as fitzcarraldo on acertm8215.
fitzcarraldo@acertm8215 ~ $

That's why I thought su had to be used, but I see what you mean about it having to be run from a terminal:
Code:
fitzcarraldo@acertm8215 ~ $ cat /tmp/*.err
su: must be run from a terminal
fitzcarraldo@acertm8215 ~ $
Back to top
View user's profile Send private message
Havin_it
Veteran
Veteran


Joined: 17 Jul 2005
Posts: 1172
Location: Edinburgh, UK

PostPosted: Sun Jul 05, 2009 4:41 pm    Post subject: Reply with quote

@fitzcarraldo, have you tried my two-script system? The whole thing is posted in the other thread on this subject you've recently posted in, and it still works fine for me.
Back to top
View user's profile Send private message
Fitzcarraldo
Veteran
Veteran


Joined: 30 Aug 2008
Posts: 1769
Location: United Kingdom

PostPosted: Sun Jul 05, 2009 5:36 pm    Post subject: Reply with quote

I haven't, Havin_it, but I have no doubt that it works perfectly. I'm just curious to find out why KDialog will not work when using a stand-alone PostProcessing script rather than a two-script solution such as yours. The Zenity single-script solution works perfectly, so it's not essential that I find a KDialog solution, but it would be interesting to find out why KDialog doesn't work this way.
Back to top
View user's profile Send private message
iaselle
n00b
n00b


Joined: 10 Jul 2014
Posts: 1

PostPosted: Thu Jul 10, 2014 4:14 pm    Post subject: Reply with quote

Very upset with the drawbacks of CUPS-PDF, I have developed a PDF printer (called pdf-optima) that produces smaller PDF files than CUPS-PDF and that offers a "save as" dialog.

The solution and installation instruction are here
http://forums.linuxmint.com/viewtopic.php?f=47&t=172796
Back to top
View user's profile Send private message
sicvolo
n00b
n00b


Joined: 26 Sep 2014
Posts: 1

PostPosted: Fri Sep 26, 2014 3:40 am    Post subject: Solved! Reply with quote

@Havin_it

I had the same issue with kdialog and was able to solve it. The reason kdialog crashes is that it can't connect to the active DBUS for the user session in question.
So, find the DBUS address, export it out in a variable, along with DISPLAY, XAUTHORITY and HOME and kdialog will work. Finding DBUS address is a bit tricky, but here is what worked for me:

get_dbus.sh
Code:

#!/bin/bash
if [ -z $1 ]; then
    echo 'specify user'
    exit 1
fi
# Search these processes for the session variable (they are run as the current user and have the DBUS session variable set)
compatiblePrograms=( kdeinit kded4 pulseaudio trackerd ) #nautilus

# Attempt to get a program pid
for index in ${compatiblePrograms[@]}; do
   PID=$(ps -ef | grep $1 | grep ${index} | head -1 | awk '{print $2}')
   if [[ "${PID}" != "" ]]; then
      break
   fi
done
if [[ "${PID}" == "" ]]; then
   echo "Could not detect active login session"
   exit 1
fi
QUERY_ENVIRON="$(tr '\0' '\n' < /proc/${PID}/environ | grep "DBUS_SESSION_BUS_ADDRESS" | cut -d "=" -f 2-)"
if [[ "${QUERY_ENVIRON}" != "" ]]; then
   #export DBUS_SESSION_BUS_ADDRESS="${QUERY_ENVIRON}"
   #echo "Connected to session:"
   echo ${QUERY_ENVIRON}
   exit 0
else
   echo "Could not find dbus session ID in user environment."
   exit 1
fi


The PDF post processing code that uses it:
Code:

#!/bin/bash
CURRENT_PDF="$1"
CURRENT_USER="$2"

export DISPLAY=:0
export XAUTHORITY=/home/${CURRENT_USER}/.Xauthority
export HOME=/home/${CURRENT_USER}/
CONFIG_FILE="/home/${CURRENT_USER}/.pdf-writer.conf"
CANCELLED="No"

export DBUS_SESSION_BUS_ADDRESS=$(/home/${CURRENT_USER}/bin/get_dbus.sh ${CURRENT_USER})

while [ True ]; do
   CURDATE=$(/bin/date +%Y%m%d)
   CURNAME=$(/bin/cat "$CONFIG_FILE")
   BASENAME=$(basename $CURRENT_PDF)
   #FILENAME=$(/usr/bin/zenity --file-selection --save --confirm-overwrite --title="Save PDF" --filename="$CURNAME$CURDATE - " --file-filter="*.pdf")
   FILENAME=$(/usr/bin/kdialog --getsavefilename "$CURNAME$CURDATE - " "*.pdf" --title="Save PDF")
   if [ $? -eq 1 ]; then
      CANCELLED="Yes"
      break
   fi
   echo $FILENAME
   if [ ! "$FILENAME" = "" ]; then
      if [ -e "$FILENAME" ]; then
         /usr/bin/kdialog --warningcontinuecancel "File already exists"
         if [ $? -eq 1 ]; then
            continue;
         fi
      fi
      FILENAME=$(echo $FILENAME.pdf | sed -re "s/(\.pdf)+$/.pdf/g")
      break;
   else
      #/usr/bin/zenity --error --text "You must select a file or Cancel"
      /usr/bin/kdialog --error "You must select a file or hit cancel. No mocking around."
   fi
   break
done

if [ "${CANCELLED}" == "No" ]; then
   /bin/cp "${CURRENT_PDF}" "${FILENAME}"
   #evince "${FILENAME}" &
   okular "${FILENAME}" & #--geometry 1500x1000 "${FILENAME}" & # fighting an odd maximization of the window
fi
/bin/rm "${CURRENT_PDF}"
exit 0


PS Zenity did not work for me, so I had to find the solution ;)

PPS Wish laselle's code was in debian repos. Too lazy to compile and mess around with installing it
Back to top
View user's profile Send private message
Fitzcarraldo
Veteran
Veteran


Joined: 30 Aug 2008
Posts: 1769
Location: United Kingdom

PostPosted: Sun Oct 05, 2014 5:28 pm    Post subject: Reply with quote

sicvolo, thank you for posting. Although the single script using Zenity that I posted back in July 2009 still works fine on my machines, just out of curiosity I tried your two scripts (I only modified the directory path to get_dbus.sh in the post-processing script, to suit my installation) and they work, so kudos. Presumably your two scripts could be conflated, though?


EDIT Mon 6 Oct 00:26:56 BST 2014 You can replace the two scripts with the following single script (which I named cups-pdf_script.sh):

Code:
#!/bin/bash

get_dbus()
{
if [ -z $1 ]; then
    echo "specify user" >> $HOME/cups-pdf_script.log
    return 1
fi
# Search these processes for the session variable (they are run as the current user and have the DBUS session variable set)
compatiblePrograms=( kdeinit kded4 pulseaudio trackerd )

# Attempt to get a program pid
for index in ${compatiblePrograms[@]}; do
   PID=$(ps -ef | grep $1 | grep ${index} | head -1 | awk '{print $2}')
   if [[ "${PID}" != "" ]]; then
      break
   fi
done
if [[ "${PID}" == "" ]]; then
   echo "Could not detect active login session" >> $HOME/cups-pdf_script.log
   return 1
fi
QUERY_ENVIRON="$(tr '\0' '\n' < /proc/${PID}/environ | grep "DBUS_SESSION_BUS_ADDRESS" | cut -d "=" -f 2-)"
if [[ "${QUERY_ENVIRON}" != "" ]]; then
   echo ${QUERY_ENVIRON} >> $HOME/cups-pdf_script.log
   return 0
else
   echo "Could not find dbus session ID in user environment." >> $HOME/cups-pdf_script.log
   return 1
fi
}

CURRENT_PDF="$1"
CURRENT_USER="$2"

export DISPLAY=:0
export XAUTHORITY=/home/${CURRENT_USER}/.Xauthority
export HOME=/home/${CURRENT_USER}/
CONFIG_FILE="/home/${CURRENT_USER}/.pdf-writer.conf"
CANCELLED="No"

date > $HOME/cups-pdf_script.log

get_dbus ${CURRENT_USER}
export DBUS_SESSION_BUS_ADDRESS=${QUERY_ENVIRON}

while [ True ]; do
   CURDATE=$(/bin/date +%Y%m%d)
   CURNAME=$(/bin/cat "$CONFIG_FILE")
   BASENAME=$(basename $CURRENT_PDF)
   FILENAME=$(/usr/bin/kdialog --getsavefilename "$CURNAME$CURDATE-" "*.pdf" --title="Save PDF")
   if [ $? -eq 1 ]; then
      CANCELLED="Yes"
      break
   fi
   echo $FILENAME
   if [ ! "$FILENAME" = "" ]; then
      if [ -e "$FILENAME" ]; then
         /usr/bin/kdialog --warningcontinuecancel "File already exists"
         if [ $? -eq 1 ]; then
            continue;
         fi
      fi
      FILENAME=$(echo $FILENAME.pdf | sed -re "s/(\.pdf)+$/.pdf/g")
      break;
   else
      /usr/bin/kdialog --error "You must select a file or hit Cancel."
   fi
   break
done

if [ "${CANCELLED}" == "No" ]; then
   /bin/cp "${CURRENT_PDF}" "${FILENAME}"
   okular "${FILENAME}" &
fi
/bin/rm "${CURRENT_PDF}"
exit 0

_________________
Clevo W230SS: amd64 nvidia-drivers & xf86-video-intel.
Compal NBLB2: ~amd64 xf86-video-ati. Dual boot Win 7 Pro 64-bit.
OpenRC eudev elogind & KDE on both.

Fitzcarraldo's blog
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum