summaryrefslogtreecommitdiffstats
path: root/krename/dateplugin.cpp
blob: 31522d214e31b785c47ba4f472ca1c318ece4711 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/***************************************************************************
                         dateplugin.cpp  -  description
                             -------------------
    begin                : Mon Feb 02 2004
    copyright            : (C) 2004 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include "dateplugin.h"

// TQt includes
#include <tqdatetime.h>

// KDE includes
#include <tdefileitem.h>
#include <tdeio/netaccess.h>
#include <tdelocale.h>

DatePlugin::DatePlugin()
 : FilePlugin(0)
{
    keys.append("date");
    keys.append("date;.*");
    keys.append("year");
    keys.append("month");
    keys.append("day");
    keys.append("time");
    keys.append("hour");
    keys.append("minute");
    keys.append("second");
    keys.append("user");
    keys.append("group");
    keys.append("creationdate");
    keys.append("creationdate;.*");
    keys.append("modificationdate");
    keys.append("modificationdate;.*");
    keys.append("accessdate");
    keys.append("accessdate;.*");
    setupKeys();
 
    m_icon = "clock";   
}


TQString DatePlugin::processFile(BatchRenamer* b, int i, TQString token, int )
{
    if( !supports( token ) )
        return TQString();
    
    if( token.lower().startsWith( getPattern() ) )
        token = token.mid( getPattern().length(), token.length() - getPattern().length() );
      
    
    TQDate d = TQDate::currentDate();
    TQTime t = TQTime::currentTime();
    TQString tmp, text;
    TQString format = "dd-MM-yyyy";
    if( token.contains( ";" ) )
    {
        format = token.section( ';', 1, 1 );
        token = token.section( ';', 0, 0 ).lower();
    } else 
        token = token.lower();

    if( token == "date" ) {
        return TQDateTime::currentDateTime().toString( format );
    } else if( token == "year" )
        return TQString( "%1" ).arg( d.year() );
    else if( token == "month" )
        return tmp.sprintf("%0*i", 2, d.month() );
    else if( token == "day" )
        return tmp.sprintf("%0*i", 2, d.day() );
    else if( token == "time" )
        return TQString( "%1-%2-%3" ).arg( t.hour() ).arg( TQString().sprintf("%0*i", 2, t.minute() ) ).arg( TQString().sprintf("%0*i", 2, t.second() ) );
    else if( token == "hour" )
        return tmp.sprintf("%0*i", 2, t.hour() );
    else if( token == "minute" )
        return tmp.sprintf("%0*i", 2, t.minute() );
    else if( token == "second" )
        return tmp.sprintf("%0*i", 2, t.second() );
    else {
        TDEIO::UDSEntry entry;
        TDEIO::NetAccess::stat( b->files()[i].src.url, entry );
        KFileItem item( entry, b->files()[i].src.url );
        if( token == "user" )
            return item.user();
        else if( token == "group" )
            return item.group();
        else if( token == "creationdate" )
            return time( item.time( TDEIO::UDS_CREATION_TIME ), format );
        else if( token == "modificationdate" )
            return time( item.time( TDEIO::UDS_MODIFICATION_TIME ), format );
        else if( token == "accessdate" )
            return time( item.time( TDEIO::UDS_ACCESS_TIME ), format );
        
    }
    
    return TQString();    
}

const TQString DatePlugin::getAccelName() const
{
    return i18n("&System Functions");
}

const TQString DatePlugin::getName() const
{
    return i18n("System Functions");
}

const TQString DatePlugin::getPattern() const
{
    return TQString();
}

const TQString DatePlugin::time( time_t time, const TQString & format )
{
    TQDateTime dt;
    dt.setTime_t( time );
    return dt.toString( format );
}

void DatePlugin::addHelp( HelpDialogData* data )
{
    TQStringList list;
    list.append( "[date];;" + i18n("Insert the current date") );
    list.append( "[date;yyyy-MM-dd];;" + i18n("Insert the current date using the formatting string yyyy-MM-dd") );
    list.append( "[year];;" + i18n("Insert the current year") );
    list.append( "[month];;" + i18n("Insert the current month as number") );
    list.append( "[day];;" + i18n("Insert the current day as number") );
    list.append( "[time];;" + i18n("Insert the current time") );
    list.append( "[hour];;" + i18n("Insert the current hour as number") );
    list.append( "[minute];;" + i18n("Insert the current minute as number") );
    list.append( "[second];;" + i18n("Insert the current second as number") );
    list.append( "[user];;" + i18n("Owner of the file") );
    list.append( "[group];;" + i18n("Owning group of the file") );
    list.append( "[creationdate];;" + i18n("Insert the files creation date"));
    list.append( "[creationdate;yyyy-MM-dd];;" + i18n("Insert the formatted file creation date") );
    list.append( "[modificationdate];;" + i18n("Insert the files modification date"));
    list.append( "[modificationdate;yyyy-MM-dd];;" + i18n("Insert the formatted modification date") );
    list.append( "[accessdate];;" + i18n("Insert the date of the last file access") );
    list.append( "[accessdate;yyyy-MM-dd];;" + i18n("Insert the formatted date of the last file access") );

    data->add( getName(), &list, getIcon() );
}