~ubuntu-branches/ubuntu/lucid/gnome-subtitles/lucid

« back to all changes in this revision

Viewing changes to sublib-0.7/src/SubLib/Application/SubtitleCollection.cs

  • Committer: Bazaar Package Importer
  • Author(s): Tiago Bortoletto Vaz
  • Date: 2007-12-03 20:52:52 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071203205252-2y6uuv4gcw9mi9n5
Tags: 0.7-1
* New upstream release;
* Add libxml-parser-perl to Build-Depends-Indep. Thanks to Lucas Nussbaum.
  (Closes: #445799);
* Fixes manpage issue with dpatch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is part of SubLib.
3
 
 * Copyright (C) 2005-2006 Pedro Castro
4
 
 *
5
 
 * SubLib is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * SubLib is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
using System;
21
 
using System.Collections;
22
 
 
23
 
namespace SubLib {
24
 
        
25
 
/// <summary>A container that represents all the subtitles.</summary>
26
 
public class SubtitleCollection {
27
 
        private ArrayList subtitles = new ArrayList();
28
 
        
29
 
        /// <summary>The number of subtitles in the collection.</summary>
30
 
        public int Count {
31
 
                get { return subtitles.Count; }
32
 
        }
33
 
        
34
 
        /// <summary>Returns an enumerator that can iterate through the collection.</summary>
35
 
        /// <returns>An <see cref="IEnumerator" /> for the entire <see cref="SubtitleCollection" />.</returns>
36
 
        public IEnumerator GetEnumerator () {
37
 
                return subtitles.GetEnumerator();
38
 
        }
39
 
        
40
 
        public Subtitle this [int index] {
41
 
                get {
42
 
                        try {
43
 
                                return subtitles[index] as Subtitle;
44
 
                        }
45
 
                        catch (ArgumentOutOfRangeException) {
46
 
                                return null;
47
 
                        }
48
 
                }
49
 
        }
50
 
        
51
 
        /// <summary>Returns the subtitle at the specified index.</summary>
52
 
        /// <param name="index">The zero-based subtitle's index.</param>
53
 
        /// <returns>The subtitle at the specified index, or null in case the index is invalid.</returns>
54
 
        public Subtitle Get (int index) {
55
 
                if ((index >= 0) && (index < Count))
56
 
                        return (Subtitle)subtitles[index];
57
 
                else
58
 
                        return null;
59
 
        }
60
 
        
61
 
        /// <summary>Adds a subtitle to the end of the collection.</summary>
62
 
        /// <param name="subtitle">The subtitle to add.</param>
63
 
        public void Add (Subtitle subtitle){
64
 
                subtitles.Add(subtitle);
65
 
        }
66
 
        
67
 
        /// <summary>Adds a subtitle to the collection, inserting it at the specified index.</summary>
68
 
        /// <param name="subtitle">The subtitle to add.</param>
69
 
        /// <param name="index">The zero-based index at which the subtitle should be inserted.</param>
70
 
        public void Add (Subtitle subtitle, int index){
71
 
                subtitles.Insert(index, subtitle);
72
 
        }
73
 
        
74
 
        /// <summary>Creates a subtitle based on the subtitle at the specified index and adds it to the
75
 
        /// collection, inserting it right before that index.</summary>
76
 
        /// <remarks>The newly created subtitle's times will be based on the specified subtitle. Its end
77
 
        /// time will be the start time of the existing subtitle minus <see cref="SubtitleConstants.MinTimeBetweenSubtitles" />.
78
 
        /// Its duration will be <see cref="SubtitleConstants.MaxSingleLineSubtitleDuration" />. Both times will be wrapped to
79
 
        /// zero if they are less than zero.</remarks>
80
 
        /// <param name="index">The zero-based index before which the subtitle should be inserted.</param>
81
 
        /// <param name="subtitleProperties">The SubtitleProperties of the subtitles.</param>
82
 
        /// <returns>True if the subtitle could be added, false otherwise.</returns>
83
 
        public bool AddNewBefore (int index, SubtitleProperties subtitleProperties) {
84
 
                Subtitle existing = Get(index);
85
 
                if (existing == null)
86
 
                        return false;
87
 
                
88
 
                TimeSpan subtitleEnd = existing.Times.Start - TimeSpan.FromSeconds(SubtitleConstants.MinTimeBetweenSubtitles);
89
 
                if (subtitleEnd < TimeSpan.Zero)
90
 
                        subtitleEnd = TimeSpan.FromSeconds(0);
91
 
 
92
 
                TimeSpan subtitleStart = subtitleEnd - TimeSpan.FromSeconds(SubtitleConstants.MaxSingleLineSubtitleDuration);
93
 
                if (subtitleStart < TimeSpan.Zero)
94
 
                        subtitleStart = TimeSpan.FromSeconds(0);
95
 
 
96
 
                Subtitle subtitle = new Subtitle(subtitleProperties, subtitleStart, subtitleEnd);
97
 
                Add(subtitle, index);
98
 
                return true;
99
 
        }
100
 
 
101
 
        /// <summary>Creates a subtitle based on the subtitle at the specified index and adds it to the
102
 
        /// collection, inserting it right after that index.</summary>
103
 
        /// <remarks>The newly created subtitle's times will be based on the specified subtitle. Its start
104
 
        /// time will be the start time of the existing subtitle plus <see cref="SubtitleConstants.MinTimeBetweenSubtitles" />.
105
 
        /// Its duration will be <see cref="SubtitleConstants.MaxSingleLineSubtitleDuration" />.</remarks>
106
 
        /// <param name="index">The zero-based index after which the subtitle should be inserted.</param>
107
 
        /// <param name="subtitleProperties">The SubtitleProperties of the subtitles.</param>
108
 
        /// <returns>True if the subtitle could be added, false otherwise.</returns>
109
 
        public bool AddNewAfter (int index, SubtitleProperties subtitleProperties) {
110
 
                Subtitle existing = Get(index);
111
 
                if (existing == null)
112
 
                        return false;
113
 
                
114
 
                TimeSpan subtitleStart = existing.Times.End + TimeSpan.FromSeconds(SubtitleConstants.MinTimeBetweenSubtitles);
115
 
                TimeSpan subtitleEnd = subtitleStart + TimeSpan.FromSeconds(SubtitleConstants.MaxSingleLineSubtitleDuration);
116
 
                Subtitle subtitle = new Subtitle(subtitleProperties, subtitleStart, subtitleEnd);
117
 
                Add(subtitle, index + 1);
118
 
                return true;
119
 
        }
120
 
        
121
 
        /// <summary>Creates a subtitle and adds it to the collection, inserting it at the specified index.</summary>
122
 
        /// <remarks>The newly created subtitle's start time will be zero and its duration will be
123
 
        /// <see cref="SubtitleConstants.MaxSingleLineSubtitleDuration" />.</remarks>
124
 
        /// <param name="index">The zero-based index at which the subtitle should be inserted.</param>
125
 
        /// <param name="subtitleProperties">The SubtitleProperties of the subtitles.</param>
126
 
        /// <returns>True if the subtitle could be added, false otherwise.</returns>
127
 
        public bool AddNewAt (int index, SubtitleProperties subtitleProperties) {
128
 
                if ((index < 0) || (index > Count))
129
 
                        return false;
130
 
                
131
 
                TimeSpan subtitleStart = TimeSpan.FromSeconds(0);
132
 
                TimeSpan subtitleEnd = TimeSpan.FromSeconds(SubtitleConstants.MaxSingleLineSubtitleDuration);
133
 
                Subtitle subtitle = new Subtitle(subtitleProperties, subtitleStart, subtitleEnd);
134
 
                Add(subtitle, index);
135
 
                return true;
136
 
        }
137
 
        
138
 
        /// <summary>Checks whether a subtitle with the specified index exists in the collection.</summary>
139
 
        /// <param name="index">The zero-based index.</param>
140
 
        /// <returns>Whether the index is contained within the collection.</returns>
141
 
        public bool Contains (int index) {
142
 
                return (index >= 0) && (index < Count);
143
 
        }
144
 
        
145
 
        /// <summary>Removes a subtitle from the collection, given its index.</summary>
146
 
        /// <param name="index">The zero-based index of the subtitle to be removed.</param>
147
 
        public void Remove (int index) {
148
 
                subtitles.RemoveAt(index);
149
 
        }
150
 
 
151
 
        public override string ToString(){
152
 
                string result = "\t* SUBTITLE LIST *\n";
153
 
                foreach(Subtitle subtitle in subtitles){
154
 
                        result += subtitle.ToString();
155
 
                }
156
 
                return result;
157
 
        }
158
 
        
159
 
        /* Internal methods */
160
 
        
161
 
        internal void SetPropertiesForAll (SubtitleProperties properties) {
162
 
                foreach (Subtitle subtitle in subtitles)
163
 
                        subtitle.Properties = properties;
164
 
        }
165
 
 
166
 
}
167
 
 
168
 
}