lördag 20 oktober 2012

Simple version control of your files

This litle script under the action menu in dolphin comes in handy when you often need to do copies of some files in one folder.
I use it when i am scripting and need to know that different script(files) are in sync with each other. It adds date and time to the end of the filename--20121020-2236.

Save this to a file named myAction.desktop ( the .desktop is the important part of the name ) in this folder  /home/USER/.kde/share/kde4/services/ . Changeg USER to  your username.
[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=all/allfiles
Actions=backupFile

[Desktop Action backupFile]
Name=Copy file(s) and add date-time to filename(s) 
Icon=background
Exec=~/bin/cpRename.sh %F


Then in /home/USER/bin folder  save this to a file named cpRename.sh and make it executable.
#!/bin/bash
 
addDate=$(date +%Y%m%d) #20121020
addTime=$(date +%k%M) #1743
addText=$(kdialog --inputbox "Add additional text ( optional )")
isCansled=$? # Check if text was added or if the dialog was cancelled

# $@ is all the selected files and folders
for file in "${@}"
do
  # checks if it is a file so that not a folder is selected by mistake
  if [ -f "$file" ]
  then
    if [ $isCansled == 1 ]
      then
      new_name="$file--$addDate-$addTime"
      cp "$file" "$new_name"
    else
      new_name="$file--$addDate-$addTime-$addText"
      cp "$file" "$new_name"
    fi
  fi
done


Now restart Dolphin and select some files, right click and select actions and now ther should be a submenu with the name "Copy file(s) and add date-time to filename(s)"

tisdag 2 oktober 2012

Qt quick folderlistmodel

To be able to use folderlistmodel in qml, install it like this:
sudo apt-get install libqt4-declarative-folderlistmodel
and use it like this:
import QtQuick 1.1
import Qt.labs.folderlistmodel 1.0

ListView {
  id:myListView
     width: 200; height: 400

    FolderListModel {
         id: folderModel
         nameFilters: ["*.svg"]
         folder: "./images/"
         showDirs:false
     }

    Component{
      id:fileDelegate
 
      Item{
        width:myListView.width
        height:myListView.height/10
  
        Text { id :imageInfo 
          text: "file name",fileName
          width:parent.width 
          height:imageInfo.text.height 
         }
        Image { source: filePath 
          anchors.top:imageInfo.bottom
          width:parent.width 
          height:parent.height}
      }
    }
     
  model: folderModel
  delegate: fileDelegate
}
Read more here: folderlistmodel