~ubuntu-branches/ubuntu/karmic/partitionmanager/karmic

« back to all changes in this revision

Viewing changes to src/core/partitionnode.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2009-01-23 17:57:36 UTC
  • Revision ID: james.westby@ubuntu.com-20090123175736-2ltrhgg3m55dokbm
Tags: upstream-1.0.0~beta1a
ImportĀ upstreamĀ versionĀ 1.0.0~beta1a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2008 by Volker Lanz <vl@fidra.de>                       *
 
3
 *                                                                         *
 
4
 *   This program is free software; you can redistribute it and/or modify  *
 
5
 *   it under the terms of the GNU General Public License as published by  *
 
6
 *   the Free Software Foundation; either version 2 of the License, or     *
 
7
 *   (at your option) any later version.                                   *
 
8
 *                                                                         *
 
9
 *   This program is distributed in the hope that it will be useful,       *
 
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
12
 *   GNU General Public License for more details.                          *
 
13
 *                                                                         *
 
14
 *   You should have received a copy of the GNU General Public License     *
 
15
 *   along with this program; if not, write to the                         *
 
16
 *   Free Software Foundation, Inc.,                                       *
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 
18
 ***************************************************************************/
 
19
 
 
20
#include "core/partitionnode.h"
 
21
 
 
22
#include "core/partition.h"
 
23
#include "core/partitionrole.h"
 
24
 
 
25
#include "fs/filesystem.h"
 
26
 
 
27
 
 
28
#include <kdebug.h>
 
29
 
 
30
/** Tries to find the predecessor for a Partition.
 
31
        @param p the Partition to find a predecessor for
 
32
        @return pointer to the predecessor or NULL if none was found
 
33
*/
 
34
Partition* PartitionNode::predecessor(Partition& p)
 
35
{
 
36
        Q_ASSERT(p.parent());
 
37
 
 
38
        Partitions& plist = p.parent()->isRoot() == false ? p.parent()->children() : children();
 
39
 
 
40
        for (int idx = 1; idx < plist.size(); idx++)
 
41
                if (plist[idx] == &p)
 
42
                        return plist[idx - 1];
 
43
 
 
44
        return NULL;
 
45
}
 
46
 
 
47
/**
 
48
        @overload
 
49
*/
 
50
const Partition* PartitionNode::predecessor(const Partition& p) const
 
51
{
 
52
        Q_ASSERT(p.parent());
 
53
 
 
54
        const Partitions& plist = p.parent()->isRoot() == false ? p.parent()->children() : children();
 
55
 
 
56
        for (int idx = 1; idx < plist.size(); idx++)
 
57
                if (plist[idx] == &p)
 
58
                        return plist[idx - 1];
 
59
 
 
60
        return NULL;
 
61
}
 
62
 
 
63
/** Tries to find the successor for a Partition.
 
64
        @param p the Partition to find a successor for
 
65
        @return pointer to the successor or NULL if none was found
 
66
 */
 
67
Partition* PartitionNode::successor(Partition& p)
 
68
{
 
69
        Q_ASSERT(p.parent());
 
70
 
 
71
        Partitions& plist = p.parent()->isRoot() == false ? p.parent()->children() : children();
 
72
 
 
73
        for (int idx = plist.size() - 2; idx >= 0; idx--)
 
74
                if (plist[idx] == &p)
 
75
                        return plist[idx + 1];
 
76
 
 
77
        return NULL;
 
78
}
 
79
 
 
80
/**
 
81
        @overload
 
82
*/
 
83
const Partition* PartitionNode::successor(const Partition& p) const
 
84
{
 
85
        Q_ASSERT(p.parent());
 
86
 
 
87
        const Partitions& plist = p.parent()->isRoot() == false ? p.parent()->children() : children();
 
88
 
 
89
        for (int idx = plist.size() - 2; idx >= 0; idx--)
 
90
                if (plist[idx] == &p)
 
91
                        return plist[idx + 1];
 
92
 
 
93
        return NULL;
 
94
}
 
95
 
 
96
/** Inserts a Partition into a PartitionNode's children
 
97
        @param p pointer to the Partition to insert. May be NULL.
 
98
        @return true on success
 
99
*/
 
100
bool PartitionNode::insert(Partition* p)
 
101
{
 
102
        if (p == NULL)
 
103
                return false;
 
104
 
 
105
        for (int idx = 0; idx < children().size(); idx++)
 
106
        {
 
107
                if (children()[idx]->firstSector() > p->firstSector())
 
108
                {
 
109
                        children().insert(idx, p);
 
110
                        return true;
 
111
                }
 
112
        }
 
113
 
 
114
        children().insert(children().size(), p);
 
115
 
 
116
        return true;
 
117
}
 
118
 
 
119
/** Removes a Partition from the PartitionNode's children.
 
120
        @param p pointer to the Partition to remove. May be NULL.
 
121
        @return true on success.
 
122
*/
 
123
bool PartitionNode::remove(Partition* p)
 
124
{
 
125
        if (p == NULL)
 
126
                return false;
 
127
 
 
128
        if (children().removeOne(p))
 
129
                return true;
 
130
 
 
131
        return false;
 
132
}
 
133
 
 
134
/** Deletes all children */
 
135
void PartitionNode::clearChildren()
 
136
{
 
137
        qDeleteAll(children());
 
138
        children().clear();
 
139
}
 
140
 
 
141
/** Finds a Partition by sector.
 
142
        @param s the sector the Partition is at
 
143
        @param role the PartitionRole the Partition is supposed to have
 
144
        @return pointer to the Partition found or NULL if none was found
 
145
*/
 
146
Partition* PartitionNode::findPartitionBySector(qint64 s, const PartitionRole& role)
 
147
{
 
148
        foreach (Partition* p, children())
 
149
        {
 
150
                // (women and) children first. ;-)
 
151
                foreach (Partition* child, p->children())
 
152
                        if ((child->roles().roles() & role.roles()) && s >= child->firstSector() && s <= child->lastSector())
 
153
                                return child;
 
154
                
 
155
                if ((p->roles().roles() & role.roles()) && s >= p->firstSector() && s <= p->lastSector())
 
156
                        return p;
 
157
        }
 
158
 
 
159
        return NULL;
 
160
}
 
161
 
 
162
/**
 
163
        @overload
 
164
*/
 
165
const Partition* PartitionNode::findPartitionBySector(qint64 s, const PartitionRole& role) const
 
166
{
 
167
        foreach (const Partition* p, children())
 
168
        {
 
169
                foreach (const Partition* child, p->children())
 
170
                        if ((child->roles().roles() & role.roles()) && s >= child->firstSector() && s <= child->lastSector())
 
171
                                return child;
 
172
 
 
173
                if ((p->roles().roles() & role.roles()) && s >= p->firstSector() && s <= p->lastSector())
 
174
                        return p;
 
175
        }
 
176
 
 
177
        return NULL;
 
178
}
 
179
 
 
180
/** Reparents a Partition to this PartitionNode
 
181
        @param p the Partition to reparent
 
182
*/
 
183
void PartitionNode::reparent(Partition& p)
 
184
{
 
185
        p.setParent(this);
 
186
 
 
187
        if (!isRoot())
 
188
                p.setRoles(PartitionRole(PartitionRole::Logical));
 
189
        else if (!p.roles().has(PartitionRole::Extended))
 
190
                p.setRoles(PartitionRole(PartitionRole::Primary));
 
191
        else
 
192
                p.setRoles(PartitionRole(PartitionRole::Extended));
 
193
}
 
194
 
 
195
/** @return the number of the highest mounted child, e.g. 7 if /dev/sdd7 is a child of this PartitionNode and mounted and /dev/sdd8 and /dev/sdd9 and so on aren't
 
196
*/
 
197
qint32 PartitionNode::highestMountedChild() const
 
198
{
 
199
        qint32 result = -1;
 
200
 
 
201
        foreach (const Partition* p, children())
 
202
                if (p->number() > result && p->isMounted())
 
203
                        result = p->number();
 
204
        
 
205
        return result;
 
206
}
 
207
 
 
208
/** @return true if any of the partition's children are mounted */
 
209
bool PartitionNode::isChildMounted() const
 
210
{
 
211
        foreach (const Partition* child, children())
 
212
                if (child->isMounted() || (child->hasChildren() && child->isChildMounted()))
 
213
                        return true;
 
214
 
 
215
        return false;
 
216
}