# Path: engine/archiver/zip_format.py
# def zip(source, dest):
#     """Compresses a file/dir, given a source path, to the zip format
#         The resulting file will be created in the given dest path. """
# 
#     dest_directory = os.path.dirname(dest)
# 
#     if not os.path.exists(dest_directory):
#         print("Created zip destination folder. %s" % dest_directory)
#         os.makedirs(dest_directory)
# 
#     if os.path.exists(source):
#         epoch_time = str(int(time.time()))
#         # moved the creation of the zip file inside each case below in order to create the zip only if necessary.
# 
#         if os.path.isfile(source):
# 
#             printDebugInfo(" inside zip, the source is a file ")
# 
#             zOut = zipfile.ZipFile(dest + FILE_NAME_SEPARATOR + epoch_time + ZIP_EXT, mode='w')
#             zOut.write(source, compress_type=zipfile.ZIP_DEFLATED, arcname=os.path.basename(
#                 source))  # This fixes the bug where the compressed file in the zip did not have the correct file extension
#             zOut.close()
#             print ("   Compressed: %s into %s" % (source, dest))
#         elif os.path.isdir(source):
#             printDebugInfo(" inside zip, the source is a directory ")
# 
#             with zipfile.ZipFile(dest + FILE_NAME_SEPARATOR + str(epoch_time) + ZIP_EXT, mode='w') as myzip:
#                 rootlen = len(source)  # use the sub-part of path which you want to keep in your zip file
#                 for base, dirs, files in os.walk(source):
#                     for ifile in files:
#                         fn = os.path.join(base, ifile)
#                         myzip.write(fn, fn[rootlen:], compress_type=zipfile.ZIP_DEFLATED)
# 
#     else:
#         print("File: " + source + " Doesn't exist! zip function aborted.")
# 
# Path: engine/archiver/tar_format.py
# def tar(source, dest):
#     """Compresses a file/dir, given a source path, using tar archiving
#         with bzip2 compression. The resulting file will be created in
#         the given dest path. """
# 
#     if os.path.exists(source):
#         if os.listdir(source):  # Compress files, not an empty directory
#             epochTime = str(int(time.time()))
# 
#             with tarfile.open(dest + SEPARATOR + epochTime + EXT, "w:bz2") as tarBall:
#                 tarBall.add(source, arcname=os.path.basename(source))
#                 tarBall.close()
#     else:
#         print("   File: " + source + " Doesn't exist! tar function aborted.")
# 
# Path: gui/progress_bar.py
# class ProgressBar(gtk.Window):
# 
#     # Clean up allocated memory and remove the timer
#     def destroy_progress(self, widget, data=None):
#         self.destroy()
# 
#     # Update the value of the progress bar so that we get
#     # some movement
#     def setValue(self, pvalue):
#         # Set the new value
#         self.pbar.set_fraction(pvalue)
# 
#     def __init__(self):
#         super(ProgressBar, self).__init__()
# 
#         #Configure the Window
#         self.set_resizable(False)
#         self.connect("destroy", self.destroy_progress)
#         self.set_title("Progress Bar")
#         self.set_position(gtk.WIN_POS_CENTER)
#         self.set_size_request(460, 50)
#         self.set_border_width(0)
# 
#         #Create the VBox in case we want to add additional items later
#         vbox = gtk.VBox(False, 5)
#         vbox.set_border_width(10)
#         self.add(vbox)
#         vbox.show()
# 
#         # Create the ProgressBar
#         self.pbar = gtk.ProgressBar()
#         self.pbar.set_fraction(0.0)
#         self.pbar.set_text("Starting")
#         vbox.add(self.pbar)
#         self.pbar.show()
# 
#         #Display the Window
#         self.show()


import gtk
import os
import shutil
import time
import definitions
import utils.gui
from os.path import expanduser
from engine.archiver.zip_format import zip
from engine.archiver.tar_format import tar
from gui.progress_bar import ProgressBar

    def close_export_dialog(self, event):
        self.hide_all()

    def export(self, event):
        export_base_dir = self.entry_selected_folder.get_text()
        export_raw = self.checkbutton_export_raw.get_active()
        export_compressed = self.checkbutton_export_compressed.get_active()
        export_parsed = self.checkbutton_export_parsed.get_active()

        if not export_base_dir:
            utils.gui.show_error_message(self, "Please select a directory to export to.")
            return
        if not os.path.isdir(export_base_dir):
            utils.gui.show_error_message(self, "Please select a valid directory to export to.")
            return
        if not export_raw and not export_compressed and not export_parsed:
            utils.gui.show_error_message(self, "Please select at least one data type to export.")
            return

        export_dir = os.path.join(export_base_dir, definitions.PLUGIN_COLLECTORS_EXPORT_DIRNAME.replace(
            definitions.TIMESTAMP_PLACEHOLDER, "_" + str(int(time.time()))))
        export_raw_dir = os.path.join(export_dir, definitions.PLUGIN_COLLECTORS_OUTPUT_DIRNAME)
        export_compressed_dir = os.path.join(export_dir, definitions.PLUGIN_COLLECTORS_COMPRESSED_DIRNAME)
        export_parsed_dir = os.path.join(export_dir, definitions.PLUGIN_COLLECTORS_PARSED_DIRNAME)
        os.makedirs(export_raw_dir)
        os.makedirs(export_compressed_dir)
        os.makedirs(export_parsed_dir)

        progress = 0

# Path: engine/archiver/zip_format.py
# def zip(source, dest):
#     """Compresses a file/dir, given a source path, to the zip format
#         The resulting file will be created in the given dest path. """
# 
#     dest_directory = os.path.dirname(dest)
# 
#     if not os.path.exists(dest_directory):
#         print("Created zip destination folder. %s" % dest_directory)
#         os.makedirs(dest_directory)
# 
#     if os.path.exists(source):
#         epoch_time = str(int(time.time()))
#         # moved the creation of the zip file inside each case below in order to create the zip only if necessary.
# 
#         if os.path.isfile(source):
# 
#             printDebugInfo(" inside zip, the source is a file ")
# 
#             zOut = zipfile.ZipFile(dest + FILE_NAME_SEPARATOR + epoch_time + ZIP_EXT, mode='w')
#             zOut.write(source, compress_type=zipfile.ZIP_DEFLATED, arcname=os.path.basename(
#                 source))  # This fixes the bug where the compressed file in the zip did not have the correct file extension
#             zOut.close()
#             print ("   Compressed: %s into %s" % (source, dest))
#         elif os.path.isdir(source):
#             printDebugInfo(" inside zip, the source is a directory ")
# 
#             with zipfile.ZipFile(dest + FILE_NAME_SEPARATOR + str(epoch_time) + ZIP_EXT, mode='w') as myzip:
#                 rootlen = len(source)  # use the sub-part of path which you want to keep in your zip file
#                 for base, dirs, files in os.walk(source):
#                     for ifile in files:
#                         fn = os.path.join(base, ifile)
#                         myzip.write(fn, fn[rootlen:], compress_type=zipfile.ZIP_DEFLATED)
# 
#     else:
#         print("File: " + source + " Doesn't exist! zip function aborted.")
# 
# Path: engine/archiver/tar_format.py
# def tar(source, dest):
#     """Compresses a file/dir, given a source path, using tar archiving
#         with bzip2 compression. The resulting file will be created in
#         the given dest path. """
# 
#     if os.path.exists(source):
#         if os.listdir(source):  # Compress files, not an empty directory
#             epochTime = str(int(time.time()))
# 
#             with tarfile.open(dest + SEPARATOR + epochTime + EXT, "w:bz2") as tarBall:
#                 tarBall.add(source, arcname=os.path.basename(source))
#                 tarBall.close()
#     else:
#         print("   File: " + source + " Doesn't exist! tar function aborted.")
# 
# Path: gui/progress_bar.py
# class ProgressBar(gtk.Window):
# 
#     # Clean up allocated memory and remove the timer
#     def destroy_progress(self, widget, data=None):
#         self.destroy()
# 
#     # Update the value of the progress bar so that we get
#     # some movement
#     def setValue(self, pvalue):
#         # Set the new value
#         self.pbar.set_fraction(pvalue)
# 
#     def __init__(self):
#         super(ProgressBar, self).__init__()
# 
#         #Configure the Window
#         self.set_resizable(False)
#         self.connect("destroy", self.destroy_progress)
#         self.set_title("Progress Bar")
#         self.set_position(gtk.WIN_POS_CENTER)
#         self.set_size_request(460, 50)
#         self.set_border_width(0)
# 
#         #Create the VBox in case we want to add additional items later
#         vbox = gtk.VBox(False, 5)
#         vbox.set_border_width(10)
#         self.add(vbox)
#         vbox.show()
# 
#         # Create the ProgressBar
#         self.pbar = gtk.ProgressBar()
#         self.pbar.set_fraction(0.0)
#         self.pbar.set_text("Starting")
#         vbox.add(self.pbar)
#         self.pbar.show()
# 
#         #Display the Window
#         self.show()


# Path: gui/export_gui.py
import gtk
import os
import shutil
import time
import definitions
import utils.gui
from os.path import expanduser
from engine.archiver.zip_format import zip
from engine.archiver.tar_format import tar
from gui.progress_bar import ProgressBar


    def close_export_dialog(self, event):
        self.hide_all()

    def export(self, event):
        export_base_dir = self.entry_selected_folder.get_text()
        export_raw = self.checkbutton_export_raw.get_active()
        export_compressed = self.checkbutton_export_compressed.get_active()
        export_parsed = self.checkbutton_export_parsed.get_active()

        if not export_base_dir:
            utils.gui.show_error_message(self, "Please select a directory to export to.")
            return
        if not os.path.isdir(export_base_dir):
            utils.gui.show_error_message(self, "Please select a valid directory to export to.")
            return
        if not export_raw and not export_compressed and not export_parsed:
            utils.gui.show_error_message(self, "Please select at least one data type to export.")
            return

        export_dir = os.path.join(export_base_dir, definitions.PLUGIN_COLLECTORS_EXPORT_DIRNAME.replace(
            definitions.TIMESTAMP_PLACEHOLDER, "_" + str(int(time.time()))))
        export_raw_dir = os.path.join(export_dir, definitions.PLUGIN_COLLECTORS_OUTPUT_DIRNAME)
        export_compressed_dir = os.path.join(export_dir, definitions.PLUGIN_COLLECTORS_COMPRESSED_DIRNAME)
        export_parsed_dir = os.path.join(export_dir, definitions.PLUGIN_COLLECTORS_PARSED_DIRNAME)
        os.makedirs(export_raw_dir)
        os.makedirs(export_compressed_dir)
        os.makedirs(export_parsed_dir)

        progress = 0

        pb = ProgressBar()