"""
IDAPython script providing an user interface for setting the configuration for
annotating database files with MSDN information.

Authors: Moritz Raabe, William Ballenthin
Copyright 2014 Mandiant, A FireEye Company

Mandiant licenses this file to you under the Apache License, Version
2.0 (the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
"""

import sys
import logging
import traceback
import ConfigParser
from ConfigParser import SafeConfigParser
from PySide import QtGui
from PySide import QtCore
from PySide.QtCore import Qt

idaapi.require("IDB_MSDN_Annotator")


CONFIG_FILE = 'MSDN_annotations.cfg'


g_logger = logging.getLogger(__name__)

def getDefaultMsdnDataDir():
    return os.path.abspath(os.path.join(idaapi.get_user_idadir(), 'MSDN_data'))

class MSDNAnnotationDialog(QtGui.QDialog):

    def read_config(self):
        config = {}
        if not self.config_parser.has_section('Functions') or \
           not self.config_parser.has_section('Arguments') or \
           not self.config_parser.has_section('Constants'):
            # Create default
            self.config_parser.add_section('Functions')
            self.config_parser.add_section('Arguments')
            self.config_parser.add_section('Constants')
            config['functions_annotate'] = True
            config['functions_repeatable_comment'] = False
            config['arguments_annotate'] = True
            config['constants_import'] = True
            config['msdn_data_dir'] = getDefaultMsdnDataDir()

        else:
            # Read existing
            config['functions_annotate'] = self.config_parser.getboolean('Functions', 'annotate')
            config['functions_repeatable_comment'] = self.config_parser.getboolean('Functions', 'repeatable_comment')
            config['arguments_annotate'] = self.config_parser.getboolean('Arguments', 'annotate')
            config['constants_import'] = self.config_parser.getboolean('Constants', 'import')
            try:
                config['msdn_data_dir'] = self.config_parser.get('Constants', 'msdn_data_dir')
            except ConfigParser.NoOptionError:
                config['msdn_data_dir'] = getDefaultMsdnDataDir()

        return config

    def save_config(self):
        self.config_parser.set('Functions', 'annotate', str(self.chkFunctionsAnnotate.isChecked()))
        self.config_parser.set('Functions', 'repeatable_comment', str(self.chkFuntcsRepeatable.isChecked()))
        self.config_parser.set('Arguments', 'annotate', str(self.chkArgumentsAnnotate.isChecked()))
        self.config_parser.set('Constants', 'import', str(self.chkConstantsImport.isChecked()))
        self.config_parser.set('Constants', 'msdn_data_dir', str(self.dirText.text()) )

        with open(self.file_path, 'wb') as conffile: