| | 573 | void create_temp_table (QString create_table_name,QString like_name) |
| | 574 | { |
| | 575 | MSqlQuery query(MSqlQuery::InitCon()); |
| | 576 | query.prepare("create table " + create_table_name + " like " + like_name + ";" ); |
| | 577 | query.exec(); |
| | 578 | if ( like_name == "settings") |
| | 579 | { |
| | 580 | query.prepare("Alter table " + create_table_name + " add unique ( value ) ; " ); |
| | 581 | query.exec(); |
| | 582 | } |
| | 583 | query.prepare(" truncate " + create_table_name + ";" ); |
| | 584 | query.exec(); |
| | 585 | }; |
| | 586 | |
| | 587 | void create_new_table (QString create_table_name,QString like_name) |
| | 588 | { |
| | 589 | //should check for table before blindly creating it. |
| | 590 | MSqlQuery query(MSqlQuery::InitCon()); |
| | 591 | query.prepare("create table " + create_table_name + " like " + like_name + ";" ); |
| | 592 | query.exec(); |
| | 593 | }; |
| | 594 | |
| | 595 | |
| | 596 | void drop_temp_table (QString tname) |
| | 597 | { |
| | 598 | MSqlQuery query(MSqlQuery::InitCon()); |
| | 599 | query.prepare("drop table " + tname + ";" ); |
| | 600 | query.exec(); |
| | 601 | } |
| | 602 | |
| | 603 | |
| | 604 | |
| | 605 | |
| | 606 | void savesettings (QString hname ,QString templatenumber ) |
| | 607 | { |
| | 608 | QString thname; |
| | 609 | thname=hname; |
| | 610 | VERBOSE(VB_IMPORTANT, "Saving..." + templatenumber); |
| | 611 | QString templatename = "settings_" + templatenumber ; |
| | 612 | create_new_table(templatename,"settings"); |
| | 613 | VERBOSE(VB_IMPORTANT, thname ); |
| | 614 | // Remove old saved values |
| | 615 | MSqlQuery query(MSqlQuery::InitCon()); |
| | 616 | query.prepare("delete from " + templatename + |
| | 617 | " where hostname=:HOSTNAME ;" ); |
| | 618 | query.bindValue(":HOSTNAME" ,thname); |
| | 619 | query.exec(); |
| | 620 | //insert all new settings |
| | 621 | query.prepare( "replace into " + templatename + |
| | 622 | " (select * from settings where hostname=:HOSTNAME ) ;" ) ; |
| | 623 | query.bindValue(":HOSTNAME",thname); |
| | 624 | query.exec(); |
| | 625 | |
| | 626 | // repeat for keybindings |
| | 627 | templatename = "keybindings_" + templatenumber ; |
| | 628 | create_new_table(templatename,"keybindings"); |
| | 629 | query.prepare("delete from " + templatename + " where hostname=:HOSTNAME;"); |
| | 630 | query.bindValue(":HOSTNAME",thname); |
| | 631 | query.exec(); |
| | 632 | |
| | 633 | query.prepare("replace into " + templatename + |
| | 634 | " (select * from keybindings where hostname=:HOSTNAME );"); |
| | 635 | query.bindValue(":HOSTNAME",thname); |
| | 636 | query.exec(); |
| | 637 | } |
| | 638 | |
| | 639 | void restoresettings (QString hname, QString templatenumber ) |
| | 640 | { |
| | 641 | QString thname; |
| | 642 | thname=hname; |
| | 643 | VERBOSE(VB_IMPORTANT, "Restore..." + templatenumber + " " + thname); |
| | 644 | if ( settings_check ( templatenumber , thname ) ) |
| | 645 | { |
| | 646 | // DEFAULT settings are handled by the copy from routine |
| | 647 | MSqlQuery query(MSqlQuery::InitCon()); |
| | 648 | QStringList tablelist ; |
| | 649 | QStringList::Iterator it; |
| | 650 | QString current_table; |
| | 651 | tablelist.append ("settings"); |
| | 652 | tablelist.append ("keybindings"); |
| | 653 | QString templatename; |
| | 654 | for ( it = tablelist.begin(); it != tablelist.end(); ++it ) |
| | 655 | { |
| | 656 | current_table = *it ; |
| | 657 | //find template table to use |
| | 658 | QString templatename = current_table + "_" + templatenumber ; |
| | 659 | QString temptable="temp_table_" + current_table + "_" + thname ; |
| | 660 | |
| | 661 | // create temp table for merging settings |
| | 662 | //The merge is needed to account for any new settings. |
| | 663 | create_temp_table(temptable, current_table ); |
| | 664 | |
| | 665 | // copy in current settings |
| | 666 | query.prepare( "replace into " + temptable + |
| | 667 | " (select * from " + current_table + |
| | 668 | " where hostname=:HOSTNAME );" ); |
| | 669 | query.bindValue( ":HOSTNAME" , thname ); |
| | 670 | query.exec(); |
| | 671 | |
| | 672 | // copy in stored settings |
| | 673 | query.prepare("replace into " + temptable + |
| | 674 | " (select * from " + templatename + |
| | 675 | " where hostname=:HOSTNAME ); "); |
| | 676 | query.bindValue(":HOSTNAME",thname ); |
| | 677 | query.exec(); |
| | 678 | |
| | 679 | // remove current settings. |
| | 680 | //Need to remove because the old table allows for duplicates |
| | 681 | //and replace into doesn't seem to replace |
| | 682 | query.prepare("delete from " + current_table + |
| | 683 | " where hostname=:HOSTNAME ;"); |
| | 684 | query.bindValue(":HOSTNAME",thname); |
| | 685 | query.exec(); |
| | 686 | |
| | 687 | // copy new settings from temp to current |
| | 688 | query.prepare("replace into " + current_table + |
| | 689 | " (select * from " + temptable + " );"); |
| | 690 | query.exec(); |
| | 691 | // drop temptable |
| | 692 | drop_temp_table(temptable); |
| | 693 | } |
| | 694 | } |
| | 695 | }; |
| | 696 | |
| | 697 | void deletesettings(QString deletehost , QString templatenumber) |
| | 698 | { |
| | 699 | int tempItem; |
| | 700 | QString dhostname; |
| | 701 | dhostname = deletehost; |
| | 702 | |
| | 703 | VERBOSE(VB_IMPORTANT, "Deleteing..." + templatenumber); |
| | 704 | QString templatename = "settings_" + templatenumber ; |
| | 705 | // Remove old saved values |
| | 706 | MSqlQuery query(MSqlQuery::InitCon()); |
| | 707 | query.prepare("delete from " + templatename + " where hostname=:HOSTNAME ;"); |
| | 708 | query.bindValue(":HOSTNAME" ,dhostname); |
| | 709 | query.exec(); |
| | 710 | //check if ok to delete table |
| | 711 | query.prepare( "select count(*) from " + templatename + ";" ); |
| | 712 | if (query.exec() && query.isActive() && query.size() > 0) |
| | 713 | { |
| | 714 | query.next(); |
| | 715 | tempItem = query.value(0).toInt(); |
| | 716 | if ( tempItem == 0 ) |
| | 717 | drop_temp_table(templatename); |
| | 718 | } |
| | 719 | |
| | 720 | // repeat for keybindings |
| | 721 | templatename = "keybindings_" + templatenumber ; |
| | 722 | query.prepare("delete from " + templatename + " where hostname=:HOSTNAME ;"); |
| | 723 | query.bindValue( ":HOSTNAME" , dhostname ); |
| | 724 | query.exec(); |
| | 725 | //check if ok to delete table |
| | 726 | query.prepare("select count(*) from " + templatename + ";"); |
| | 727 | if (query.exec() && query.isActive() && query.size() > 0) |
| | 728 | { |
| | 729 | query.next(); |
| | 730 | tempItem = query.value(0).toInt(); |
| | 731 | if ( tempItem == 0 ) |
| | 732 | drop_temp_table(templatename); |
| | 733 | } |
| | 734 | } |
| | 735 | |
| | 736 | void c_from(QString copyhost, QString templatenumber, QString hname) |
| | 737 | { |
| | 738 | VERBOSE(VB_IMPORTANT, "Copy " + copyhost + " " + |
| | 739 | templatenumber + "to " + hname ); |
| | 740 | |
| | 741 | if ( settings_check ( templatenumber , copyhost ) || |
| | 742 | templatenumber == "Current" || |
| | 743 | templatenumber == "Default" || templatenumber == "Default_1" ) |
| | 744 | { |
| | 745 | MSqlQuery query(MSqlQuery::InitCon()); |
| | 746 | //Create temp table, copy in settings from host_template |
| | 747 | //update hostname for new host,copy temp_table to settings. |
| | 748 | QStringList tablelist ; |
| | 749 | QStringList::Iterator it; |
| | 750 | QString current_table ; |
| | 751 | tablelist.append ("settings"); |
| | 752 | tablelist.append ("keybindings"); |
| | 753 | QString templatename; |
| | 754 | QString temptable; |
| | 755 | QString sub_sql; |
| | 756 | for ( it = tablelist.begin(); it != tablelist.end(); ++it ) |
| | 757 | { |
| | 758 | current_table = *it ; |
| | 759 | VERBOSE(VB_IMPORTANT, current_table ); |
| | 760 | //find template table to use |
| | 761 | if ( templatenumber == "Current" ) |
| | 762 | templatename = current_table ; |
| | 763 | else if ( templatenumber == "Default_1") |
| | 764 | templatename = current_table + "_" + "Default" ; |
| | 765 | else |
| | 766 | templatename = current_table + "_" + templatenumber ; |
| | 767 | |
| | 768 | temptable="temp_table_" + current_table + "_" + hname ; |
| | 769 | // create temp table for merging settings |
| | 770 | create_temp_table(temptable, current_table ); |
| | 771 | VERBOSE(VB_IMPORTANT, temptable + " " + current_table); |
| | 772 | //copy current settings from this host into temptable |
| | 773 | //minus all Mythvantage settings |
| | 774 | if ( current_table == "settings") |
| | 775 | { |
| | 776 | query.prepare( "replace into " + temptable + |
| | 777 | " (select * from " + current_table + |
| | 778 | " where hostname=:HOSTNAME and value not like 'HOST%' ) ; " ); |
| | 779 | query.bindValue(":HOSTNAME" , hname); |
| | 780 | } |
| | 781 | else |
| | 782 | { |
| | 783 | query.prepare( "replace into " + temptable + |
| | 784 | " (select * from " + current_table + |
| | 785 | " where hostname=:HOSTNAME );"); |
| | 786 | query.bindValue(":HOSTNAME" , hname); |
| | 787 | } |
| | 788 | query.exec(); |
| | 789 | |
| | 790 | // update hostname to match the hostname of the template |
| | 791 | query.prepare ("update " + temptable + " set hostname=:HOSTNAME; "); |
| | 792 | query.bindValue(":HOSTNAME" , copyhost ); |
| | 793 | query.exec(); |
| | 794 | |
| | 795 | // copy current settings from copy host into temptable |
| | 796 | //minus all Mythvantage settings |
| | 797 | if ( current_table == "settings") |
| | 798 | query.prepare( "replace into " + temptable + |
| | 799 | " (select * from " + templatename + |
| | 800 | " where hostname=:HOSTNAME and value not like 'HOST%' ); " ); |
| | 801 | else |
| | 802 | query.prepare( "replace into " + temptable + |
| | 803 | " (select * from " + templatename + |
| | 804 | " where hostname=:HOSTNAME );"); |
| | 805 | query.bindValue( ":HOSTNAME" , copyhost ); |
| | 806 | query.exec(); |
| | 807 | |
| | 808 | //update hostname to match current |
| | 809 | query.prepare ("update " + temptable + " set hostname=:HOSTNAME ;"); |
| | 810 | query.bindValue( ":HOSTNAME" , hname ); |
| | 811 | query.exec(); |
| | 812 | |
| | 813 | // delete old settings from settings table |
| | 814 | if ( current_table == "settings") |
| | 815 | { |
| | 816 | query.prepare( "delete from " + current_table + |
| | 817 | " where hostname=:HOSTNAME and value not like 'HOST%' ;" ); |
| | 818 | query.bindValue( ":HOSTNAME" , hname ); |
| | 819 | } |
| | 820 | else |
| | 821 | { |
| | 822 | query.prepare( "delete from " + current_table + |
| | 823 | " where hostname=:HOSTNAME ;" ); |
| | 824 | query.bindValue(":HOSTNAME" , hname ); |
| | 825 | } |
| | 826 | query.exec(); |
| | 827 | |
| | 828 | // copy settings from temptable to settings |
| | 829 | if ( current_table == "settings") |
| | 830 | query.prepare( "replace into " + current_table + |
| | 831 | " (select * from " + temptable + |
| | 832 | " where value not like 'HOST% ' ) ;" ); |
| | 833 | else |
| | 834 | query.prepare( "replace into " + current_table + |
| | 835 | " (select * from " + temptable + " );" ); |
| | 836 | |
| | 837 | query.exec(); |
| | 838 | drop_temp_table(temptable); |
| | 839 | }; |
| | 840 | }; |
| | 841 | }; |
| | 842 | |
| | 843 | bool settings_check (QString tname , QString hname) |
| | 844 | { |
| | 845 | int tempItem; |
| | 846 | bool returncode = FALSE ; |
| | 847 | QString real_tname; |
| | 848 | if ( tname != "settings" ) |
| | 849 | real_tname="settings_"+tname; |
| | 850 | |
| | 851 | QString thname; |
| | 852 | thname = hname; |
| | 853 | |
| | 854 | QString sub_sql; |
| | 855 | MSqlQuery query(MSqlQuery::InitCon()); |
| | 856 | sub_sql="SELECT count(data) from "; |
| | 857 | sub_sql+=real_tname; |
| | 858 | sub_sql+=" where hostname=:HOSTNAME ; "; |
| | 859 | query.prepare(sub_sql); |
| | 860 | query.bindValue(":HOSTNAME" , thname ); |
| | 861 | |
| | 862 | if (query.exec() && query.isActive() && query.size() > 0 ) |
| | 863 | { |
| | 864 | while (query.next()) |
| | 865 | { |
| | 866 | tempItem = query.value(0).toInt(); |
| | 867 | if ( tempItem >= 5 ) |
| | 868 | returncode = TRUE; |
| | 869 | else |
| | 870 | VERBOSE(VB_IMPORTANT,tname + |
| | 871 | " Group does not contain valid settings for " + thname); |
| | 872 | |
| | 873 | |
| | 874 | } |
| | 875 | } |
| | 876 | else |
| | 877 | VERBOSE(VB_IMPORTANT,"Couldn't find " + real_tname ); |
| | 878 | return returncode; |
| | 879 | } |