| 1 | #include <mythcontext.h> |
| 2 | #include <unistd.h> |
| 3 | #include <iostream> |
| 4 | #include <fstream> |
| 5 | #include <stdio.h> |
| 6 | #include "settings.h" |
| 7 | #include <qsqldatabase.h> |
| 8 | #include <qsqlquery.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <cstdlib> |
| 11 | #include <mythtv/mythdbcon.h> |
| 12 | #include <mythtv/mythdb.h> |
| 13 | |
| 14 | #include "settings_template.h" |
| 15 | |
| 16 | QString TemplateSettings::gather_setting(QString QUERY) |
| 17 | { |
| 18 | QString returnvalue = "" ; |
| 19 | if ( QUERY == "HOSTtemplatetype" ) |
| 20 | returnvalue=HOSTtemplatetype->getValue(); |
| 21 | else if ( QUERY == "HostTemplateSave") |
| 22 | returnvalue=HostTemplateSave->getValue(); |
| 23 | else if ( QUERY == "HostTemplateRestore") |
| 24 | returnvalue=HostTemplateRestore->getValue(); |
| 25 | else if ( QUERY == "HostSelectcopy_from") |
| 26 | returnvalue=HostSelectcopy_from->getValue(); |
| 27 | else if ( QUERY == "HostTemplateCopy_restore") |
| 28 | returnvalue=HostTemplateCopy_restore->getValue(); |
| 29 | else if ( QUERY == "HostTemplateDelete") |
| 30 | returnvalue=HostTemplateDelete->getValue(); |
| 31 | return returnvalue; |
| 32 | } |
| 33 | |
| 34 | void TemplateSettings::fillHOSTtemplatetype() |
| 35 | { |
| 36 | HOSTtemplatetype->setLabel(QObject::tr("Settings options")); |
| 37 | HOSTtemplatetype->addSelection("Restore"); |
| 38 | HOSTtemplatetype->addSelection("Save") ; |
| 39 | HOSTtemplatetype->addSelection("Copy From"); |
| 40 | HOSTtemplatetype->addSelection("Delete"); |
| 41 | HOSTtemplatetype->setHelpText(QObject::tr("Select the command to run.")); |
| 42 | } |
| 43 | |
| 44 | void TemplateSettings::fillHostTemplateSave() |
| 45 | { |
| 46 | HostTemplateSave->setLabel(QObject::tr("Save Template")); |
| 47 | HostTemplateSave->addSelection("user1") ; |
| 48 | HostTemplateSave->addSelection("user2") ; |
| 49 | HostTemplateSave->setHelpText(QObject::tr("Save settings under this name")); |
| 50 | MSqlQuery query(MSqlQuery::InitCon()); |
| 51 | QString tempItem; |
| 52 | query.prepare( "show tables like \"settings_%\" " ); |
| 53 | if (query.exec() && query.isActive() && query.size() > 0) |
| 54 | { |
| 55 | while (query.next()) |
| 56 | { |
| 57 | tempItem = query.value(0).toString(); |
| 58 | tempItem=tempItem.remove(QRegExp("settings_")); |
| 59 | HostTemplateSave->addSelection(tempItem); |
| 60 | VERBOSE(VB_IMPORTANT, "Adding " + tempItem + "to save list"); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void TemplateSettings::fillHostTemplateRestore() |
| 66 | { |
| 67 | HostTemplateRestore->setLabel(QObject::tr("Restore")); |
| 68 | QString tempItem; |
| 69 | QString current_hostname = gContext->GetHostName(); |
| 70 | QString sub_sql; |
| 71 | QString returnvalue; |
| 72 | MSqlQuery query(MSqlQuery::InitCon()); |
| 73 | MSqlQuery sub_query(MSqlQuery::InitCon()); |
| 74 | query.prepare( "show tables like \"settings_%\" " ); |
| 75 | if (query.exec() && query.isActive() && query.size() > 0) |
| 76 | { |
| 77 | while (query.next()) |
| 78 | { |
| 79 | tempItem = query.value(0).toString(); |
| 80 | //if found tables has settings for this hostname add the table |
| 81 | sub_sql="SELECT value from "; |
| 82 | sub_sql+=tempItem; |
| 83 | sub_sql+=" where hostname=:HOSTNAME ;"; |
| 84 | sub_query.prepare(sub_sql); |
| 85 | sub_query.bindValue( ":HOSTNAME" , current_hostname ); |
| 86 | if (sub_query.exec() && sub_query.isActive() && sub_query.size() > 0) |
| 87 | { |
| 88 | tempItem=tempItem.remove(QRegExp("settings_")); |
| 89 | HostTemplateRestore->addSelection(tempItem); |
| 90 | VERBOSE(VB_IMPORTANT, "Adding " + tempItem + "to restore list"); |
| 91 | } |
| 92 | else |
| 93 | VERBOSE(VB_IMPORTANT, "Couldn't find settings for " + |
| 94 | current_hostname + " in " + tempItem); |
| 95 | } |
| 96 | } |
| 97 | // check to see if default is present before adding them |
| 98 | query.prepare ("SELECT count(data) FROM settings_Default WHERE value = 'Theme' AND hostname = 'Default'"); |
| 99 | if (query.exec() && query.isActive() && query.size() > 0) |
| 100 | { |
| 101 | query.next(); |
| 102 | tempItem = query.value(0).toString(); |
| 103 | if ( tempItem == "1" ) |
| 104 | HostTemplateRestore->addSelection("Default"); |
| 105 | } |
| 106 | |
| 107 | query.prepare ("SELECT count(data) FROM settings_Default WHERE value = 'Theme' AND hostname = 'Default_1'"); |
| 108 | if (query.exec() && query.isActive() && query.size() > 0) |
| 109 | { |
| 110 | query.next(); |
| 111 | tempItem = query.value(0).toString(); |
| 112 | if ( tempItem == "1" ) |
| 113 | HostTemplateRestore->addSelection("Default_1"); |
| 114 | } |
| 115 | HostTemplateRestore->setHelpText(QObject::tr("Saved settings to be restored.")); |
| 116 | } |
| 117 | |
| 118 | void TemplateSettings:: fillHostSelectcopy_from() |
| 119 | { |
| 120 | |
| 121 | HostSelectcopy_from->setLabel(QObject::tr("Host")); |
| 122 | QString tempItem; |
| 123 | MSqlQuery query(MSqlQuery::InitCon()); |
| 124 | query.prepare( "SELECT DISTINCT hostname from settings where hostname is not null;"); |
| 125 | if (query.exec() && query.isActive() && query.size() > 0) |
| 126 | { |
| 127 | while (query.next()) |
| 128 | { |
| 129 | tempItem = query.value(0).toString(); |
| 130 | tempItem=tempItem.remove(QRegExp("settings_")); |
| 131 | HostSelectcopy_from->addSelection(tempItem); |
| 132 | } |
| 133 | } |
| 134 | HostSelectcopy_from->setHelpText(QObject::tr("Select the host to copy settings from.")); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | void TemplateSettings::fillHostTemplateCopy_restore() |
| 139 | { |
| 140 | QString tempItem; |
| 141 | HostTemplateCopy_restore->addSelection("Current"); |
| 142 | HostTemplateCopy_restore->setLabel(QObject::tr("Restore")); |
| 143 | QString current_hostname = gContext->GetHostName(); |
| 144 | MSqlQuery query(MSqlQuery::InitCon()); |
| 145 | query.prepare( "show tables like \"settings_%\" " ); |
| 146 | if (query.exec() && query.isActive() && query.size() > 0) |
| 147 | { |
| 148 | while (query.next()) |
| 149 | { |
| 150 | tempItem = query.value(0).toString(); |
| 151 | tempItem=tempItem.remove(QRegExp("settings_")); |
| 152 | if ( tempItem != "Default" ) |
| 153 | { |
| 154 | HostTemplateCopy_restore->addSelection(tempItem); |
| 155 | VERBOSE(VB_IMPORTANT, "Adding " + tempItem + " to copy from list"); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | else |
| 160 | VERBOSE(VB_IMPORTANT, "Didn't find any profiles for copy from list"); |
| 161 | HostTemplateCopy_restore->setHelpText(QObject::tr("Select the settings group to restore.")); |
| 162 | |
| 163 | }; |
| 164 | |
| 165 | void TemplateSettings::fillHostTemplateDelete() |
| 166 | { |
| 167 | HostTemplateDelete->setLabel(QObject::tr("Delete Template")); |
| 168 | HostTemplateDelete->setHelpText(QObject::tr("Delete settings under this name")); |
| 169 | MSqlQuery query(MSqlQuery::InitCon()); |
| 170 | QString tempItem; |
| 171 | query.prepare( "show tables like \"settings_%\" " ); |
| 172 | if (query.exec() && query.isActive() && query.size() > 0) |
| 173 | { |
| 174 | while (query.next()) |
| 175 | { |
| 176 | tempItem = query.value(0).toString(); |
| 177 | tempItem=tempItem.remove(QRegExp("settings_")); |
| 178 | if ( tempItem != "Default" ) |
| 179 | { |
| 180 | HostTemplateDelete->addSelection(tempItem); |
| 181 | VERBOSE(VB_IMPORTANT, "Adding " + tempItem + "to Delete list"); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void TemplateSettings::remove_deleted_item(QString ditem) |
| 188 | { |
| 189 | |
| 190 | HostTemplateDelete->setValue(0); |
| 191 | HostTemplateDelete->removeSelection(ditem); |
| 192 | HostTemplateRestore->removeSelection(ditem); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | TemplateSettings::TemplateSettings(): TriggeredConfigurationGroup(true) |
| 197 | { |
| 198 | setLabel(QObject::tr("Manage Settings")); |
| 199 | HOSTtemplatetype= new HostComboBox("HOSTtemplatetype"); |
| 200 | HostTemplateSave= new HostComboBox("HostTemplateSave",true); |
| 201 | HostTemplateRestore = new HostComboBox("HostTemplateRestore"); |
| 202 | HostSelectcopy_from = new HostComboBox("HostSelectcopy_from"); |
| 203 | HostTemplateCopy_restore = new HostComboBox("HostTemplateCopy_restore"); |
| 204 | HostTemplateDelete = new HostComboBox("HostTemplateDelete"); |
| 205 | |
| 206 | fillHOSTtemplatetype(); |
| 207 | fillHostTemplateSave(); |
| 208 | fillHostTemplateRestore(); |
| 209 | fillHostSelectcopy_from(); |
| 210 | fillHostTemplateCopy_restore(); |
| 211 | fillHostTemplateDelete(); |
| 212 | |
| 213 | Setting* Mtemplate = HOSTtemplatetype; |
| 214 | addChild(Mtemplate); |
| 215 | setTrigger(Mtemplate); |
| 216 | |
| 217 | ConfigurationGroup* trestore = new VerticalConfigurationGroup(false); |
| 218 | trestore->addChild(HostTemplateRestore); |
| 219 | |
| 220 | ConfigurationGroup* tsave = new VerticalConfigurationGroup(false); |
| 221 | tsave->addChild(HostTemplateSave); |
| 222 | |
| 223 | ConfigurationGroup* tcopy = new VerticalConfigurationGroup(false); |
| 224 | tcopy->addChild(HostSelectcopy_from); |
| 225 | tcopy->addChild(HostTemplateCopy_restore); |
| 226 | |
| 227 | ConfigurationGroup* tdelete = new VerticalConfigurationGroup(false); |
| 228 | tdelete->addChild(HostTemplateDelete); |
| 229 | |
| 230 | addTarget("Restore", trestore); |
| 231 | addTarget("Save", tsave); |
| 232 | addTarget("Copy From", tcopy); |
| 233 | addTarget("Delete", tdelete); |
| 234 | |
| 235 | }; |
| 236 | |
| 237 | void popup_error(){ |
| 238 | QString errorText="Could not find any settings to restore."; |
| 239 | MythPopupBox::showOkPopup(gContext->GetMainWindow(), |
| 240 | "Open Failed", |
| 241 | errorText); |
| 242 | } |
| 243 | |
| 244 | void TemplateSettings::choosetemplate () |
| 245 | { |
| 246 | QString templateop; |
| 247 | QString templatename; |
| 248 | QString templatehostname=""; |
| 249 | templateop=gather_setting("HOSTtemplatetype"); |
| 250 | VERBOSE(VB_IMPORTANT, "Button was pressed... " + templateop); |
| 251 | VERBOSE(VB_IMPORTANT, "####################################"); |
| 252 | templatename = "none"; |
| 253 | if ( templateop == "Save" ) |
| 254 | { |
| 255 | templatename=gather_setting("HostTemplateSave"); |
| 256 | templatehostname=gContext->GetHostName(); |
| 257 | VERBOSE(VB_IMPORTANT, "FE----RUNNING... ...." + templateop + " " + templatename ); |
| 258 | savesettings(templatehostname,templatename); |
| 259 | } |
| 260 | else if ( templateop == "Restore" ) |
| 261 | { |
| 262 | //Run Check to see if settings are present |
| 263 | templatename=gather_setting("HostTemplateRestore"); |
| 264 | templatehostname=gContext->GetHostName(); |
| 265 | VERBOSE(VB_IMPORTANT, "FE----RUNNING... " + templateop + " " + templatename ); |
| 266 | if ( templatename == "Default") |
| 267 | { |
| 268 | if ( settings_check(templatename,"Default") ) |
| 269 | { |
| 270 | VERBOSE(VB_IMPORTANT, "FE----RUNNING... c_from " + templateop + " " + templatename ); |
| 271 | c_from("Default" , "Default",gContext->GetHostName()); |
| 272 | gContext->SaveSetting("HOSTrestartfe","1" ); |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | popup_error(); |
| 277 | } |
| 278 | } |
| 279 | else if ( templatename == "Default_1") |
| 280 | { |
| 281 | if ( settings_check("Default" ,"Default_1") ) |
| 282 | { |
| 283 | VERBOSE(VB_IMPORTANT, "FE----RUNNING... c_from " + templateop + " " + templatename ); |
| 284 | c_from("Default_1" , "Default",gContext->GetHostName() ); |
| 285 | gContext->SaveSetting("HOSTrestartfe","1" ); |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | popup_error(); |
| 290 | } |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | if ( settings_check(templatename,templatehostname) ) |
| 295 | { |
| 296 | restoresettings(templatehostname,templatename); |
| 297 | gContext->SaveSetting("HOSTrestartfe","1" ); |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | popup_error(); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | else if ( templateop == "Copy From" ) |
| 306 | { |
| 307 | templatehostname=gather_setting("HostSelectcopy_from"); |
| 308 | templatename=gather_setting("HostTemplateCopy_restore"); |
| 309 | |
| 310 | if ( settings_check(templatename,templatehostname) ) |
| 311 | { |
| 312 | gContext->SaveSetting("HOSTrestartfe","1" ); |
| 313 | VERBOSE(VB_IMPORTANT, "FE----RUNNING... ...." + templateop + " " + templatename + " " + templatehostname ); |
| 314 | c_from(templatehostname,templatename,gContext->GetHostName()); |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | popup_error(); |
| 319 | } |
| 320 | } |
| 321 | else if ( templateop == "Delete" ) |
| 322 | { |
| 323 | templatehostname=gContext->GetHostName(); |
| 324 | templatename=gather_setting("HostTemplateDelete"); |
| 325 | deletesettings(templatehostname,templatename); |
| 326 | remove_deleted_item(templatename); |
| 327 | } |
| 328 | VERBOSE(VB_IMPORTANT, "####################################"); |
| 329 | |
| 330 | }; |
| 331 | |
| 332 | ProfileSettings_MainFrame::ProfileSettings_MainFrame(): |
| 333 | VerticalConfigurationGroup(false,false,false,false) |
| 334 | { |
| 335 | setLabel(QObject::tr("Save/Backup settings")); |
| 336 | TemplateSettings *templatesettings = new TemplateSettings(); |
| 337 | |
| 338 | userapplyButton = new TransButtonSetting; |
| 339 | userapplyButton->setLabel("Run"); |
| 340 | |
| 341 | VerticalConfigurationGroup* frame_profile = |
| 342 | new VerticalConfigurationGroup(false, true,false,false); |
| 343 | frame_profile->addChild(templatesettings); |
| 344 | frame_profile->addChild(userapplyButton); |
| 345 | |
| 346 | addChild(frame_profile); |
| 347 | connect(userapplyButton, SIGNAL(pressed()), |
| 348 | templatesettings,SLOT(choosetemplate())); |
| 349 | }; |
| 350 | |
| 351 | MythCtemplate::MythCtemplate() |
| 352 | { |
| 353 | ProfileSettings_MainFrame *profilesettings_mainframe = new ProfileSettings_MainFrame(); |
| 354 | addChild(profilesettings_mainframe); |
| 355 | }; |