/*
   Name   : mark_file()
   Purpose: Set the marked attribute in the file metadata record.
            Gets the first selected item for the file system tree
            widget and looks up the file metadata record.
   Input  : None.
   Output : None.
*/
void Fineline_File_System_Tree::mark_file()
{
	Fl_Tree_Item *flti = first_selected_item();
   fl_file_record_t *flrec = NULL;
   char file_path[FL_PATH_MAX]; // FL_PATH_MAX 2048 is an FLTK constant, Fineline FL_PATH_MAX_LENGTH 4096
   string full_path;

   if (flti != 0)
   {
      if (item_pathname(file_path, FL_PATH_MAX, flti) != 0)
      {
         Fineline_Log::print_log_entry("Fineline_File_System_Tree::mark_file() <ERROR> Could not get tree item path.");
         fl_message(" <ERROR> Could not get tree item. ");
         return;
      }

      full_path.append(file_path);
      flrec = find_file(full_path);
      if (flrec != NULL)
      {
         flrec->marked = 1;
         flti->labelcolor(FL_DARK_GREEN);
         flti->labelfont(FL_COURIER_BOLD);
         if (flrec->file_type == TSK_FS_META_TYPE_DIR) //If a directory then mark all the files in the directory.
         {
            mark_children(flti);
         }
         Fl::awake();
         Fineline_Log::print_log_entry("Fineline_File_System_Tree::mark_file() <INFO> marked file.");
      }
   }
   return;
}
// TODO: populate the curve list once we actually have a list of curves.
void ModelerUserInterface::populateCurveList(Property* prop, Fl_Tree_Item* parent) {
	// Create a tree node for this property
	Fl_Tree_Item* item;
	if (parent == NULL) {
		// HACK: We have to add and remove a fake list item so that the tree
		// control will create a root node to put it under.
		curvesTree->remove(curvesTree->add("You shouldn't see this."));

		item = curvesTree->root();
		item->label(prop->getName());
	} else {
		item = curvesTree->add(parent, prop->getName());
	}
	item->labelfont(FL_HELVETICA_BOLD);
	if (GroupProperty* g = dynamic_cast<GroupProperty*>(prop)) {
		if (g->getCollapsed()) {
			item->close();
		}
	} else if (dynamic_cast<RGBProperty*>(prop)) {
		item->close();
	}

	// Examine the list of properties.
	PropertyList* controls = prop->getProperties();
	for (PropertyList::iterator iter = controls->begin();
		 iter != controls->end();
		 iter++) {
        // For now, only RangeProperty is supported by the Curve interface.
		// The RGBProperty is also indirectly supported because it can provide
	    // RangeProperties when its getProperties() method is called.
		// TODO: make this work with more property types (using a new
		// plotting widget?)
		if (RangeProperty* range = dynamic_cast<RangeProperty*>(*iter)) {
			// TODO: connect to Curve object instead of Property object.
			Fl_Tree_Item* childNode = curvesTree->add(item, range->getName());
			curveProps.push_back(range);
			childNode->user_data((void*)(curveProps.size() - 1));
			range->setCurveIndex(curveProps.size() - 1);
			graph->addCurve(range->getValue(), range->getMin(),
				range->getMax());
		} else if ((*iter)->getProperties() != NULL) {
			// Try to get a list of GroupProperties out of it.
			populateCurveList(*iter, item);
		}
	}
}
void Fineline_File_System_Tree::unmark_children(Fl_Tree_Item *flti)
{
   Fl_Tree_Item *fltc;
   fl_file_record_t *flrec = NULL;
   char file_path[FL_PATH_MAX]; // FL_PATH_MAX 2048 is an FLTK constant, Fineline FL_PATH_MAX_LENGTH 4096
   string full_path;
   int ccount, i;

   if ((ccount = flti->children()) > 0)
   {
      for (i = 0; i < ccount; i++)
      {
         fltc = flti->child(i);
         if (fltc != 0)
         {
            if (item_pathname(file_path, FL_PATH_MAX, fltc) != 0)
            {
               Fineline_Log::print_log_entry("Fineline_File_System_Tree::unmark_children() <ERROR> Could not get tree item path.");
               fl_message(" <ERROR> Could not get tree item. ");
               return;
            }

            full_path = file_path;
            flrec = find_file(full_path);
            if (flrec != NULL)
            {
               flrec->marked = 0;
               fltc->labelcolor(FL_FOREGROUND_COLOR);
               fltc->labelfont(FL_HELVETICA);
               if (flrec->file_type == TSK_FS_META_TYPE_DIR) //If a directory then mark all the files in the directory.
               {
                  unmark_children(fltc);
               }
               Fl::awake();
               Fineline_Log::print_log_entry("Fineline_File_System_Tree::unmark_children() <INFO> unmarked file.");
            }
         }
      }
   }
}
void Fineline_File_System_Tree::unmark_file()
{
	Fl_Tree_Item *flti = first_selected_item();
   fl_file_record_t *flrec = NULL;
   char file_path[FL_PATH_MAX]; // FL_PATH_MAX 2048 is an FLTK constant, Fineline FL_PATH_MAX_LENGTH 4096
   string full_path;

   if (flti != 0)
   {
      if (item_pathname(file_path, FL_PATH_MAX, flti) != 0)
      {
         Fineline_Log::print_log_entry("Fineline_File_System_Tree::unmark_file() <ERROR> Could not get tree item path.");
         fl_message(" <ERROR> Could not get tree item. ");
          return;
      }

      full_path.append(file_path);
      flrec = find_file(full_path);
      if (flrec != NULL)
      {
         flrec->marked = 0;
         flti->labelcolor(FL_FOREGROUND_COLOR);
         flti->labelfont(FL_COURIER);
         if (flrec->file_type == TSK_FS_META_TYPE_DIR) //If a directory then mark all the files in the directory.
         {
            //TODO: recursively iterate through the tree subdirectory children and mark each one.
            if (flti->has_children())
            {
               unmark_children(flti);
            }
         }
         Fl::awake();
      }
   }
   return;
}