~ubuntu-branches/ubuntu/saucy/f-spot/saucy

« back to all changes in this revision

Viewing changes to lib/Hyena/src/Hyena/Hyena.Query/TimeSpanQueryValue.cs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2010-05-24 10:35:57 UTC
  • mfrom: (2.4.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20100524103557-1j0i8f66caybci2n
Tags: 0.7.0-1
* New upstream release 0.7.0
 + First release of the unstable 0.7 development series. Massive changes.
 + Reparenting and detaching support (Anton Keks) (Closes: #559745)
 + A new Mallard-based documentation (Harold Schreckengost)
 + No longer embeds flickrnet, uses distribution copy (Iain Lane)
 + Adoption of a large amount of Hyena functionality (Paul Lange, Peter
   Goetz)
 + No longer embeds gnome-keyring-sharp
 + Completely rewritten import, much faster and less memory hungry (Ruben
   Vermeersch) (Closes: #559080, #492658, #341790, #357811, #426017) (LP:
   #412091)
 + No longer use gphoto2-sharp, now uses gvfs which is less crash-pron
   (Ruben Vermeersch)
 + Fix Facebook support (Ruben Vermeersch)
 + Modernized unit tests
 + Revamped build (Mike Gemünde)
 + Much improved duplicate detection (much faster too) (Ruben Vermeersch)
 + Mouse selection in Iconview (Vincent Pomey)
 + Image panning support using middle mouse button (Wojciech Dzierżanowski)
 + Timeline slider now restricted to the size of the window (Iain Churcher)
 + Over 100 bugs closed (http://bit.ly/cyVjnD)
   - No more warnings about schema defaults (Closes: #584215) (LP: #586132)
* debian/control: Clean up build deps to match configure checks
* debian/rules: Don't run dh_makeshilbs as we don't ship any shared
  libraries. There are some private ones though, which get picked up and
  result in a useless postinst/postrm call to ldconfig. Thanks, lintian.
* debian/patches/debian_fix-distclean.patch,
  debian/patches/debian_fix_f-spot.exe.config.patch,
  debian/patches/debian_link-system-flickrnet.patch,
  debian/patches/debian_link-system-gnome-keyring.patch,
  debian/patches/debian_disable-unit-tests,
  debian/patches/git_transition_duration.patch,
  debian/patches/ubuntu_fix_folder_export_hang.patch:
  Clean up obsolete patches which are no longer necessary 
* debian/patches/*: Temporarily disable patches which originated from Ubuntu
  and no longer apply cleanly. We will get these back in a future upstream
  development release.
* debian/patches/*: Refresh to apply cleanly 
* debian/rules: Add new include dir to autoreconf call to pick up f-spot
  macros 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// TimeSpanQueryValue.cs
 
3
//
 
4
// Authors:
 
5
//   Gabriel Burt <gburt@novell.com>
 
6
//
 
7
// Copyright (C) 2007-2008 Novell, Inc.
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
//
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
//
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
using System.Collections.Generic;
 
31
using System.Globalization;
 
32
using System.Xml;
 
33
using System.Text;
 
34
using System.Text.RegularExpressions;
 
35
 
 
36
using Mono.Unix;
 
37
 
 
38
using Hyena;
 
39
 
 
40
namespace Hyena.Query
 
41
{
 
42
    public enum TimeFactor {
 
43
        Second = 1,
 
44
        Minute = 60,
 
45
        Hour   = 3600,
 
46
        Day    = 3600*24,
 
47
        Week   = 3600*24*7,
 
48
        Month  = 3600*24*30,
 
49
        Year   = 3600*24*365
 
50
    }
 
51
 
 
52
    public class TimeSpanQueryValue : IntegerQueryValue
 
53
    {
 
54
        /*public static readonly Operator Equal              = new Operator ("equals", "= {0}", "==", "=", ":");
 
55
        public static readonly Operator NotEqual           = new Operator ("notEqual", "!= {0}", true, "!=", "!:");
 
56
        public static readonly Operator LessThanEqual      = new Operator ("lessThanEquals", "<= {0}", "<=");
 
57
        public static readonly Operator GreaterThanEqual   = new Operator ("greaterThanEquals", ">= {0}", ">=");
 
58
        public static readonly Operator LessThan           = new Operator ("lessThan", "< {0}", "<");
 
59
        public static readonly Operator GreaterThan        = new Operator ("greaterThan", "> {0}", ">");*/
 
60
 
 
61
        protected double offset = 0;
 
62
        protected TimeFactor factor = TimeFactor.Second;
 
63
 
 
64
        public override string XmlElementName {
 
65
            get { return "timespan"; }
 
66
        }
 
67
 
 
68
        //protected static AliasedObjectSet<Operator> operators = new AliasedObjectSet<Operator> (Equal, NotEqual, LessThan, GreaterThan, LessThanEqual, GreaterThanEqual);
 
69
        protected static AliasedObjectSet<Operator> ops = new AliasedObjectSet<Operator> (LessThan, GreaterThan, LessThanEqual, GreaterThanEqual);
 
70
        public override AliasedObjectSet<Operator> OperatorSet {
 
71
            get { return operators; }
 
72
        }
 
73
 
 
74
        public override object Value {
 
75
            get { return offset; }
 
76
        }
 
77
 
 
78
        public virtual double Offset {
 
79
            get { return offset; }
 
80
        }
 
81
 
 
82
        public TimeFactor Factor {
 
83
            get { return factor; }
 
84
        }
 
85
 
 
86
        public double FactoredValue {
 
87
            get { return Offset / (double) factor; }
 
88
        }
 
89
 
 
90
        // FIXME replace period in following with culture-dependent character
 
91
        private static Regex number_regex = new Regex ("\\d+(\\.\\d+)?", RegexOptions.Compiled);
 
92
        public override void ParseUserQuery (string input)
 
93
        {
 
94
            Match match = number_regex.Match (input);
 
95
            if (match != Match.Empty && match.Groups.Count > 0) {
 
96
                double val;
 
97
                try {
 
98
                    val = Convert.ToDouble (match.Groups[0].Captures[0].Value);
 
99
                } catch (FormatException) {
 
100
                    val = Convert.ToDouble (match.Groups[0].Captures[0].Value, NumberFormatInfo.InvariantInfo);
 
101
                }
 
102
                foreach (TimeFactor factor in Enum.GetValues (typeof(TimeFactor))) {
 
103
                    if (input == FactorString (factor, val, true) || input == FactorString (factor, val, false)) {
 
104
                        SetUserRelativeValue (val, factor);
 
105
                        return;
 
106
                    }
 
107
                }
 
108
            }
 
109
            IsEmpty = true;
 
110
        }
 
111
 
 
112
        public override string ToUserQuery ()
 
113
        {
 
114
            return FactorString (factor, FactoredValue, true);
 
115
        }
 
116
 
 
117
        public virtual void SetUserRelativeValue (double offset, TimeFactor factor)
 
118
        {
 
119
            SetRelativeValue (offset, factor);
 
120
        }
 
121
 
 
122
        public void SetRelativeValue (double offset, TimeFactor factor)
 
123
        {
 
124
            this.factor = factor;
 
125
            this.offset = (long) (offset * (double)factor);
 
126
            IsEmpty = false;
 
127
        }
 
128
 
 
129
        public override void LoadString (string val)
 
130
        {
 
131
            try {
 
132
                SetRelativeValue (Convert.ToDouble (val), TimeFactor.Second);
 
133
                DetermineFactor ();
 
134
            } catch {
 
135
                IsEmpty = true;
 
136
            }
 
137
        }
 
138
 
 
139
        protected void DetermineFactor ()
 
140
        {
 
141
            double val = Math.Abs (offset);
 
142
            foreach (TimeFactor factor in Enum.GetValues (typeof(TimeFactor))) {
 
143
                if (val >= (double) factor) {
 
144
                    this.factor = factor;
 
145
                }
 
146
            }
 
147
        }
 
148
 
 
149
        public override void ParseXml (XmlElement node)
 
150
        {
 
151
            try {
 
152
                LoadString (node.InnerText);
 
153
                if (node.HasAttribute ("factor")) {
 
154
                    this.factor = (TimeFactor) Enum.Parse (typeof(TimeFactor), node.GetAttribute ("factor"));
 
155
                }
 
156
            } catch {
 
157
                IsEmpty = true;
 
158
            }
 
159
        }
 
160
 
 
161
        public override void AppendXml (XmlElement node)
 
162
        {
 
163
            base.AppendXml (node);
 
164
            node.SetAttribute ("factor", factor.ToString ());
 
165
        }
 
166
 
 
167
        public override string ToSql (Operator op)
 
168
        {
 
169
            return Convert.ToString (offset * 1000, System.Globalization.CultureInfo.InvariantCulture);
 
170
        }
 
171
 
 
172
        protected virtual string FactorString (TimeFactor factor, double count, bool translate)
 
173
        {
 
174
            string result = null;
 
175
            int plural_count = StringUtil.DoubleToPluralInt (count);
 
176
            if (translate) {
 
177
                switch (factor) {
 
178
                    case TimeFactor.Second: result = Catalog.GetPluralString ("{0} second", "{0} seconds", plural_count); break;
 
179
                    case TimeFactor.Minute: result = Catalog.GetPluralString ("{0} minute", "{0} minutes", plural_count); break;
 
180
                    case TimeFactor.Hour:   result = Catalog.GetPluralString ("{0} hour",   "{0} hours", plural_count); break;
 
181
                    case TimeFactor.Day:    result = Catalog.GetPluralString ("{0} day",    "{0} days", plural_count); break;
 
182
                    case TimeFactor.Week:   result = Catalog.GetPluralString ("{0} week",   "{0} weeks", plural_count); break;
 
183
                    case TimeFactor.Month:  result = Catalog.GetPluralString ("{0} month",  "{0} months", plural_count); break;
 
184
                    case TimeFactor.Year:   result = Catalog.GetPluralString ("{0} year",   "{0} years", plural_count); break;
 
185
                    default: return null;
 
186
                }
 
187
            } else {
 
188
                switch (factor) {
 
189
                    case TimeFactor.Second: result = plural_count == 1 ? "{0} second" : "{0} seconds"; break;
 
190
                    case TimeFactor.Minute: result = plural_count == 1 ? "{0} minute" : "{0} minutes"; break;
 
191
                    case TimeFactor.Hour:   result = plural_count == 1 ? "{0} hour"   : "{0} hours"; break;
 
192
                    case TimeFactor.Day:    result = plural_count == 1 ? "{0} day"    : "{0} days"; break;
 
193
                    case TimeFactor.Week:   result = plural_count == 1 ? "{0} week"   : "{0} weeks"; break;
 
194
                    case TimeFactor.Month:  result = plural_count == 1 ? "{0} month"  : "{0} months"; break;
 
195
                    case TimeFactor.Year:   result = plural_count == 1 ? "{0} year"   : "{0} years"; break;
 
196
                    default: return null;
 
197
                }
 
198
            }
 
199
 
 
200
            return String.Format (result, StringUtil.DoubleToTenthsPrecision (
 
201
                count, false, translate ? NumberFormatInfo.CurrentInfo : NumberFormatInfo.InvariantInfo));
 
202
        }
 
203
    }
 
204
}
 
 
b'\\ No newline at end of file'