Ejemplo n.º 1
0
bool MessageFilters::remFilter(const MessageFilter& filter)
{
  // iterate over the filters
  for (int i = 0; i < maxMessageFilters; i++)
  {
    // if a match is found, remove it
    if (m_filters[i] && (*m_filters[i] == filter))
      return remFilter(i);
  }

  return false;
}
Ejemplo n.º 2
0
bool MessageFilters::remFilter(const QString& name)
{
  // iterate over the filters
  for (int i = 0; i < maxMessageFilters; i++)
  {
    // if a match is found, remove it
    if (m_filters[i] && (m_filters[i]->name() == name))
      return remFilter(i);
  }

  // no matching filter found
  return false;
}
Ejemplo n.º 3
0
bool
Filter::saveFilters(void)
{
  FILE *in;
  FILE *out;
  FilterItem* re;
  char msg[MAXLEN + 1];
  char *p;
  int count = 0;
  bool done  = false;
  bool start = false;
  
#ifdef DEBUG_FILTER
  printf("Filter::saveFilters(void)\n");
#endif
  // if no filename can't save
  if (m_file.isEmpty())
    return false;
  
  // Save filter file
  // Open source and destination files
  QString outfilename = NULL;
  QString infilename = m_file;
  
  outfilename = infilename + ".new";
  in = fopen ((const char*)infilename, "r");
  out = fopen ((const char*)outfilename, "w+");
  if (in == 0)
  {
    fprintf (stderr, "Couldn't open filter file. '%s' - %s\n", 
	     (const char*)infilename, strerror(errno));
  }
  if (out == 0)
  {
    fprintf (stderr, "Couldn't open filter file. '%s' - %s\n", 
	     (const char*)outfilename, strerror(errno));
    return false;
  }
  else
  {
    char *sType = NULL;
    
    if (in)
    {
      // Parse source file
      while (fgets (msg, MAXLEN, in) != NULL)
      {
	// terminate the line
	p = index (msg, '\n');
	if (p)
	  *p = 0;
	p = index (msg, '\r');
	if (p)
	  *p = 0;
	
	// terminate on CF or LF 
	// if this filter uses section names
	if (!m_type.isEmpty())
	{
	  // end of section - dump all items left in list that belong here
	  if (sType && !msg[0])
	  {
	    // if this is our section
	    if (m_type == sType)
	    {
	      // done copying filters that existed in file already
	      // dump whatever is left in list
	      FilterListIterator it(m_filterItems);
	      for (re = it.toFirst(); re != NULL; re = ++it)
	      {
#ifdef DEBUG_FILTER
		printf("OUT: '%s'\n", (const char*)re->name());
#endif
		fprintf(out, "%s\n", (const char*)re->name());
	      }
	      done = true;
	      start = false;
	    } // end if our section
	    free(sType);
	    sType = 0;
	  } // end if end of section
	}
	
	// treat lines beginning with # or ; as comments
	if ( (msg[0] == '#') || (msg[0] == ';'))
	{
#ifdef DEBUG_FILTER
	  printf("OUT: '%s'\n", msg);
#endif
	  fprintf(out, "%s", msg);
	  msg[0] = 0;
	}
	
	// preserve blank lines
	if (!msg[0])
	  fprintf(out, "\n");
	
	if (!m_type.isEmpty())
	{
	  // check for section name
	  if (msg[0] == '[')
	  {
	    p = index(msg, ']');
	    if (p)
	      *p = 0;
	    p = index(msg, '\r');
	    if (p)
	      *p = 0;
	    if (sType)
	      free(sType);
	    sType = strdup(msg + 1);
#ifdef DEBUG_FILTER
	    printf("OUT: '[%s]'\n", sType);
#endif
	    fprintf(out, "[%s]\n", sType);
	    start = true;
	    msg[0] = 0;
	  }
	} // end if filter uses section names  
	
	if (msg[0])
	{
	  // if this is our section
	  if (sType && (m_type == sType))
	  { 
	    // look for a match, if found put it in the file and 
	    // remove it from the list
	    FilterListIterator it(m_filterItems);
	    for (re = it.toFirst(); re != NULL; re = ++it)
	    {
	      // if we found a match, output it
	      if (re->name() ==  msg)
	      {
#ifdef DEBUG_FILTER
		printf("OUT: '%s'\n", msg);
#endif
		fprintf(out, "%s\n", msg);
		count++;
		remFilter(msg);
		break;
	      }
	     } // for all items in list
	  } // end if our section
	  
	  // someone elses section, just output it without alteration
	  else
	  {
#ifdef DEBUG_FILTER
	    printf("OUT: '%s'\n", msg);
#endif
	    fprintf(out, "%s\n", msg);
	  }
	} // end if msg
      }  // end while lines in source file
    } // end if source file
    
    // if we still have filters in our list, we never found our section
    // add it
    if (!done)
    {
      if (!m_type.isEmpty() && !start)
      {
#ifdef DEBUG_FILTER
	printf("done parsing file... creating section '%s'\n", 
	       (const char*)m_type);
#endif
	fprintf(out, "\n[%s]\n", (const char*)m_type);
      }
       
      // done copying filters that existed in file already
      // dump whatever is left in list
      FilterListIterator it(m_filterItems);
      for (re = it.toFirst(); re != NULL; re = ++it)
      {
#ifdef DEBUG_FILTER
	printf("OUT: '%s'\n", (const char*)re->name());
#endif
	fprintf(out, "%s\n", (const char*)re->name());
      }
    }
    if (fflush (out))
      fprintf (stderr, "Couldn't flush filter file. '%s' - %s\n", 
	       (const char*)outfilename, strerror(errno));
    if (fclose (out))
      fprintf (stderr, "Couldn't flush filter file. '%s' - %s\n", 
	       (const char*)outfilename, strerror(errno));
    if (in)
      fclose (in);
    
    //  printf ("Filter file saved '%s'\n", outfilename);
    
    // rename files
    int result;
    QString bakFileName = infilename + ".bak";
    // rename input file to filename.bak
    result = rename((const char*)infilename,
		    (const char*)bakFileName);
    if (result != 0)
      fprintf(stderr, "rename '%s' to '%s' failed: (%d): %s\n",
	      (const char*)infilename, (const char*)bakFileName,
	      errno, strerror(errno));

    // rename the output file to the input filename
    result = rename((const char*)outfilename,
		    (const char*)infilename);

    if (result != 0)
      fprintf(stderr, "rename '%s' to '%s' failed: (%d): %s\n",
	      (const char*)outfilename, (const char*)infilename,
	      errno, strerror(errno));

     if (sType)
       free(sType);
  }
  
  // re-read the filters from file
  loadFilters();
  
  return true;
} // end saveFilters