Fixed an error with deletion of a shared resource (filePath-object) ...
[emufront] / src / dataobjects / mediaimagecontainer.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // EmuFront is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
19
20 #include <QDebug>
21 #include "mediaimagecontainer.h"
22
23 MediaImageContainer::MediaImageContainer()
24     : EmuFrontFile(EmuFrontFile::FileType_MediaImageContainer)
25 {
26     lstMediaImage = QList<MediaImage*>();
27     //setup = 0;
28     filePath = 0;
29 }
30
31 MediaImageContainer::MediaImageContainer(int id, QString name, 
32     QString checksum, int size, QList<MediaImage *>images/*, Setup *setup*/, FilePathObject *fpo)
33     : EmuFrontFile(id, name, checksum, size, EmuFrontFile::FileType_MediaImageContainer),
34         lstMediaImage(images)/*, setup(setup)*/, filePath(fpo)
35 { }
36
37 MediaImageContainer::MediaImageContainer(QString name, QString checksum,
38     int size, QList<MediaImage *>images/*, Setup *setup*/, FilePathObject *fpo)
39     : EmuFrontFile(-1, name, checksum, size, EmuFrontFile::FileType_MediaImageContainer),
40         lstMediaImage(images)/*, setup(setup)*/, filePath(fpo)
41 { }
42
43 MediaImageContainer::~MediaImageContainer()
44 {
45     qDeleteAll(lstMediaImage);
46     //delete setup;
47     //delete filePath; // TODO: this filePath object is shared resource and cannot be deleted here!
48                     // take care of the deletion where created!!!
49 }
50
51 MediaImageContainer::MediaImageContainer(MediaImageContainer &mic)
52     : EmuFrontFile(mic)
53 {
54     lstMediaImage = QList<MediaImage*>();
55     foreach(MediaImage *mi, mic.lstMediaImage)
56         lstMediaImage.append(new MediaImage(*mi));
57     //setup = new Setup(*(mic.setup));
58     filePath = new FilePathObject(*(mic.filePath));
59 }
60
61 MediaImageContainer& MediaImageContainer::operator =(MediaImageContainer &mic)
62 {
63      if (this == &mic) return *this;
64     id = mic.id;
65     name = mic.name;
66     type = mic.type;
67     checkSum = mic.checkSum;
68     size = mic.size;
69     qDeleteAll(lstMediaImage);
70     foreach(MediaImage *mi, mic.lstMediaImage)
71         lstMediaImage.append(new MediaImage(*mi));
72     //setup = new Setup(*(mic.setup));
73     filePath = new FilePathObject(*(mic.filePath));
74     return (*this);
75 }
76
77 void MediaImageContainer::setMediaImages(QList<MediaImage *> list)
78 {
79     qDeleteAll(lstMediaImage);
80     lstMediaImage = list;
81 }
82
83 QList<MediaImage *> MediaImageContainer::getMediaImages() const
84 {   return lstMediaImage; }
85
86 void MediaImageContainer::addMediaImage(MediaImage *mi)
87 {   lstMediaImage.append(mi); }
88
89 void MediaImageContainer::clearMediaImages()
90 {
91     qDeleteAll(lstMediaImage);
92     /*foreach(MediaImage *mi, lstMediaImage) {
93         delete mi;
94         mi = 0;
95     }*/
96     lstMediaImage.clear();
97     qDebug() << lstMediaImage.count();
98 }
99
100 /*Setup* MediaImageContainer::getSetup() const
101 { return setup; }
102
103 void MediaImageContainer::setSetup(Setup *s)
104 { setup = s; }*/
105
106 FilePathObject* MediaImageContainer::getFilePath() const
107 { return filePath; }
108
109 void MediaImageContainer::setFilePath(FilePathObject *fp)
110 { filePath = fp; }