15 #include <QTextStream> 21 #include <QApplication> 35 mActivePatientFolder = this->getNoPatientFolder();
38 QTimer::singleShot(100,
this, SLOT(startupLoadPatient()));
53 bool valid = this->isValidSessionFolder(dir);
54 bool exists = this->folderExists(dir);
58 this->loadSession(dir);
62 this->initializeNewSession(dir);
66 reportError(QString(
"Failed to initialize session in existing folder with no valid session: %1").arg(dir));
70 QString SessionStorageServiceImpl::getNoPatientFolder()
const 76 bool SessionStorageServiceImpl::isValidSessionFolder(QString dir)
const 78 QString filename = QDir(dir).absoluteFilePath(this->getXmlFileName());
79 return QFileInfo::exists(filename);
82 bool SessionStorageServiceImpl::folderExists(QString dir)
const 84 return QFileInfo(dir).exists() && QFileInfo(dir).isDir();
87 void SessionStorageServiceImpl::loadSession(QString dir)
89 if (this->isActivePatient(dir))
91 this->loadPatientSilent(dir);
92 this->reportActivePatient();
93 this->writeRecentPatientData();
96 void SessionStorageServiceImpl::initializeNewSession(QString dir)
98 this->clearPatientSilent();
99 dir = this->convertToValidFolderName(dir);
100 this->createPatientFolders(dir);
101 this->setActivePatient(dir);
103 this->reportActivePatient();
104 this->writeRecentPatientData();
107 QString SessionStorageServiceImpl::getXmlFileName()
const 109 return "custusdoc.xml";
119 this->generateSaveDoc(doc.
doc());
121 QDomElement element = doc.
doc().documentElement();
124 QString filename = QDir(mActivePatientFolder).absoluteFilePath(this->getXmlFileName());
126 report(
"Saved patient " + mActivePatientFolder);
131 this->clearPatientSilent();
132 this->reportActivePatient();
133 this->writeRecentPatientData();
138 return !mActivePatientFolder.isEmpty() &&
139 (mActivePatientFolder != this->getNoPatientFolder());
144 return mActivePatientFolder;
147 void SessionStorageServiceImpl::reportActivePatient()
149 report(
"Set Active Patient: " + mActivePatientFolder);
152 void SessionStorageServiceImpl::setActivePatient(
const QString& activePatientFolder)
154 if (activePatientFolder == mActivePatientFolder)
157 mActivePatientFolder = activePatientFolder;
162 void SessionStorageServiceImpl::onSessionChanged()
167 void SessionStorageServiceImpl::clearPatientSilent()
169 this->setActivePatient(this->getNoPatientFolder());
173 bool SessionStorageServiceImpl::isActivePatient(QString patient)
const 175 return (patient == mActivePatientFolder);
178 void SessionStorageServiceImpl::loadPatientSilent(QString choosenDir)
180 if (this->isActivePatient(choosenDir))
182 this->clearPatientSilent();
183 if (choosenDir == QString::null)
186 QString filename = QDir(choosenDir).absoluteFilePath(this->getXmlFileName());
188 mActivePatientFolder = choosenDir;
193 QDomElement element = doc.documentElement();
204 void SessionStorageServiceImpl::writeRecentPatientData()
210 void SessionStorageServiceImpl::generateSaveDoc(QDomDocument& doc)
212 doc.appendChild(doc.createProcessingInstruction(
"xml version =",
"'1.0'"));
214 QDomElement patientNode = doc.createElement(
"patient");
217 QDomElement versionName = doc.createElement(
"version_name");
218 versionName.appendChild(doc.createTextNode(this->getVersionName()));
219 patientNode.appendChild(versionName);
221 QDomElement activePatientNode = doc.createElement(
"active_patient");
222 activePatientNode.appendChild(doc.createTextNode(mActivePatientFolder.toStdString().c_str()));
223 patientNode.appendChild(activePatientNode);
224 doc.appendChild(patientNode);
227 QString SessionStorageServiceImpl::convertToValidFolderName(QString dir)
const 229 if (!dir.endsWith(
".cx3"))
234 void SessionStorageServiceImpl::createPatientFolders(QString dir)
237 QDir().mkpath(dir+
"/Images");
238 QDir().mkpath(dir+
"/Logs");
239 QDir().mkpath(dir+
"/US_Acq");
242 QString SessionStorageServiceImpl::getVersionName()
244 return QString(
"%1").arg(CustusX_VERSION_STRING);
247 void SessionStorageServiceImpl::clearCache()
257 QString SessionStorageServiceImpl::getCommandLineStartupPatient()
259 int doLoad = QApplication::arguments().indexOf(
"--load");
262 if (doLoad + 1 >= QApplication::arguments().size())
265 QString folder = QApplication::arguments()[doLoad + 1];
272 void SessionStorageServiceImpl::startupLoadPatient()
274 QString folder = this->getCommandLineStartupPatient();
276 if (!folder.isEmpty())
278 report(QString(
"Startup Load [%1] from command line").arg(folder));
281 if (folder.isEmpty() &&
settings()->
value(
"Automation/autoLoadRecentPatient").toBool())
283 folder =
settings()->
value(
"startup/lastPatient").toString();
284 if(this->isValidSessionFolder(folder))
287 double minsSinceLastSave = lastSaveTime.secsTo(QDateTime::currentDateTime())/60;
288 double autoLoadRecentPatientWithinHours =
settings()->
value(
"Automation/autoLoadRecentPatientWithinHours").toDouble();
289 int allowedMinsSinceLastSave = autoLoadRecentPatientWithinHours*60;
290 if (minsSinceLastSave > allowedMinsSinceLastSave)
293 QString(
"Startup Load: Ignored recent patient because %1 hours since last save, limit is %2")
294 .arg(
int(minsSinceLastSave/60))
295 .arg(
int(allowedMinsSinceLastSave/60)));
298 if (!folder.isEmpty())
299 report(QString(
"Startup Load [%1] as recent patient").arg(folder));
303 report(
"Startup Load: Ignored recent patient because it is not valid anymore");
309 if (folder.isEmpty())
virtual bool isNull() const
static void writeXmlFile(QDomDocument &doc, QString &filename)
void reportError(QString msg)
void isLoading(QDomElement &root)
emitted while loading a session. Xml storage is available, getRootFolder() is set to loaded value...
bool removeNonemptyDirRecursively(const QString &dirName)
void isLoadingSecond(QDomElement &root)
Emitted after the isLoading signal, to allow for structures that must be loaded after core structures...
virtual void save()
Save all application data to XML file.
virtual void load(QString dir)
load session from dir, or create new session in this location if none exist
virtual bool isValid() const
virtual ~SessionStorageServiceImpl()
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
QString timestampSecondsFormat()
void sessionChanged()
emitted after change to a new session (new or loaded or cleared)
void setValue(const QString &key, const QVariant &value)
static QString getCachePath()
return path to a folder that is used during execution, will be cleared at start and stop...
QString getSubFolder(QString relative)
return and create a folder/path relative to root. Created if necessary.
Settings * settings()
Shortcut for accessing the settings instance.
void cleared()
emitted when session is cleared, before isLoading is called
static QDomDocument readXmlFile(QString &filename)
SessionStorageServiceImpl(ctkPluginContext *context)
void isSaving(QDomElement &root)
xml storage is available
virtual QString getRootFolder() const
Namespace for all CustusX production code.